Skip to content

Commit

Permalink
TST: Add test for regression in groupby with tuples yielding MultiI…
Browse files Browse the repository at this point in the history
…ndex (#58630)

* Implement test for GH #21340

* minor fixup

* Lint contribution

* Make spacing consistent

* Lint

* Remove duplicate column construction

* Avoid DeprecationWarning by setting include_groups=False in apply

---------

Co-authored-by: Jason Mok <[email protected]>
  • Loading branch information
jasonmokk and Jason Mok authored May 8, 2024
1 parent e0d3061 commit 724ad29
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,35 @@ def test_groupby_multiindex_level_empty(self):
)
tm.assert_frame_equal(result, expected)

def test_groupby_tuple_keys_handle_multiindex(self):
# https://github.com/pandas-dev/pandas/issues/21340
df = DataFrame(
{
"num1": [0, 8, 9, 4, 3, 3, 5, 9, 3, 6],
"num2": [3, 8, 6, 4, 9, 2, 1, 7, 0, 9],
"num3": [6, 5, 7, 8, 5, 1, 1, 10, 7, 8],
"category_tuple": [
(0, 1),
(0, 1),
(0, 1),
(0, 4),
(2, 3),
(2, 3),
(2, 3),
(2, 3),
(5,),
(6,),
],
"category_string": list("aaabbbbcde"),
}
)
expected = df.sort_values(by=["category_tuple", "num1"])
result = df.groupby("category_tuple").apply(
lambda x: x.sort_values(by="num1"), include_groups=False
)
expected = expected[result.columns]
tm.assert_frame_equal(result.reset_index(drop=True), expected)


# get_group
# --------------------------------
Expand Down

0 comments on commit 724ad29

Please sign in to comment.