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

TST: Add test for regression in groupby with tuples yielding MultiIndex #58630

Merged
merged 7 commits into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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