Skip to content

Commit 88f0108

Browse files
Jason MokJason Mok
Jason Mok
authored and
Jason Mok
committed
Implement test for GH pandas-dev#21340
1 parent 882b228 commit 88f0108

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pandas/tests/groupby/test_grouping.py

+33
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,39 @@ def test_groupby_multiindex_level_empty(self):
823823
tm.assert_frame_equal(result, expected)
824824

825825

826+
def test_groupby_tuple_keys_handle_multiindex():
827+
# https://github.com/pandas-dev/pandas/issues/21340
828+
df = DataFrame(
829+
{
830+
"num1": [0, 8, 9, 4, 3, 3, 5, 9, 3, 6],
831+
"num2": [3, 8, 6, 4, 9, 2, 1, 7, 0, 9],
832+
"num3": [6, 5, 7, 8, 5, 1, 1, 10, 7, 8],
833+
"category_tuple": [
834+
(0, 1),
835+
(0, 1),
836+
(0, 1),
837+
(0, 4),
838+
(2, 3),
839+
(2, 3),
840+
(2, 3),
841+
(2, 3),
842+
(5,),
843+
(6,),
844+
],
845+
"category_string": list("aaabbbbcde"),
846+
}
847+
)
848+
df = df[["category_tuple", "category_string", "num1", "num2", "num3"]]
849+
expected = df.sort_values(by=["category_tuple", "num1"])
850+
851+
msg = "DataFrameGroupBy.apply operated on the grouping columns"
852+
with tm.assert_produces_warning(DeprecationWarning, match=msg):
853+
result = df.groupby("category_tuple").apply(lambda x: x.sort_values(by="num1"))
854+
expected = expected[result.columns]
855+
856+
tm.assert_frame_equal(result.reset_index(drop=True), expected)
857+
858+
826859
# get_group
827860
# --------------------------------
828861

0 commit comments

Comments
 (0)