Skip to content

Commit 724ad29

Browse files
jasonmokkJason Mok
and
Jason Mok
authored
TST: Add test for regression in groupby with tuples yielding MultiIndex (#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]>
1 parent e0d3061 commit 724ad29

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pandas/tests/groupby/test_grouping.py

+29
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,35 @@ def test_groupby_multiindex_level_empty(self):
822822
)
823823
tm.assert_frame_equal(result, expected)
824824

825+
def test_groupby_tuple_keys_handle_multiindex(self):
826+
# https://github.com/pandas-dev/pandas/issues/21340
827+
df = DataFrame(
828+
{
829+
"num1": [0, 8, 9, 4, 3, 3, 5, 9, 3, 6],
830+
"num2": [3, 8, 6, 4, 9, 2, 1, 7, 0, 9],
831+
"num3": [6, 5, 7, 8, 5, 1, 1, 10, 7, 8],
832+
"category_tuple": [
833+
(0, 1),
834+
(0, 1),
835+
(0, 1),
836+
(0, 4),
837+
(2, 3),
838+
(2, 3),
839+
(2, 3),
840+
(2, 3),
841+
(5,),
842+
(6,),
843+
],
844+
"category_string": list("aaabbbbcde"),
845+
}
846+
)
847+
expected = df.sort_values(by=["category_tuple", "num1"])
848+
result = df.groupby("category_tuple").apply(
849+
lambda x: x.sort_values(by="num1"), include_groups=False
850+
)
851+
expected = expected[result.columns]
852+
tm.assert_frame_equal(result.reset_index(drop=True), expected)
853+
825854

826855
# get_group
827856
# --------------------------------

0 commit comments

Comments
 (0)