@@ -2596,6 +2596,28 @@ def get_stats(group):
2596
2596
result = self .df .groupby (cats ).D .apply (get_stats )
2597
2597
self .assertEqual (result .index .names [0 ], 'C' )
2598
2598
2599
+ def test_apply_categorical_data (self ):
2600
+ # GH 10138
2601
+ dense = Categorical (list ('abc' ))
2602
+ # 'b' is in the categories but not in the list
2603
+ missing = Categorical (list ('aaa' ), categories = ['a' , 'b' ])
2604
+ values = np .arange (len (dense ))
2605
+ df = DataFrame ({'missing' : missing ,
2606
+ 'dense' : dense ,
2607
+ 'values' : values })
2608
+ grouped = df .groupby (['missing' , 'dense' ])
2609
+
2610
+ # missing category 'b' should still exist in the output index
2611
+ idx = MultiIndex .from_product ([['a' , 'b' ], ['a' , 'b' , 'c' ]],
2612
+ names = ['missing' , 'dense' ])
2613
+ expected = DataFrame ([0 , 1 , 2 , np .nan , np .nan , np .nan ],
2614
+ index = idx ,
2615
+ columns = ['values' ])
2616
+
2617
+ assert_frame_equal (grouped .apply (lambda x : np .mean (x )), expected )
2618
+ assert_frame_equal (grouped .mean (), expected )
2619
+ assert_frame_equal (grouped .agg (np .mean ), expected )
2620
+
2599
2621
def test_apply_corner_cases (self ):
2600
2622
# #535, can't use sliding iterator
2601
2623
0 commit comments