@@ -32,6 +32,14 @@ def f(a):
3232 return a
3333
3434 index = MultiIndex .from_product (map (f , args ), names = names )
35+ if isinstance (fill_value , dict ):
36+ # fill_value is a dict mapping column names to fill values
37+ # -> reindex column by column (reindex itself does not support this)
38+ res = {}
39+ for col in result .columns :
40+ res [col ] = result [col ].reindex (index , fill_value = fill_value [col ])
41+ return DataFrame (res , index = index ).sort_index ()
42+
3543 return result .reindex (index , fill_value = fill_value ).sort_index ()
3644
3745
@@ -317,18 +325,14 @@ def test_apply(ordered):
317325 tm .assert_series_equal (result , expected )
318326
319327
320- def test_observed (request , using_infer_string , observed ):
328+ def test_observed (observed , using_infer_string ):
321329 # multiple groupers, don't re-expand the output space
322330 # of the grouper
323331 # gh-14942 (implement)
324332 # gh-10132 (back-compat)
325333 # gh-8138 (back-compat)
326334 # gh-8869
327335
328- if using_infer_string and not observed :
329- # TODO(infer_string) this fails with filling the string column with 0
330- request .applymarker (pytest .mark .xfail (reason = "TODO(infer_string)" ))
331-
332336 cat1 = Categorical (["a" , "a" , "b" , "b" ], categories = ["a" , "b" , "z" ], ordered = True )
333337 cat2 = Categorical (["c" , "d" , "c" , "d" ], categories = ["c" , "d" , "y" ], ordered = True )
334338 df = DataFrame ({"A" : cat1 , "B" : cat2 , "values" : [1 , 2 , 3 , 4 ]})
@@ -356,7 +360,10 @@ def test_observed(request, using_infer_string, observed):
356360 result = gb .sum ()
357361 if not observed :
358362 expected = cartesian_product_for_groupers (
359- expected , [cat1 , cat2 ], list ("AB" ), fill_value = 0
363+ expected ,
364+ [cat1 , cat2 ],
365+ list ("AB" ),
366+ fill_value = {"values" : 0 , "C" : "" } if using_infer_string else 0 ,
360367 )
361368
362369 tm .assert_frame_equal (result , expected )
0 commit comments