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: Remove assert that panics on group_by followed by head(n), where n is larger then the frame height #20819

Merged
merged 2 commits into from
Jan 21, 2025

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 21, 2025

Closes #20818.

Following the code further down, I think the panicking assert is invalid, and can be removed... @ritchie46, could you double-check, in case I missed some other paths that do rely on this?🤞😅 It's not an error to ask for a slice larger than the number of available rows - in this case it should just get clipped to how many rows there actually are.

Example

The 1.20.0 release panics on the following:

import polars as pl

lf = pl.LazyFrame({
    "key": ["xx", "yy", "zz", "xx", "zz", "zz"],
    "val": [15, 25, 10, 20, 20, 20],
})
grp = lf.group_by("key").agg(pl.col("val").sum())
grp.head(5).collect()  # aggregate result is 3 rows; head(5) panics

@github-actions github-actions bot added fix Bug fix rust Related to Rust Polars labels Jan 21, 2025
@alexander-beedie alexander-beedie changed the title fix(rust): Remove assert that panics for valid len values fix: Remove assert that can panic for group_by followed by head(n), where n is larger then the frame height Jan 21, 2025
@github-actions github-actions bot added the python Related to Python Polars label Jan 21, 2025
Copy link

codecov bot commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.69%. Comparing base (099ee3c) to head (2378854).
Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20819      +/-   ##
==========================================
+ Coverage   79.63%   79.69%   +0.06%     
==========================================
  Files        1568     1568              
  Lines      222970   222976       +6     
  Branches     2544     2544              
==========================================
+ Hits       177555   177699     +144     
+ Misses      44831    44693     -138     
  Partials      584      584              

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

@alexander-beedie alexander-beedie changed the title fix: Remove assert that can panic for group_by followed by head(n), where n is larger then the frame height fix: Remove assert that panics on group_by followed by head(n), where n is larger then the frame height Jan 21, 2025
@ritchie46
Copy link
Member

The length should then be truncated to the len of the groups. It is in invariant of the GroupsPositions that the len is in bounds.

@alexander-beedie
Copy link
Collaborator Author

alexander-beedie commented Jan 21, 2025

The length should then be truncated to the len of the groups. It is in invariant of the GroupsPositions that the len is in bounds.

Yup; seemed to be happening implicitly somewhere - have made the truncation explicit, and added a small note about the invariant 👍

@ritchie46 ritchie46 merged commit 30a7e34 into pola-rs:main Jan 21, 2025
28 checks passed
@alexander-beedie alexander-beedie deleted the remove-panicking-assert branch January 21, 2025 15:30
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.

Panic on valid head(n) following group_by
2 participants