-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
BUG: Handle multiple percentiles for all-nan slices in nanpercentile #5981
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: Handle multiple percentiles for all-nan slices in nanpercentile #5981
Conversation
776e550
to
ade92c2
Compare
This needs tests to verify that the behavior is fixed. |
ade92c2
to
4fdc1a4
Compare
Turns out the behavior of nanpercentile with multiple percentiles was not replicating percentile. This has been fixed, and tests were added to ensure the behavior matches. This clears up the the all-nan slices case, which is also checked in the tests that were added. |
@dfreese it looks like there's also some odd behavior in the current release relating to the
Any idea if your changes here fix these this issue as well? |
if q.ndim == 0: | ||
return np.nan | ||
else: | ||
return np.nan * np.ones((len(q),)) |
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.
Just to make sure, but q
may only be 0-D or 1-D at this point?! Could you maybe add a test for the higher dimensional case.
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.
Or maybe we do just don't support the higher dim case for nanpercentile.
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 didn't consider that because it doesn't appear that percentile supports the higher dimensional case either. The following results in a ValueError being raised.
import numpy as np
data = np.eye(5, dtype=np.float64)
np.percentile(data, [[60, 80], [90, 100]], keepdims=True, axis=1)
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.
Right, thought we had added that, but must have been some other thing (maybe it was just how q is interpreted if 1-d).
@ssanderson, just checked and this would take care of that issue, though the tests could be updated to be more explict about that. In [1]: import numpy as np
In [2]: data = np.eye(5, dtype=np.float64)
In [3]: np.percentile(data, [60, 80], keepdims=True, axis=1)
Out[3]:
array([[[ 0. ],
[ 0. ],
[ 0. ],
[ 0. ],
[ 0. ]],
[[ 0.2],
[ 0.2],
[ 0.2],
[ 0.2],
[ 0.2]]])
In [4]: np.nanpercentile(data, [60, 80], keepdims=True, axis=1)
Out[4]:
array([[[ 0. ],
[ 0. ],
[ 0. ],
[ 0. ],
[ 0. ]],
[[ 0.2],
[ 0.2],
[ 0.2],
[ 0.2],
[ 0.2]]]) |
4fdc1a4
to
65708e7
Compare
Tests have been updated to be more explicit about the issues pointed out by @ssanderson. |
Closing and reopening, because Travis CI failed on an out of disk space error. Trying the tests again. |
@dfreese this looks like it's passing now? |
@ssanderson, Yeah, looks good from my end, though I'm open to additional comments. |
65708e7
to
2bc6a4c
Compare
rebase to upstream master. |
2bc6a4c
to
227c79d
Compare
rebased again. |
227c79d
to
c1fd0ad
Compare
@charris just saw that 1.10 is in RC. Will this sort of fix need to wait until 1.11? |
c1fd0ad
to
d7bc812
Compare
Rebased again. Can this be merged? |
warnings.simplefilter('always') | ||
val = np.percentile(mat, perc, axis=axis, keepdims=keepdim) | ||
nan_val = np.nanpercentile(nan_mat, perc, axis=axis, | ||
keepdims=keepdim) |
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.
PEP8, line up under nan_mat
.
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.
Also below.
Couple of style nits. Could you also expand the commit message to describe what you have done? |
Fix bug where nanpercentile would crash with an all-nan slices when given multiple percentiles. Also corrects behavior where array sizes different from numpy.percentile would be returned with keepdims enabled. Fix numpy#5760
d7bc812
to
6e69994
Compare
style was fixed up, and commit message updated. |
looks good, thanks |
BUG: Handle multiple percentiles for all-nan slices in nanpercentile
Fixes #5760