-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
BUG: Fix Categorical use_inf_as_na bug #33629
Conversation
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.
Looks reasonable @TomAugspurger
pandas/core/dtypes/missing.py
Outdated
@@ -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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the code should use the same structure as _isna_ndarraylike
, refactoring out the common code maybe advantageous to avoid divergence occurring again.
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.
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 comment
The 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 comment
The 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 comment
The 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.
pandas/_libs/missing.pyx
Outdated
@@ -160,7 +161,7 @@ def isnaobj_old(arr: ndarray) -> ndarray: | |||
result = np.zeros(n, dtype=np.uint8) | |||
for i in range(n): | |||
val = arr[i] | |||
result[i] = val is NaT or _check_none_nan_inf_neginf(val) | |||
result[i] = checknull(val) or val == INF or val == NEGINF |
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 in isnaobj_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.
pandas/core/dtypes/missing.py
Outdated
@@ -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 comment
The 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.
pandas/core/dtypes/missing.py
Outdated
@@ -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) |
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 always treating inf as NA?
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.
Yes, I think / hope that's the intention when this gets called (e.g., at line 244 we have result = ~np.isfinite(values)
)
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.
can you make the similar code paths in isna_ndarraylike an here
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.
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
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 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?
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 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 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
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.
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.
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.
@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?
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.
@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 comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that looks good!
pandas/core/dtypes/missing.py
Outdated
@@ -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) |
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.
can you make the similar code paths in isna_ndarraylike an here
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.
small change, otherwise lgtm. ping on green.
in a followon, I'd like to remove entirely the _isna_old and push just have a single parameterized one as you can just parameterize _isna_new / _old
pandas/core/dtypes/missing.py
Outdated
@@ -207,40 +207,25 @@ def _use_inf_as_na(key): | |||
globals()["_isna"] = _isna_new | |||
|
|||
|
|||
def _isna_ndarraylike(obj): | |||
def _isna_ndarraylike(obj, old=False): |
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.
can you add a doc-string here and type old
thanks @dsaxton if you can push the followon when you can to consolidate code here would be great |
xref #33300 to track. |
…e_inf_as_na bug)
…a bug) (#34001) Co-authored-by: Daniel Saxton <2658661+dsaxton@users.noreply.github.com>
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff