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.unique yields incorrect output for MaskedArray when axis is not None #23281

Closed
hchau630 opened this issue Feb 26, 2023 · 3 comments
Closed

Comments

@hchau630
Copy link

hchau630 commented Feb 26, 2023

Describe the issue:

np.unique seems to ignore MaskedArray mask when axis is not None, and returns a regular np.ndarray instead of a MaskedArray. In the code example below, the output is

<class 'numpy.ma.core.MaskedArray'> [1 2 3 --]
<class 'numpy.ndarray'> [1 2 3 4]

but one should instead expect

<class 'numpy.ma.core.MaskedArray'> [1 2 3 --]
<class 'numpy.ma.core.MaskedArray'> [1 2 3 --]

Reproduce the code example:

import numpy as np
import numpy.ma as ma

a = ma.array([1,2,3,4,2,3,1,4], mask=[0,0,1,1,0,0,0,1])
out = np.unique(a)
print(type(out), out)
out = np.unique(a, axis=0)
print(type(out), out)

Error message:

No response

Runtime information:

1.24.2
3.9.13 (main, Oct 13 2022, 16:12:19)
[Clang 12.0.0 ]
WARNING: threadpoolctl not found in system! Install it by pip install threadpoolctl. Once installed, try np.show_runtime again for more detailed build information
[{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP', 'ASIMDDP'],
'not_found': ['ASIMDFHM']}}]
None

Context for the issue:

I need to find unique rows in a 2D MaskedArray.

@rgommers
Copy link
Member

rgommers commented Mar 5, 2023

You should be using np.ma.unique instead. In general, functions in the main namespace are not aware of the mask of a masked array. Things may or may not work. The semantics not working stems from:

>>> np.asarray(a)
array([1, 2, 3, 4, 2, 3, 1, 4])

Using any Python function from any library that expects numpy arrays but not masked arrays will react like that. gh-18675 is the issue discussing that, so I'll close this one.

@hchau630
Copy link
Author

hchau630 commented Mar 5, 2023

Thanks for taking a look at it. But it appears there is no axis option for np.ma.unique, which I need for performing unique along rows of a 2D array. Also I can't find the documentation for np.ma.unique. Should I open another issue about this?

@rgommers
Copy link
Member

rgommers commented Mar 5, 2023

Ah good point, yes please! One seems to be a documentation issue, and the other a feature request that should not be too difficult to implement.

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

2 participants