-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
DOC: migration guide and errors for ndarray.ptp() should mention np.ma.ptp() #26530
Comments
I just realized that So, then it seems that old behavior can be reproduced by doing something bloaty like this: def good_old_ptp(x, *args, **kwargs):
if np.ma.is_masked(x):
return np.ma.ptp(x, *args, **kwargs)
return np.ptp(x, *args, **kwargs) In any case, if this goes like this into 2.0 unchanged, then I would at least change the shown message and changelog.. from..
to..
|
I don't think |
I can follow that reasoning. In that case, personally I'd rather see an
However, please consider this: import numpy as np
x = np.arange(12).reshape((3, 4))
x = np.ma.masked_less(x, 6)
print(repr(x))
print(repr(np.mean(x, axis=1))) Not trying to be a pain, just trying to help this be a good release. ❤️ I can and will add a helper function in our project that picks the correct routine to use from amongst |
Ah, I didn't realize it returned a masked array with no mask. That doesn't seem great. I think
PRs welcome but it would be nice to see a benchmark with e.g. a trivial ndarray subclass that doesn't define a Also a case could be made for bringing back Ping @rgommers do you have a take on what the best way forward here is? |
Please no. What The problem boils down to this: >>> import numpy as np
>>> x = np.arange(12).reshape((3, 4))
... x = np.ma.masked_less(x, 6)
>>> np.asarray(x)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]) What we should do is make |
Nothing here is specific to |
OK, glad I checked. Ralf had a lot more context on this stuff than I do so I'll defer to him. |
Thanks @ngoldbaum and @rgommers, looks like you got a good way forward figured out. And yes, for me it seems like one of the good ways forward to just raise on calling a non |
I opened gh-26669 for the structural fix. |
Describe the issue:
EDIT: See below comment on
np.ma.ptp()
np.ptp()
changed its behavior (compared to the oldndarray.ptp()
) when used on masked arrays withaxis
parameter if the expected result is a partially masked output array. It ignores the mask in the computation and also returns a masked array but without a proper mask.Note that without the
reshape
andaxis
parameter in the below example it gets even a bit weirder. Old behavior was to return a scalarint64
, new behavior is to return a masked array with a single item and no mask.I believe this is a bug, since neither the maintenance_2.x changelog nor the issue tracker show anything about behavior changes for ptp.
Stumbled across this in our test suite when getting our downstream package
obspy
ready for numpy 2.0Reproduce the code example:
Error message:
Python and NumPy Versions:
Above output compares 1.23.2 and 2.0.0rc2
Runtime Environment:
Context for the issue:
Well, it seems to make inptp
unusable on masked arrays and it would need major contortions to restore the old behavior (like filling masked values with the minimum along that axis and then also copying the mask over to the result), which doesn't seem to be what numpy wants. Masked arrays "just work" and this issue would mean a breach of that rule.EDIT: When migrating code that at runtime encounters a mix of regular and masked arrays, just applying the proposed change from the migration guide ("ptp -- Use np.ptp(arr, ...) instead") will break users' codes if they don't realize that
np.ma.ptp()
is needed for masked arrays.The text was updated successfully, but these errors were encountered: