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

Multiple NaN from np.unique with masked arrays #19655

Open
hdkire opened this issue Aug 12, 2021 · 5 comments
Open

Multiple NaN from np.unique with masked arrays #19655

hdkire opened this issue Aug 12, 2021 · 5 comments
Labels
component: numpy.ma masked arrays

Comments

@hdkire
Copy link

hdkire commented Aug 12, 2021

The new behavior of np.unique seems inconsistent with masked arrays.
Release notes for 1.2.1 https://numpy.org/devdocs/release/1.21.0-notes.html#np-unique-now-returns-single-nan

Reproducing code example:

import numpy as np
a1 = np.ma.masked_array([1.0, 2.0, np.nan, np.nan],
                        [False, True, False, False])
unique_a1 = np.unique(a1)
print('a1', a1)
# ... a1 [1.0 -- nan nan]
print('unique_a1', unique_a1)
# ... unique_a1 [1.0 -- nan]
# Ok, single NaN.
assert np.count_nonzero(np.isnan(unique_a1)) == 1

a2 = np.ma.masked_array([1.0, 2.0, np.nan, np.nan, 3],
                        [False, True, False, False, True])
unique_a2 = np.unique(a2)
print('a2', a2)
# ... a2 [1.0 -- nan nan --]
print('unique_a2', unique_a2)
# ... unique_a2 [1.0 -- nan nan --]
# Fail, 2 NaNs.
assert np.count_nonzero(np.isnan(unique_a2)) == 1

NumPy/Python version information:

1.21.0 3.9.5 (default, May 11 2021, 08:20:37)
[GCC 10.3.0]

@hdkire
Copy link
Author

hdkire commented Aug 12, 2021

import numpy as np
a3 = np.ma.masked_array([np.nan, np.nan, 2.0, np.nan, np.nan],
                        [True, False, False, True, False])
unique_a3 = np.unique(a3)
print('a3', a3)
# ... a3 [-- nan 2.0 -- nan]
print('unique_a3', np.unique(a3))
# ... unique_a3 [2.0 --]
# Fail, 0 NaNs.
assert np.count_nonzero(np.isnan(unique_a3)) == 1

@rossbar
Copy link
Contributor

rossbar commented Aug 13, 2021

Related to the post-merge discussion in #19301

@KiranHipparagi
Copy link

Can I work on this issues?

@KiranHipparagi
Copy link

As of Numpy version 1.21.0, np.unique now returns single NaN:

a = np.array([8, 1, np.nan, 3, np.inf, np.nan, -np.inf, -2, np.nan, 3]) np.unique(a)
array([-inf, -2., 1., 3., 8., inf, nan])

@Dinkarkumar
Copy link

Is the issue still open ? Can i work on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: numpy.ma masked arrays
Projects
None yet
Development

No branches or pull requests

5 participants