@@ -823,6 +823,39 @@ def test_groupby_multiindex_level_empty(self):
823
823
tm .assert_frame_equal (result , expected )
824
824
825
825
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
+
826
859
# get_group
827
860
# --------------------------------
828
861
0 commit comments