@@ -3265,6 +3265,30 @@ def test_no_dummy_key_names(self):
3265
3265
self .df ['B' ].values ]).sum ()
3266
3266
self .assertEqual (result .index .names , (None , None ))
3267
3267
3268
+ def test_groupby_sort_categorical (self ):
3269
+ # dataframe groupby sort was being ignored # GH 8868
3270
+ df = DataFrame ([['(7.5, 10]' , 10 , 10 ],
3271
+ ['(7.5, 10]' , 8 , 20 ],
3272
+ ['(2.5, 5]' , 5 , 30 ],
3273
+ ['(5, 7.5]' , 6 , 40 ],
3274
+ ['(2.5, 5]' , 4 , 50 ],
3275
+ ['(0, 2.5]' , 1 , 60 ],
3276
+ ['(5, 7.5]' , 7 , 70 ]], columns = ['range' , 'foo' , 'bar' ])
3277
+ df ['range' ] = Categorical (df ['range' ])
3278
+ index = Index (['(0, 2.5]' , '(2.5, 5]' , '(5, 7.5]' , '(7.5, 10]' ], dtype = 'object' )
3279
+ index .name = 'range'
3280
+ result_sort = DataFrame ([[1 , 60 ], [5 , 30 ], [6 , 40 ], [10 , 10 ]], columns = ['foo' , 'bar' ])
3281
+ result_sort .index = index
3282
+ index = Index (['(7.5, 10]' , '(2.5, 5]' , '(5, 7.5]' , '(0, 2.5]' ], dtype = 'object' )
3283
+ index .name = 'range'
3284
+ result_nosort = DataFrame ([[10 , 10 ], [5 , 30 ], [6 , 40 ], [1 , 60 ]], index = index , columns = ['foo' , 'bar' ])
3285
+ result_nosort .index = index
3286
+
3287
+ col = 'range'
3288
+ assert_frame_equal (result_sort , df .groupby (col , sort = True ).first ())
3289
+ assert_frame_equal (result_nosort , df .groupby (col , sort = False ).first ())
3290
+
3291
+
3268
3292
def test_groupby_sort_multiindex_series (self ):
3269
3293
# series multiindex groupby sort argument was not being passed through _compress_group_index
3270
3294
# GH 9444
0 commit comments