@@ -4569,6 +4569,45 @@ def f():
4569
4569
df = DataFrame ({'a' : Categorical (idx )})
4570
4570
tm .assert_frame_equal (df .fillna (value = NaT ), df )
4571
4571
4572
+ @pytest .mark .parametrize ('fill_value expected_output' , [
4573
+ ('a' , ['a' , 'a' , 'b' , 'a' , 'a' ]),
4574
+ ({1 : 'a' , 3 : 'b' , 4 : 'b' }, ['a' , 'a' , 'b' , 'b' , 'b' ]),
4575
+ ({1 : 'a' }, ['a' , 'a' , 'b' , np .nan , np .nan ]),
4576
+ ({1 : 'a' , 3 : 'b' }, ['a' , 'a' , 'b' , 'b' , np .nan ]),
4577
+ (pd .Series ('a' ), ['a' , np .nan , 'b' , np .nan , np .nan ]),
4578
+ (pd .Series ('a' , index = [1 ]), ['a' , 'a' , 'b' , np .nan , np .nan ]),
4579
+ (pd .Series ({1 : 'a' , 3 : 'b' }), ['a' , 'a' , 'b' , 'b' , np .nan ]),
4580
+ (pd .Series (['a' , 'b' ], index = [3 , 4 ]))
4581
+ ])
4582
+ def fillna_series_categorical (self , fill_value , expected_output ):
4583
+ # GH 17033
4584
+ # Test fillna for a Categorical series
4585
+ data = ['a' , np .nan , 'b' , np .nan , np .nan ]
4586
+ s = pd .Series (pd .Categorical (data , categories = ['a' , 'b' ]))
4587
+ exp = pd .Series (pd .Categorical (expected_output , categories = ['a' , 'b' ]))
4588
+ tm .assert_series_equal (s .fillna (fill_value ), exp )
4589
+
4590
+ def fillna_series_categorical_errormsg (self ):
4591
+ data = ['a' , np .nan , 'b' , np .nan , np .nan ]
4592
+ s = pd .Series (pd .Categorical (data , categories = ['a' , 'b' ]))
4593
+
4594
+ with tm .assert_raises_regex (ValueError ,
4595
+ "fill value must be in categories" ):
4596
+ s .fillna ('d' )
4597
+
4598
+ with tm .assert_raises_regex (ValueError ,
4599
+ "fill value must be in categories" ):
4600
+ s .fillna (pd .Series ('d' ))
4601
+
4602
+ with tm .assert_raises_regex (ValueError ,
4603
+ "fill value must be in categories" ):
4604
+ s .fillna ({1 : 'd' , 3 : 'a' })
4605
+
4606
+ with tm .assert_raises_regex (TypeError ,
4607
+ '"value" parameter must be a scalar or '
4608
+ 'dict but you passed a "list"' ):
4609
+ s .fillna (['a' , 'b' ])
4610
+
4572
4611
def test_astype_to_other (self ):
4573
4612
4574
4613
s = self .cat ['value_group' ]
0 commit comments