Skip to content

Commit 16adf44

Browse files
committed
BUG: Allow a categorical series to accept dict or Series in fillna (pandas-dev#17033)
1 parent 488db6f commit 16adf44

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

pandas/core/categorical.py

+17
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,23 @@ def fillna(self, value=None, method=None, limit=None):
16891689

16901690
else:
16911691

1692+
# print(value) # is value a scalar for df but series for series and this is why isna is causing problems?
1693+
# print(values)
1694+
1695+
if isinstance(value, ABCSeries):
1696+
if not value[~value.isin(self.categories)].isnull().all():
1697+
raise ValueError("fill value must be in categories")
1698+
# if (not value.isin(self.categories).all() and
1699+
# not value[~value.isin(self.categories)].isnull().all()):
1700+
# raise ValueError("fill value must be in categories!")
1701+
# self.categories.get_loc(value[value.notna()])
1702+
# value[value.isna()] = np.array(list(self.categories[[1, 0]]))
1703+
mask = values == -1
1704+
# values[mask] = [self.categories.get_loc(x) for x in value[value.notna()]]
1705+
values[mask] = _get_codes_for_values(value[value.notna()], self.categories)
1706+
return self._constructor(values, categories=self.categories,
1707+
ordered=self.ordered, fastpath=True)
1708+
16921709
if not isna(value) and value not in self.categories:
16931710
raise ValueError("fill value must be in categories")
16941711

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from pandas.core.common import (_count_not_none,
3333
_maybe_box_datetimelike, _values_from_object,
3434
AbstractMethodError, SettingWithCopyError,
35-
SettingWithCopyWarning)
35+
SettingWithCopyWarning, is_categorical_dtype)
3636

3737
from pandas.core.base import PandasObject, SelectionMixin
3838
from pandas.core.index import (Index, MultiIndex, _ensure_index,

pandas/tests/test_categorical.py

+12
Original file line numberDiff line numberDiff line change
@@ -4603,6 +4603,18 @@ def f():
46034603
df = pd.DataFrame({'a': pd.Categorical(idx)})
46044604
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)
46054605

4606+
s = pd.Series(pd.Categorical(['a', np.nan, 'b', np.nan], categories=['a', 'b']))
4607+
s1_exp = pd.Series(pd.Categorical(['a', 'a', 'b', 'a']))
4608+
tm.assert_series_equal(s.fillna('a'), s1_exp)
4609+
4610+
s = pd.Series(pd.Categorical(['a', np.nan, 'b', np.nan], categories=['a', 'b']))
4611+
s2_exp = pd.Series(pd.Categorical(['a', 'a', 'b', 'b']))
4612+
tm.assert_series_equal(s.fillna({1: 'a', 3: 'b'}), s2_exp)
4613+
4614+
s = pd.Series(pd.Categorical(['a', np.nan, 'b', np.nan], categories=['a', 'b']))
4615+
s3_exp = pd.Series(pd.Categorical(['a', 'a', 'b', 'a']))
4616+
tm.assert_series_equal(s.fillna(pd.Series('a')), s3_exp)
4617+
46064618
def test_astype_to_other(self):
46074619

46084620
s = self.cat['value_group']

0 commit comments

Comments
 (0)