Skip to content
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

Merged
merged 19 commits into from
Apr 27, 2020
Merged

BUG: Fix Categorical use_inf_as_na bug #33629

merged 19 commits into from
Apr 27, 2020

Conversation

dsaxton
Copy link
Member

@dsaxton dsaxton commented Apr 18, 2020

@dsaxton dsaxton added Bug Categorical Categorical Data Type Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate NA - MaskedArrays Related to pd.NA and nullable extension arrays labels Apr 18, 2020
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

Looks reasonable @TomAugspurger

@@ -231,7 +231,11 @@ def _isna_ndarraylike(obj):


def _isna_ndarraylike_old(obj):
values = getattr(obj, "_values", obj)
if not isinstance(obj, np.ndarray):
Copy link
Contributor

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?

Copy link
Member Author

@dsaxton dsaxton Apr 19, 2020

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

Copy link
Member

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.

Copy link
Member Author

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])

Copy link
Member

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?

Copy link
Member Author

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)

Copy link
Contributor

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 Show resolved Hide resolved
@@ -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
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to #33656

Copy link
Member Author

@dsaxton dsaxton Apr 19, 2020

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.

Copy link
Member

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.

@dsaxton dsaxton changed the title BUG: Fix use_inf_as_na bugs BUG: Fix Categorical use_inf_as_na bug Apr 19, 2020
@dsaxton dsaxton removed the NA - MaskedArrays Related to pd.NA and nullable extension arrays label Apr 19, 2020
@@ -231,7 +231,11 @@ def _isna_ndarraylike(obj):


def _isna_ndarraylike_old(obj):
values = getattr(obj, "_values", obj)
if not isinstance(obj, np.ndarray):
Copy link
Contributor

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.

@@ -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

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!

@@ -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.

can you make the similar code paths in isna_ndarraylike an here

Copy link
Contributor

@jreback jreback left a 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

@@ -207,40 +207,25 @@ def _use_inf_as_na(key):
globals()["_isna"] = _isna_new


def _isna_ndarraylike(obj):
def _isna_ndarraylike(obj, old=False):
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 add a doc-string here and type old

@jreback jreback added this to the 1.1 milestone Apr 26, 2020
@jreback jreback merged commit 91150d9 into pandas-dev:master Apr 27, 2020
@jreback
Copy link
Contributor

jreback commented Apr 27, 2020

thanks @dsaxton

if you can push the followon when you can to consolidate code here would be great

@dsaxton dsaxton deleted the use-inf-as-na branch April 27, 2020 21:40
@simonjayhawkins
Copy link
Member

xref #33300 to track.

simonjayhawkins pushed a commit to simonjayhawkins/pandas that referenced this pull request May 5, 2020
simonjayhawkins added a commit that referenced this pull request May 5, 2020
…a bug) (#34001)

Co-authored-by: Daniel Saxton <2658661+dsaxton@users.noreply.github.com>
rhshadrach pushed a commit to rhshadrach/pandas that referenced this pull request May 10, 2020
@simonjayhawkins simonjayhawkins modified the milestones: 1.1, 1.0.4 May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: pandas 1.0 dropna error with categorical data if pd.options.mode.use_inf_as_na = True
6 participants