-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: masked-array median of 1d array should be scalar #7592
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
Conversation
…ial case for list of scalars
I noticed that this issue was also reported in #5969. Should move discussion to there. |
2 comments: First, about advanced indexing, you are right that there is a bug here due to the fact that numpy treats Second, I think the behavior can be further improved. The way you have it now median returns a 0-d array when called on a 1-d array, but I think it would be better to simply return a scalar. I think this might easily achieved by changing |
low = asorted[ind] | ||
low._sharedmask = False | ||
if hasattr(low,"mask"): |
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 if isinstance(low, MaskedArray):
I think.
Thanks for the comments. |
Adding a tuple call to the indexing, will not affect anything except the speed of the |
Be good to finish this up. |
Last call for getting this fixed for 1.11.1. |
sry did not manage to get to it. |
I am going to do 1.11.1 this weekend (May 15). |
I have implemented an alternate fix (#7635), by explicitly checking for the 1d case. |
BUG: ma.median alternate fix for #7592
☔ The latest upstream changes (presumably #7635) made this pull request unmergeable. Please resolve the merge conflicts. |
Closing as #7635 has been merged. |
It seems like PR #4760, which improved the n-dimensional performance of ma.median, has broken the behavior for the 1d case. The previous behavior seems more correct, because:
(a) It matches the behavior of the non-masked median.
(b) It matches the examples given in the doc-strings.
(c) It matches a simpler rule: a median of n-dim array is n-1 dimensional.
This pull request fixes that, and adds a test that fails in the unfixed code.
Details:
It appears that the issue can be traced back to a special-case in numpy's "advanced indexing" algorithm, where a list of scalars is treated as a 1d array.
The fix works by avoiding this special case. The items of the index are forced to be arrays (in particular, scalars are converted to 0d arrays), so advanced indexing works in a consistent manner.
I wrote it this way to explicitly highlight the fact that the 1d result should be consistent with higher dimensional cases.
However, it is possible that an "if dim==1" would be more readable...