-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: Ensure MaskedArray.flat can access single items #4585
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
_mask.shape = result.shape | ||
result._mask = _mask | ||
elif _mask: | ||
return masked |
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.
masked
is masked scalar, right? Why is that the right thing 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.
Or perhaps a better question is, does isinstance(_mask, ndarray)
catch all the alternatives.
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 just tried ma.flat.maskiter[1]
, [:1]
, and .next()
and it would seem isinstance(_mask, ndarray)
catches all the alternatives. Of course, I could also put it in a try/except, or turn the if statement around, if isinstance(_mask, np.bool_)
; perhaps the latter is clearest?
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 latter might be better, it's sort of six of one, half dozen of the other. The underlying type of masked
is float64
, does that change the previous output type?
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 the advantage of testing for np.bool_
is mostly that it is clearer why one has to do something special. As for previous output type, there was none -- one would get an exception! Of course, not so odd this wasn't caught, as most people would use flat
only for the iterator.
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 confess I don't really understand what is going on here, particularly this bit _mask.shape = result.shape
. Is this some sort of broadcasting? What is the relation of the two iterators? I could study the code, but the easier route is to ask you ;) Maybe a comment would be helpful.
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 do not really understand either how the two iterators could return something with a different shape, but worry there is a good reason, but one not necessarily covered by the relatively sparse tests. I checked who last changed the lines with git blame
and it seems to be @pierregm - Pierre: this is about MaskedIterator.__getitem__
; could you comment?
Looks generally correct, but I'd like a little reassurance. |
@mhvk Could you make a stab at understanding what is going on here? I'd like to get this in together with the follow on PR. |
@charris - Thinking more about it, the assignment to Note that since single-item access clearly was broken as it was, this should be an improvement no matter what! Let me know if you can think of further tests to add... |
LGTM, but what do I know ;) I still can't figure why that assignment was there to begin with, although grep shows several such assignments in the code, which I also haven't looked at. I'm going to put this in and we'll see how it goes. It could use testing with some applications that make heavy use of masked arrays. Matplotlib? Pandas? Thanks for working on the masked array code. |
BUG: Ensure MaskedArray.flat can access single items
Sorry, not been following things closely. The assignment might be there for masked matrices (some weird beast...). Since matrices can change the shape in unexpected calls. Not sure anyone actually uses masked matrices, or that it is actually necessary here. But it is the reason why the |
@seberg Hmm..., so we need some tests for that. |
@mhvk Looks like we need a fix.
|
Revert line from #4585 to get mask, data shapes to match in .flat
Currently, the
MaskedArray.flat
iterator barfs when trying to access single items, because it is tried to overwriteshape
of a non-ndarray mask (see below). This PR corrects this, and adds test cases.