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

feat(python): move F-order data in and out of numpy to polars zero copy #14259

Merged
merged 8 commits into from
Feb 4, 2024

Conversation

ritchie46
Copy link
Member

@ritchie46 ritchie46 commented Feb 4, 2024

We could already read F-order numpy data zero-copy into a DataFrame.

This PR adds a fast path for moving data back to numpy. If all arrays are back to back allocated we create a zero-copy numpy buffer.

This means you can move read-only data in and out polars for free. :)

import polars as pl

rows = int(1e8)
cols = 5
x = np.ones((rows, cols), order='F')
# no-copy
df = pl.DataFrame(x)
# no-copy
df.to_numpy()

# But completely safe. We will never mutate
# underlying data. If you do, we will simply
# trigger a copy when you move data out into numpy

Edit: cannot make this work on windows. @ghuls any idea what windows + numpy does differently?

EDIT: nevermind. Using the numpy C-API.

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Feb 4, 2024
@ritchie46 ritchie46 changed the title feat(python): move F-order data in and out of numpy to polars zero copy feat(python): move F-order data in and out of numpy to polars zero copy Feb 4, 2024
@ritchie46 ritchie46 merged commit de05552 into main Feb 4, 2024
15 checks passed
@ritchie46 ritchie46 deleted the numpy branch February 4, 2024 19:35
@c-peters c-peters added the accepted Ready for implementation label Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants