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: Don't divide by zero in partitioned group-by #21498

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/polars-expr/src/expressions/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,12 @@ impl PartitionedAggregation for AggregationExpr {
let count = &fields[1];
let (agg_count, agg_s) =
unsafe { POOL.join(|| count.agg_sum(groups), || sum.agg_sum(groups)) };

let agg_count = agg_count.idx().unwrap();
// Ensure that we don't divide by zero.
let agg_count = agg_count
.apply_values(|v| if v == 0 { 1 } else { v })
.into_series();
let agg_s = &agg_s / &agg_count;
Ok(agg_s?.with_name(new_name).into_column())
},
Expand Down
Loading