Skip to content

fix: Maintain float32 type in partitioned group-by #22340

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

Merged
merged 1 commit into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crates/polars-expr/src/expressions/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/unit/operations/test_group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading