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: Fix from_arrow for struct type #17839

Merged
merged 5 commits into from
Jul 25, 2024

Conversation

delsner
Copy link
Contributor

@delsner delsner commented Jul 24, 2024

Motivation

Using an arrow struct column type in an arrow table causes from_arrow to fail with the following error message:

tests/unit/dataframe/test_df.py:210: in test_from_arrow
    df = cast(pl.DataFrame, pl.from_arrow(b))
polars/convert/general.py:433: in from_arrow
    arrow_to_pydf(
polars/_utils/construction/dataframe.py:1171: in arrow_to_pydf
    elif isinstance(column.type, pa.StructType) and column.num_chunks > 1:
E   AttributeError: 'pyarrow.lib.StructArray' object has no attribute 'num_chunks'

Changes

  • Fix arrow_to_pydf function to first check for the num_chunks attribute
  • Update unit test
  • Ruff updates

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jul 24, 2024
@ritchie46
Copy link
Member

Thanks, can you add a test?

Copy link

codecov bot commented Jul 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.50%. Comparing base (4c19115) to head (bebd395).
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17839      +/-   ##
==========================================
- Coverage   80.51%   80.50%   -0.01%     
==========================================
  Files        1503     1504       +1     
  Lines      197038   197162     +124     
  Branches     2805     2806       +1     
==========================================
+ Hits       158641   158723      +82     
- Misses      37876    37918      +42     
  Partials      521      521              

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

@delsner
Copy link
Contributor Author

delsner commented Jul 24, 2024

Thanks, can you add a test?

I added a unit test for both chunked and regular arrays and updated the existing unit test for from_array.

@delsner
Copy link
Contributor Author

delsner commented Jul 25, 2024

anything else that I should include in the PR @ritchie46?

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @delsner

@ritchie46 ritchie46 merged commit eb760cd into pola-rs:main Jul 25, 2024
14 checks passed
@delsner delsner deleted the fix-arrow-struct-to-pydf branch July 25, 2024 08:50
@AlexH-BoxFish
Copy link

I ran into this same issue and while this fix does work, I was wondering if the pa.StructType can ever have a num_chunks method? From the docs it doesn't look like it.

The only type I can see with a num_chunks method is pa.ChunkedArray, so perhaps there's another bug in this block?

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.

3 participants