Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
BUG: Fix Categorical use_inf_as_na bug #33629
Changes from 3 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
There are no files selected for viewing
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):
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.
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:
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 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 failingThere 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!