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

fix(rust, python): Correctly convert data frames to NumPy for C index order #17000

Merged
merged 2 commits into from
Jun 17, 2024

Conversation

borchero
Copy link
Contributor

@borchero borchero commented Jun 16, 2024

Motivation

#16288 introduced a regression that caused to_numpy(order="c") calls to silently yield erroneous NumPy arrays whenever the data frame has more than one chunk. To reproduce with the latest release or polars main:

import numpy as np
import polars as pl
from polars.testing import assert_frame_equal

rng = np.random.default_rng()
df = pl.DataFrame({
    f"col_{i}": rng.normal(size=100_000)
    for i in range(1000)
})
df_chunked = pl.concat([df.slice(i * 10_000, 10_000) for i in range(10)])
assert_frame_equal(
    df_chunked,
    pl.from_numpy(df_chunked.to_numpy(order="c"), schema=df_chunked.schema)
)

Currently, this results in an assertion failure. This PR fixes the issue.


I'm not sure if this is easily feasible but it would be great if this change could be released prior to a v1 release as it is quite a nasty bug 👀

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jun 16, 2024
Copy link

codecov bot commented Jun 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.01%. Comparing base (1a47c46) to head (768931e).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17000      +/-   ##
==========================================
- Coverage   81.02%   81.01%   -0.02%     
==========================================
  Files        1446     1446              
  Lines      190424   190422       -2     
  Branches     2709     2708       -1     
==========================================
- Hits       154291   154270      -21     
- Misses      35636    35655      +19     
  Partials      497      497              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ritchie46
Copy link
Member

Thanks for the fix. Can you add the test with a small amount of data. Something like this:

rng = np.random.default_rng()
df = pl.DataFrame({
    f"col_{i}": rng.normal(size=20)
    for i in range(3)
})
df_chunked = pl.concat([df.slice(i * 10, 10) for i in range(3)])
assert_frame_equal(
    df_chunked,
    pl.from_numpy(df_chunked.to_numpy(order="c"), schema=df_chunked.schema)
)

@borchero
Copy link
Contributor Author

Thanks for adding the test @ritchie46!

@ritchie46 ritchie46 merged commit c19c377 into pola-rs:main Jun 17, 2024
26 checks passed
@borchero borchero deleted the fix-to-numpy branch June 17, 2024 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants