Skip to content

BUG: Fix Categorical use_inf_as_na bug #33629

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

Merged
merged 19 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ Categorical
- :meth:`Categorical.fillna` now accepts :class:`Categorical` ``other`` argument (:issue:`32420`)
- Bug where :meth:`Categorical.replace` would replace with ``NaN`` whenever the new value and replacement value were equal (:issue:`33288`)
- Bug where an ordered :class:`Categorical` containing only ``NaN`` values would raise rather than returning ``NaN`` when taking the minimum or maximum (:issue:`33450`)
- Bug where :meth:`Series.isna` and :meth:`DataFrame.isna` would raise for categorical dtype when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`33594`)

Datetimelike
^^^^^^^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,10 @@ def _isna_ndarraylike_old(obj):
values = getattr(obj, "_values", obj)
dtype = values.dtype

if is_string_dtype(dtype):
if is_extension_array_dtype(dtype):
result = values.isna() | (values == -np.inf) | (values == np.inf)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this always treating inf as NA?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think / hope that's the intention when this gets called (e.g., at line 244 we have result = ~np.isfinite(values))

Copy link
Contributor

Choose a reason for hiding this comment

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

can you make the similar code paths in isna_ndarraylike an here

Copy link
Member Author

@dsaxton dsaxton Apr 21, 2020

Choose a reason for hiding this comment

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

The functions are almost the same now, unless there are objections (can revert if so) I'm going to try parametrizing _isna_ndarraylike over old / new and just deleting this function

elif is_string_dtype(dtype):
result = _isna_string_dtype(values, dtype, old=True)

elif needs_i8_conversion(dtype):
# this is the NaT pattern
result = values.view("i8") == iNaT
Expand Down
26 changes: 25 additions & 1 deletion pandas/tests/arrays/categorical/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from pandas.core.dtypes.dtypes import CategoricalDtype

from pandas import Categorical, Index, Series, isna
import pandas as pd
from pandas import Categorical, DataFrame, Index, Series, isna
import pandas._testing as tm


Expand Down Expand Up @@ -97,3 +98,26 @@ def test_fillna_array(self):
expected = Categorical(["A", "B", "C", "B", "A"], dtype=cat.dtype)
tm.assert_categorical_equal(result, expected)
assert isna(cat[-1]) # didnt modify original inplace

@pytest.mark.parametrize(
"values, expected",
[
([1, 2, 3], np.array([False, False, False])),
([1, 2, np.nan], np.array([False, False, True])),
([1, 2, np.inf], np.array([False, False, True])),
],
)
def test_use_inf_as_na(self, values, expected):
# https://github.com/pandas-dev/pandas/issues/33594
with pd.option_context("mode.use_inf_as_na", True):
cat = Categorical(values)
Copy link
Member

Choose a reason for hiding this comment

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

Should we also test this with putting the Categorical creation outside of the option context?

Copy link
Member Author

@dsaxton dsaxton Apr 20, 2020

Choose a reason for hiding this comment

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

It turns out that actually fails interestingly enough: #33629 (comment) (if you set the option after constructing the object it loses its effect). What's even more strange is the value displays as NaN:

[ins] In [3]: arr = pd.Categorical([1, 2, np.inf])                                                                                                                                                           

[ins] In [4]: pd.options.mode.use_inf_as_na = True                                                                                                                                                           

[ins] In [5]: arr                                                                                                                                                                                            
Out[5]: 
[1.0, 2.0, NaN]
Categories (3, float64): [1.0, 2.0, NaN]

[ins] In [6]: arr.isna()                                                                                                                                                                                     
Out[6]: array([False, False, False])

Copy link
Member

Choose a reason for hiding this comment

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

What's even more strange is the value displays as NaN:

I think that is somewhat "expected", given the discussion above in #33629 (comment) (not that I like that behaviour though).

But regardless of how the categorical is created (with or without the option), I find it very strange that isna is then failing

Copy link
Member

@jorisvandenbossche jorisvandenbossche Apr 20, 2020

Choose a reason for hiding this comment

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

Hmm, being a bit confused in the comment above :)
The Categorical.isna is failing, of course, since it just looks at -1 in the codes, and when inf is part of the categories, it is not -1 in the codes. Hence it is failing to detect it.

Copy link
Member

Choose a reason for hiding this comment

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

@dsaxton so could you additionally test it for pd.isna ? In constract to Categorical.isna(), pd.isna should work even if the Categorical is created before the context, I think?

Copy link
Member Author

Choose a reason for hiding this comment

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

@jorisvandenbossche Added some tests, let me know if this is roughly what you had in mind

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that looks good!

result = cat.isna()
tm.assert_numpy_array_equal(result, expected)

result = Series(cat).isna()
expected = Series(expected)
tm.assert_series_equal(result, expected)

result = DataFrame(cat).isna()
expected = DataFrame(expected)
tm.assert_frame_equal(result, expected)