Skip to content

Commit

Permalink
Fix: DataFrameGroupBy.get_group was raising with length>1 tuples (#17653
Browse files Browse the repository at this point in the history
)

#17216 added similar logic to what's in pandas https://github.com/pandas-dev/pandas/blob/602ae10f3d0d599ebbdd151e8a09f0baf20b4637/pandas/core/groupby/groupby.py#L787-L794, but missed one crucial ingredient: checking that the length of the keys is `1` before raising

Authors:
  - Marco Edward Gorelli (https://github.com/MarcoGorelli)
  - Bradley Dice (https://github.com/bdice)

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #17653
  • Loading branch information
MarcoGorelli authored Dec 23, 2024
1 parent f7f7e33 commit 45b40c5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def get_group(self, name, obj=None):
"instead of ``gb.get_group(name, obj=df)``.",
FutureWarning,
)
if is_list_like(self._by):
if is_list_like(self._by) and len(self._by) == 1:
if isinstance(name, tuple) and len(name) == 1:
name = name[0]
else:
Expand Down
7 changes: 7 additions & 0 deletions python/cudf/cudf/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4076,6 +4076,13 @@ def test_get_group_list_like():
df.groupby(["a"]).get_group([1])


def test_get_group_list_like_len_2():
df = cudf.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [3, 2, 1]})
result = df.groupby(["a", "b"]).get_group((1, 4))
expected = df.to_pandas().groupby(["a", "b"]).get_group((1, 4))
assert_eq(result, expected)


def test_size_as_index_false():
df = pd.DataFrame({"a": [1, 2, 1], "b": [1, 2, 3]}, columns=["a", "b"])
expected = df.groupby("a", as_index=False).size()
Expand Down

0 comments on commit 45b40c5

Please sign in to comment.