Skip to content
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

Fix groupby.get_group with length-1 tuple with list-like grouper #17216

Merged
merged 3 commits into from
Oct 31, 2024
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
5 changes: 5 additions & 0 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ def get_group(self, name, obj=None):
"instead of ``gb.get_group(name, obj=df)``.",
FutureWarning,
)
if is_list_like(self._by):
if isinstance(name, tuple) and len(name) == 1:
name = name[0]
else:
raise KeyError(name)
return obj.iloc[self.indices[name]]

@_performance_tracking
Expand Down
16 changes: 16 additions & 0 deletions python/cudf/cudf/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4059,3 +4059,19 @@ def test_ndim():
pgb = pser.groupby([0, 0, 1])
ggb = gser.groupby(cudf.Series([0, 0, 1]))
assert pgb.ndim == ggb.ndim


@pytest.mark.skipif(
not PANDAS_GE_220, reason="pandas behavior applicable in >=2.2"
)
def test_get_group_list_like():
df = cudf.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
result = df.groupby(["a"]).get_group((1,))
expected = df.to_pandas().groupby(["a"]).get_group((1,))
assert_eq(result, expected)

with pytest.raises(KeyError):
df.groupby(["a"]).get_group((1, 2))

with pytest.raises(KeyError):
df.groupby(["a"]).get_group([1])
Loading