@@ -433,6 +433,35 @@ def test_observed_groups_with_nan(observed):
433433 tm .assert_dict_equal (result , expected )
434434
435435
436+ def test_observed_ops (agg_func ):
437+ cat = pd .Categorical (['a' , np .nan , np .nan ], categories = ['a' , 'b' , 'c' ])
438+ ser = pd .Series ([1. , 2. , 3. ])
439+ df = pd .DataFrame ({'cat' : cat , 'ser' : ser })
440+
441+ grp = df .groupby ('cat' , observed = False )['ser' ]
442+ func = getattr (grp , agg_func )
443+
444+ if agg_func == 'nth' : # Need an argument
445+ result = func (0 )
446+ else :
447+ result = func ()
448+
449+ if agg_func == 'sum' : # TODO: maybe a bug?
450+ expected_vals = [1. , 0. , 0. ]
451+ elif agg_func == 'prod' : # TODO: Definitely seems like a bug
452+ expected_vals = [1. , 1. , 1. ]
453+ elif agg_func == 'var' :
454+ expected_vals = [np .nan , np .nan , np .nan ]
455+ else :
456+ expected_vals = [1. , np .nan , np .nan ]
457+
458+ index = pd .Categorical (['a' , 'b' , 'c' ], categories = ['a' , 'b' , 'c' ])
459+ expected = pd .Series (expected_vals , index = index , name = 'ser' )
460+ expected .index .name = 'cat'
461+
462+ tm .assert_series_equal (result , expected )
463+
464+
436465def test_dataframe_categorical_with_nan (observed ):
437466 # GH 21151
438467 s1 = pd .Categorical ([np .nan , 'a' , np .nan , 'a' ],
0 commit comments