@@ -914,3 +914,32 @@ def test_sort_index_na_position(self):
914
914
expected = df .copy ()
915
915
result = df .sort_index (level = [0 , 1 ], na_position = "last" )
916
916
tm .assert_frame_equal (result , expected )
917
+
918
+ @pytest .mark .parametrize ("ascending" , [True , False ])
919
+ def test_sort_index_multiindex_sort_remaining (self , ascending ):
920
+ # GH #24247
921
+ df = DataFrame (
922
+ {"A" : [1 , 2 , 3 , 4 , 5 ], "B" : [10 , 20 , 30 , 40 , 50 ]},
923
+ index = MultiIndex .from_tuples (
924
+ [("a" , "x" ), ("a" , "y" ), ("b" , "x" ), ("b" , "y" ), ("c" , "x" )]
925
+ ),
926
+ )
927
+
928
+ result = df .sort_index (level = 1 , sort_remaining = False , ascending = ascending )
929
+
930
+ if ascending :
931
+ expected = DataFrame (
932
+ {"A" : [1 , 3 , 5 , 2 , 4 ], "B" : [10 , 30 , 50 , 20 , 40 ]},
933
+ index = MultiIndex .from_tuples (
934
+ [("a" , "x" ), ("b" , "x" ), ("c" , "x" ), ("a" , "y" ), ("b" , "y" )]
935
+ ),
936
+ )
937
+ else :
938
+ expected = DataFrame (
939
+ {"A" : [2 , 4 , 1 , 3 , 5 ], "B" : [20 , 40 , 10 , 30 , 50 ]},
940
+ index = MultiIndex .from_tuples (
941
+ [("a" , "y" ), ("b" , "y" ), ("a" , "x" ), ("b" , "x" ), ("c" , "x" )]
942
+ ),
943
+ )
944
+
945
+ tm .assert_frame_equal (result , expected )
0 commit comments