@@ -241,6 +241,28 @@ def test_level_get_group(observed):
241
241
tm .assert_frame_equal (result , expected )
242
242
243
243
244
+ def test_sorting_with_different_categoricals ():
245
+ # GH 24271
246
+ df = DataFrame (
247
+ {
248
+ "group" : ["A" ] * 6 + ["B" ] * 6 ,
249
+ "dose" : ["high" , "med" , "low" ] * 4 ,
250
+ "outcomes" : np .arange (12.0 ),
251
+ }
252
+ )
253
+
254
+ df .dose = Categorical (df .dose , categories = ["low" , "med" , "high" ], ordered = True )
255
+
256
+ result = df .groupby ("group" )["dose" ].value_counts ()
257
+ result = result .sort_index (level = 0 , sort_remaining = True )
258
+ index = ["low" , "med" , "high" , "low" , "med" , "high" ]
259
+ index = Categorical (index , categories = ["low" , "med" , "high" ], ordered = True )
260
+ index = [["A" , "A" , "A" , "B" , "B" , "B" ], CategoricalIndex (index )]
261
+ index = MultiIndex .from_arrays (index , names = ["group" , None ])
262
+ expected = Series ([2 ] * 6 , index = index , name = "dose" )
263
+ tm .assert_series_equal (result , expected )
264
+
265
+
244
266
@pytest .mark .parametrize ("ordered" , [True , False ])
245
267
def test_apply (ordered ):
246
268
# GH 10138
0 commit comments