diff --git a/crates/polars-expr/src/expressions/gather.rs b/crates/polars-expr/src/expressions/gather.rs index bd602a15cd34..a6450bcb531b 100644 --- a/crates/polars-expr/src/expressions/gather.rs +++ b/crates/polars-expr/src/expressions/gather.rs @@ -84,7 +84,7 @@ impl PhysicalExpr for GatherExpr { }; ac.with_series(taken.into_series(), true, Some(&self.expr))?; - ac.with_update_groups(UpdateGroups::WithGroupsLen); + ac.with_update_groups(UpdateGroups::WithSeriesLen); Ok(ac) } diff --git a/py-polars/tests/unit/operations/test_gather.py b/py-polars/tests/unit/operations/test_gather.py index 4c74f60a7ff9..1d7cc1a85fef 100644 --- a/py-polars/tests/unit/operations/test_gather.py +++ b/py-polars/tests/unit/operations/test_gather.py @@ -201,3 +201,15 @@ def test_gather_array_outer_validity_19482() -> None: expect = pl.Series([[1], None], dtype=pl.Array(pl.Int64, 1)) assert_series_equal(s, expect) assert_series_equal(s.gather([0, 1]), expect) + + +def test_gather_len_19561() -> None: + N = 4 + df = pl.DataFrame({"foo": ["baz"] * N, "bar": range(N)}) + idxs = pl.int_range(1, N).repeat_by(pl.int_range(1, N)).flatten() + gather = pl.col.bar.gather(idxs).alias("gather") + + assert df.group_by("foo").agg(gather.len()).to_dict(as_series=False) == { + "foo": ["baz"], + "gather": [6], + }