Skip to content
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: np.min not respecting masks on list of masked arrays #20401

Open
fourpoints opened this issue Nov 18, 2021 · 1 comment
Open

BUG: np.min not respecting masks on list of masked arrays #20401

fourpoints opened this issue Nov 18, 2021 · 1 comment

Comments

@fourpoints
Copy link

Describe the issue:

I'm almost sure this is intended, but it caught me by surprise.

We have that min(a.min(), b.min()) != np.min([a, b]) when (a, b) are masked arrays, but I expected them to be the same.

This also applies to other functions, such as np.max, np.nanmin, np.nanmax, and probably other reduce functions.

Reproduce the code example:

ma = np.ma.array([0, 1], mask=[True, False])

assert np.min(ma) == np.min([ma])  # AssertionError, 1 == 0
assert np.min([np.min(ma), np.min(ma)]) == np.min([ma, ma])  # AssertionError, (1, 1) == (0, 0)

Error message:

No response

NumPy/Python version information:

1.21.4 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]

@RuddeK
Copy link

RuddeK commented Nov 18, 2021

The function np.ma.min works correctly with MaskedArrays, ndarrays and lists of the earlier arrays. np.ma.min handles lists of arrays by converting them to MaskedArrays. np.min is an alias for np.amin that tries to call obj.min. If the object does not have a min method, it appears to be first converted to an ndarray and then it's min method can be called. I couldn't find the specific code for this last claim, but you can execute np.array([ma]).min() to get the same outcome as calling np.min([ma]).

In short: the difference between np.min and np.ma.min is that they convert objects without a min method (e.g. lists) to different kinds of arrays.

@WarrenWeckesser WarrenWeckesser added the component: numpy.ma masked arrays label Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants