@@ -4601,6 +4601,45 @@ def f():
4601
4601
df = pd .DataFrame ({'a' : pd .Categorical (idx )})
4602
4602
tm .assert_frame_equal (df .fillna (value = pd .NaT ), df )
4603
4603
4604
+ @pytest .mark .parametrize ('fill_value expected_output' , [
4605
+ ('a' , ['a' , 'a' , 'b' , 'a' , 'a' ]),
4606
+ ({1 : 'a' , 3 : 'b' , 4 : 'b' }, ['a' , 'a' , 'b' , 'b' , 'b' ]),
4607
+ ({1 : 'a' }, ['a' , 'a' , 'b' , np .nan , np .nan ]),
4608
+ ({1 : 'a' , 3 : 'b' }, ['a' , 'a' , 'b' , 'b' , np .nan ]),
4609
+ (pd .Series ('a' ), ['a' , np .nan , 'b' , np .nan , np .nan ]),
4610
+ (pd .Series ('a' , index = [1 ]), ['a' , 'a' , 'b' , np .nan , np .nan ]),
4611
+ (pd .Series ({1 : 'a' , 3 : 'b' }), ['a' , 'a' , 'b' , 'b' , np .nan ]),
4612
+ (pd .Series (['a' , 'b' ], index = [3 , 4 ]))
4613
+ ])
4614
+ def fillna_series_categorical (self , fill_value , expected_output ):
4615
+ # GH 17033
4616
+ # Test fillna for a Categorical series
4617
+ data = ['a' , np .nan , 'b' , np .nan , np .nan ]
4618
+ s = pd .Series (pd .Categorical (data , categories = ['a' , 'b' ]))
4619
+ exp = pd .Series (pd .Categorical (expected_output , categories = ['a' , 'b' ]))
4620
+ tm .assert_series_equal (s .fillna (fill_value ), exp )
4621
+
4622
+ def fillna_series_categorical_errormsg (self ):
4623
+ data = ['a' , np .nan , 'b' , np .nan , np .nan ]
4624
+ s = pd .Series (pd .Categorical (data , categories = ['a' , 'b' ]))
4625
+
4626
+ with tm .assert_raises_regex (ValueError ,
4627
+ "fill value must be in categories" ):
4628
+ s .fillna ('d' )
4629
+
4630
+ with tm .assert_raises_regex (ValueError ,
4631
+ "fill value must be in categories" ):
4632
+ s .fillna (pd .Series ('d' ))
4633
+
4634
+ with tm .assert_raises_regex (ValueError ,
4635
+ "fill value must be in categories" ):
4636
+ s .fillna ({1 : 'd' , 3 : 'a' })
4637
+
4638
+ with tm .assert_raises_regex (TypeError ,
4639
+ '"value" parameter must be a scalar or '
4640
+ 'dict but you passed a "list"' ):
4641
+ s .fillna (['a' , 'b' ])
4642
+
4604
4643
def test_astype_to_other (self ):
4605
4644
4606
4645
s = self .cat ['value_group' ]
0 commit comments