-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: fix ma.diff not preserving mask when using append/prepend #22776
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
Thanks, tests could probably be expanded, but seem OK. The code and examples are very close to the original, so that looks all fine. The one thing I would like is to not define the method |
numpy/ma/core.py
Outdated
@@ -7341,6 +7341,118 @@ def size(obj, axis=None): | |||
size.__doc__ = np.size.__doc__ | |||
|
|||
|
|||
def diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue): |
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.
def diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue): | |
def diff(a, /, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue): |
Just thought we should start doing this, its not like a
is a name that is nice enough we should promise it in all eternity.
I forgot one other thing (sorry about that): Can you add this function to the stubs file in core.pyi
(should be able to adapt the original .pyi
file 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.
I wrote it in the form
def diff(a, /, n=..., axis=..., prepend=..., append=...): ...
like the rest of the file.
Let me know if I should write it like the original:
@overload
def diff(
a: _T,
n: L[0],
axis: SupportsIndex = ...,
prepend: ArrayLike = ...,
append: ArrayLike = ...,
) -> _T: ...
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.
Sorry, too late to do a full review now, but wanted to comment on the doc changes.
numpy/ma/core.py
Outdated
arrays with length 1 in the direction of axis and the shape | ||
of the input array in along all other axes. Otherwise the | ||
dimension and shape must match `a` except along axis. | ||
.. versionadded:: 1.16.0 |
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 here newlines... You can remove the .. versionadded::
here, but it actually would be good to add .. versionadded:: 1.25.0
to the top (right before the Parameters
section)!
@seberg I have made a bit of a mess with accidentally committing some changes and then (kind of) revert (see 2 rev commits). Should I try to rewrite the history? |
It's fine to rewrite the history of pull requests. |
Update numpy/ma/core.py Co-authored-by: Sebastian Berg <[email protected]>
7e86a5c
to
95aac56
Compare
I don't understand the linux_aarch64 test failing. Can anyone help me out? |
Network problem, it happens. I reran 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.
Thanks, not sure we need the release note, but I guess it doesn't hurt.
@markopacak I am happy to put this in soon, because I think it looks correct.
Looking at the original code now, if n == 0
it returns a
unmodified, but here we do not have that code branch. To be clear, the original code looks buggy here when prepend/append is used (I also don't like that a
is returned directly rather than a copy in all other cases, but so be it...)
A test that it fails if a
is 0-dimensional would also not be bad (I am sure it must fail in the concatenation, which was explicit in the np.diff
code).
@seberg Update: added check for Note: I just realized that if I pass a non-MA array without prepend/append to MA diff, I get back a non-MA array, despite my docstring saying that a MaskedArray is always returned (I even added an example in the docstrings):
Should I convert any input Or should I just add a note to the docs? |
@seberg Ping. |
if n < 0: | ||
raise ValueError("order must be non-negative but got " + repr(n)) | ||
|
||
if a.ndim == 0: |
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.
Sorry for not looking earlier. I am happy to do this to keep it identical to the original code (will open an issue about it, I don't think n == 0
is correct, it should check for prepend/append to be None).
Doing a.ndim == 0
may fail on e.g. list inputs when it currently didn't fail, so we probably also need that np.ma.asanyarray()
call first that the old function has.
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.
Added np.ma.asanyarray
to keep it identical to BASE diff.
Pls ping me when you open that issue, I'd also like to take a look.
Thanks, lets put it in, sorry it took a bit! CircleCI is failing because the PR was opened a bit ago and its now still on Python 3.8 here. It was working before and the docs weren't changed, so going to take that chance. |
Closes #22465.
Ported CORE diff to MA
Added 2 unit tests