From f992a7afb2783f9e330502a540a9f0bc73e56219 Mon Sep 17 00:00:00 2001 From: Weijie Guo Date: Mon, 15 Apr 2024 19:31:47 +0800 Subject: [PATCH] fix: `group_by` multiple null columns produce phantom row (#15659) --- crates/polars-core/src/frame/group_by/mod.rs | 7 ++++++- py-polars/tests/unit/operations/test_group_by.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/polars-core/src/frame/group_by/mod.rs b/crates/polars-core/src/frame/group_by/mod.rs index f7218e1434d7..438c3c24fd74 100644 --- a/crates/polars-core/src/frame/group_by/mod.rs +++ b/crates/polars-core/src/frame/group_by/mod.rs @@ -76,8 +76,13 @@ impl DataFrame { .cloned() .collect::>(); if by.is_empty() { + let groups = if self.height() == 0 { + vec![] + } else { + vec![[0, self.height() as IdxSize]] + }; Ok(GroupsProxy::Slice { - groups: vec![[0, self.height() as IdxSize]], + groups, rolling: false, }) } else { diff --git a/py-polars/tests/unit/operations/test_group_by.py b/py-polars/tests/unit/operations/test_group_by.py index e432d1e8b9bd..82a24afaaef4 100644 --- a/py-polars/tests/unit/operations/test_group_by.py +++ b/py-polars/tests/unit/operations/test_group_by.py @@ -985,3 +985,8 @@ def test_aggregated_scalar_elementwise_15602() -> None: ) expected = pl.DataFrame({"group": [1, 2], "foo": [[True, True], [True]]}) assert_frame_equal(out, expected) + + +def test_group_by_multiple_null_cols_15623() -> None: + df = pl.DataFrame(schema={"a": pl.Null, "b": pl.Null}).group_by(pl.all()).len() + assert df.is_empty()