BUG: Accept dict or Series in fillna for categorical Series by reidy-p · Pull Request #18293 · pandas-dev/pandas (original) (raw)

Yeah, I was thinking about that but couldn't get it to work properly. The problem I was having was that if the user passes a scalar all the NaNs will be filled with that single scalar. But if the user passes a Series (or a dict which is then converted to a Series in the function) the NaNs will be filled with different values according to the index values:

In [1]: data = ['a', np.nan, 'b', np.nan, np.nan]
In [2]: s = pd.Series(pd.Categorical(data, categories=['a', 'b']))

In [3]: s.fillna('a')
Out[3]: 
0    a
1    a
2    b
3    a
4    a
dtype: category
Categories (2, object): [a, b]

In [4]: s.fillna(pd.Series(['a', 'b', 'b'], index=[1, 3, 4])
Out[4]:
0    a
1    a
2    b
3    b
4    b
dtype: category
Categories (2, object): [a, b]

And I was finding it difficult to deal with both cases with the same code. But I can keep thinking about it.