-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 6 commits
8985831
55ed2cf
9582927
365df7e
1d05145
a1b12ba
c7c03b8
dbf9830
60c7625
6b413b4
ddff3d2
cbfdd6a
81c310f
1838a33
11d50a9
afa9448
c7b65d5
a95f38c
fe1648f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,7 +231,11 @@ def _isna_ndarraylike(obj): | |
|
||
|
||
def _isna_ndarraylike_old(obj): | ||
values = getattr(obj, "_values", obj) | ||
if not isinstance(obj, np.ndarray): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the the same construct we use in isna_arraylike? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In _isna_arraylike it looks like we use getattr, but then check if we have an ExtensionArray and call the isna method if so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree the code should use the same structure as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would you recommend using a similar pattern here? We can't rely on isna to catch infinity (it only works if you construct the array after setting the option): [ins] In [1]: cat = pd.Categorical([1, 2, np.inf])
[ins] In [2]: pd.options.mode.use_inf_as_na = True
[ins] In [3]: cat.isna()
Out[3]: array([False, False, False])
[ins] In [4]: cat = pd.Categorical([1, 2, np.inf])
[ins] In [5]: cat.isna()
Out[5]: array([False, False, True]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so is there also a bug in Categorical.isna? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose you could call this behavior a bug, but I'm not sure if it's a problem with Categorical.isna (it's simply checking Categorical._codes, which makes total sense and shouldn't need to change in my opinion). IMHO the issue is more with trying to have the behavior of these constructors / methods respect hidden global variables instead of being well-defined in isolation (I could even see the merit of deprecating options like this rather than needing to patch every place where NA checking might happen) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to make this code substatially the same, difference only be the inf change. |
||
values = obj.to_numpy() | ||
else: | ||
values = obj | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
dtype = values.dtype | ||
|
||
if is_string_dtype(dtype): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, being a bit confused in the comment above :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dsaxton so could you additionally test it for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this related to the OP in #33594?. I think the NA checking for string dtype #33594 (comment) maybe should be a separate issue/PR. We may want to backport the fix relating to the change in return type of Categorical.ravel from numpy array to Categorical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unrelated, I found this other bug while debugging the original issue and decided to fix it here. I'll go ahead and separate it out for easier bookkeeping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to #33656
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, they are kind of related in that a fix needs to be made to missing.py as well. I'll wait to see how that ends up here before making the associated change in the other PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one is a bug in
_isna_ndarraylike_old
and the other is a bug inisnaobj_old
. They are related but I think should be treated indepedently.for the NA issue (bug in
isnaobj_old
once bug in_isna_ndarraylike_old
is fixed) we may want to add more tests for other extension arrays or add a test/parameterise an existing test in the base extension array tests.