Skip to content

BUG: Dataframe.fillna with np.nan for dtype=category(GH 14021) #14051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ Bug Fixes
- Bug where ``pd.read_gbq()`` could throw ``ImportError: No module named discovery`` as a result of a naming conflict with another python package called apiclient (:issue:`13454`)
- Bug in ``Index.union`` returns an incorrect result with a named empty index (:issue:`13432`)
- Bugs in ``Index.difference`` and ``DataFrame.join`` raise in Python3 when using mixed-integer indexes (:issue:`13432`, :issue:`12814`)
- Bug in ``DataFrame(..., dtype='category').fillna(value=np.nan)`` raise ``KeyError`` (:issue:`14021`)
- Bug in ``.to_excel()`` when DataFrame contains a MultiIndex which contains a label with a NaN value (:issue:`13511`)
- Bug in invalid frequency offset string like "D1", "-2-3H" may not raise ``ValueError (:issue:`13930`)
- Bug in ``concat`` and ``groupby`` for hierarchical frames with ``RangeIndex`` levels (:issue:`13542`).
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,10 @@ def fillna(self, value=None, method=None, limit=None):
mask = values == -1
if mask.any():
values = values.copy()
values[mask] = self.categories.get_loc(value)
if isnull(value):
values[mask] = -1
else:
values[mask] = self.categories.get_loc(value)

return self._constructor(values, categories=self.categories,
ordered=self.ordered, fastpath=True)
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4100,6 +4100,31 @@ def f():
res = df.fillna("a")
tm.assert_frame_equal(res, df_exp)

# GH 14021
cat = Categorical([np.nan, 2, np.nan])
val = Categorical([np.nan, np.nan, np.nan])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a similar test that uses datetimelike and NaT (in fact ideally loop over DTI, TDI, PI)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback Fixed

df = DataFrame({"cats": cat, "vals": val})
res = df.fillna(df.median())
v_exp = [np.nan, np.nan, np.nan]
df_exp = pd.DataFrame({"cats": [2, 2, 2], "vals": v_exp},
dtype='category')
tm.assert_frame_equal(res, df_exp)

idx = pd.DatetimeIndex(['2011-01-01 09:00', '2016-01-01 23:45',
'2011-01-01 09:00', pd.NaT, pd.NaT])
df = DataFrame({'a': pd.Categorical(idx)})
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)

idx = pd.PeriodIndex(['2011-01', '2011-01', '2011-01',
pd.NaT, pd.NaT], freq='M')
df = DataFrame({'a': pd.Categorical(idx)})
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)

idx = pd.TimedeltaIndex(['1 days', '2 days',
'1 days', pd.NaT, pd.NaT])
df = pd.DataFrame({'a': pd.Categorical(idx)})
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)

def test_astype_to_other(self):

s = self.cat['value_group']
Expand Down