diff --git a/crates/polars-expr/src/expressions/aggregation.rs b/crates/polars-expr/src/expressions/aggregation.rs index 60471ffa2b8c..7b578cd77eb3 100644 --- a/crates/polars-expr/src/expressions/aggregation.rs +++ b/crates/polars-expr/src/expressions/aggregation.rs @@ -580,7 +580,7 @@ impl PartitionedAggregation for AggregationExpr { let mask = agg_count.equal(0 as IdxSize); let agg_count = agg_count.set(&mask, None).unwrap().into_series(); - let agg_s = &agg_s / &agg_count; + let agg_s = &agg_s / &agg_count.cast(agg_s.dtype()).unwrap(); Ok(agg_s?.with_name(new_name).into_column()) }, _ => Ok(Column::full_null( diff --git a/py-polars/tests/unit/operations/test_group_by.py b/py-polars/tests/unit/operations/test_group_by.py index e77911ab3296..9bd96de12873 100644 --- a/py-polars/tests/unit/operations/test_group_by.py +++ b/py-polars/tests/unit/operations/test_group_by.py @@ -1251,3 +1251,20 @@ def test_group_by_cse_dup_key_alias_22238() -> None: pl.DataFrame({"a": [1, 2], "a_with_alias": [1, 2], "x": [11, 5]}), check_row_order=False, ) + + +def test_group_by_22328() -> None: + N = 20 + + df1 = pl.select( + x=pl.repeat(1, N // 2).append(pl.repeat(2, N // 2)).shuffle(), + y=pl.lit(3.0, pl.Float32), + ).lazy() + + df2 = pl.select(x=pl.repeat(4, N)).lazy() + + assert ( + df2.join(df1.group_by("x").mean().with_columns(z="y"), how="left", on="x") + .with_columns(pl.col("z").fill_null(0)) + .collect() + ).shape == (20, 3)