Skip to content

Commit

Permalink
fix: Don't divide by zero in partitioned group-by (#21498)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Feb 27, 2025
1 parent 5e280d9 commit fec5425
Showing 1 changed file with 6 additions and 0 deletions.
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

0 comments on commit fec5425

Please sign in to comment.