@@ -994,3 +994,30 @@ def test_groupby_get_by_index():
994
994
res = df .groupby ("A" ).agg ({"B" : lambda x : x .get (x .index [- 1 ])})
995
995
expected = pd .DataFrame (dict (A = ["S" , "W" ], B = [1.0 , 2.0 ])).set_index ("A" )
996
996
pd .testing .assert_frame_equal (res , expected )
997
+
998
+
999
+ def test_aggregate_categorical_with_isnan ():
1000
+ # GH 29837
1001
+ df = pd .DataFrame (
1002
+ {
1003
+ "A" : [1 , 1 , 1 , 1 ],
1004
+ "B" : [1 , 2 , 1 , 2 ],
1005
+ "numerical_col" : [0.1 , 0.2 , np .nan , 0.3 ],
1006
+ "object_col" : ["foo" , "bar" , "foo" , "fee" ],
1007
+ "categorical_col" : ["foo" , "bar" , "foo" , "fee" ],
1008
+ }
1009
+ )
1010
+
1011
+ df = df .astype ({"categorical_col" : "category" })
1012
+
1013
+ result = df .groupby (["A" , "B" ]).agg (lambda df : df .isna ().sum ())
1014
+ index = pd .MultiIndex .from_arrays ([[1 , 1 ], [1 , 2 ]], names = ("A" , "B" ))
1015
+ expected = pd .DataFrame (
1016
+ data = {
1017
+ "numerical_col" : [1.0 , 0.0 ],
1018
+ "object_col" : [0 , 0 ],
1019
+ "categorical_col" : [0 , 0 ],
1020
+ },
1021
+ index = index ,
1022
+ )
1023
+ tm .assert_frame_equal (result , expected )
0 commit comments