@@ -32,6 +32,14 @@ def f(a):
32
32
return a
33
33
34
34
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
+
35
43
return result .reindex (index , fill_value = fill_value ).sort_index ()
36
44
37
45
@@ -317,18 +325,14 @@ def test_apply(ordered):
317
325
tm .assert_series_equal (result , expected )
318
326
319
327
320
- def test_observed (request , using_infer_string , observed ):
328
+ def test_observed (observed , using_infer_string ):
321
329
# multiple groupers, don't re-expand the output space
322
330
# of the grouper
323
331
# gh-14942 (implement)
324
332
# gh-10132 (back-compat)
325
333
# gh-8138 (back-compat)
326
334
# gh-8869
327
335
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
-
332
336
cat1 = Categorical (["a" , "a" , "b" , "b" ], categories = ["a" , "b" , "z" ], ordered = True )
333
337
cat2 = Categorical (["c" , "d" , "c" , "d" ], categories = ["c" , "d" , "y" ], ordered = True )
334
338
df = DataFrame ({"A" : cat1 , "B" : cat2 , "values" : [1 , 2 , 3 , 4 ]})
@@ -356,7 +360,10 @@ def test_observed(request, using_infer_string, observed):
356
360
result = gb .sum ()
357
361
if not observed :
358
362
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 ,
360
367
)
361
368
362
369
tm .assert_frame_equal (result , expected )
0 commit comments