Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Array zero-copy from numpy array. #14946

Open
2 tasks done
franz101 opened this issue Mar 9, 2024 · 1 comment
Open
2 tasks done

Support Array zero-copy from numpy array. #14946

franz101 opened this issue Mar 9, 2024 · 1 comment
Labels
A-interop-numpy Area: interoperability with NumPy enhancement New feature or an improvement of an existing feature performance Performance issues or improvements python Related to Python Polars

Comments

@franz101
Copy link

franz101 commented Mar 9, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import numpy as np
import pyarrow as pa
import polars as pl
# Working example
values = np.random.rand(1_000_000, 512).flatten()
typ = pa.list_(pa.field("name", pa.float64()), 512)
result = pa.FixedSizeListArray.from_arrays(values, type=typ)
arrow_table = pa.Table.from_arrays([result], names=["arr"])
# Crashes memory
df = pl.DataFrame(
    {
        "arr": np.random.rand(1_000_000, 512),
    },
    schema={
        "arr": pl.Array(inner=pl.Float64, width=512),
    },
)

https://colab.research.google.com/drive/1_c9xUIoAdDgtc2hOridsvUcE5IxFhBxN?usp=sharing

Log output

Memory used by pyarrow 4.8gb
Memory used by polars 7gb

Issue description

I know arrow is not optimal for arrays but this example should work:
https://colab.research.google.com/drive/1_c9xUIoAdDgtc2hOridsvUcE5IxFhBxN#scrollTo=VBpvlRvcXR5u

As you can see pyarrow works but polars crashes memory.

It works with smaller arrays though

Expected behavior

Same or less memory than yarrow

Installed versions

--------Version info---------
Polars:               0.20.2
Index type:           UInt32
Platform:             Linux-6.1.58+-x86_64-with-glibc2.35
Python:               3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          2.2.1
connectorx:           <not installed>
deltalake:            <not installed>
fsspec:               2023.6.0
gevent:               <not installed>
matplotlib:           3.7.1
numpy:                1.25.2
openpyxl:             3.1.2
pandas:               1.5.3
pyarrow:              14.0.2
pydantic:             2.6.3
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           2.0.28
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>
@franz101 franz101 added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Mar 9, 2024
@ritchie46
Copy link
Member

We should be able to do this zero-copy. Currently there is one copy, I think. It isn't a bug, more a feature request. Also the naming of the issue is a but dramatic.

@ritchie46 ritchie46 changed the title FixedSizeListArray Memory crash / memory inefficiency Support Array zero-copy from numpy array. Mar 10, 2024
@ritchie46 ritchie46 added enhancement New feature or an improvement of an existing feature and removed bug Something isn't working needs triage Awaiting prioritization by a maintainer labels Mar 10, 2024
@orlp orlp added the performance Performance issues or improvements label Mar 11, 2024
@stinodego stinodego added the A-interop-numpy Area: interoperability with NumPy label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-interop-numpy Area: interoperability with NumPy enhancement New feature or an improvement of an existing feature performance Performance issues or improvements python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

4 participants