-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
BUG: Fix np.unique with axis=0 and 1D input not collapsing NaNs with equal_nan=True #29336 #29372
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
base: main
Are you sure you want to change the base?
BUG: Fix np.unique with axis=0 and 1D input not collapsing NaNs with equal_nan=True #29336 #29372
Conversation
corrected white space
Review comment implementation
63104ca
to
7f04ede
Compare
Hi @ngoldbaum , |
Haven't looked at this, but one note/question is how it ties back to: #27345 (and maybe there is even something relevant for this there). |
@seberg , |
@seberg are we going to merge this PR ? ,as it is a Quick fix for 1st case(For 1D arrays, np.nan values are not treated as equal even when equal_nan=True.) |
@@ -290,7 +290,7 @@ def unique(ar, return_index=False, return_inverse=False, | |||
""" | |||
ar = np.asanyarray(ar) | |||
if axis is None: | |||
if axis is None or (axis == 0 and ar.ndim == 1): |
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.
are we going to merge this PR
I suppose it makes sense, yes. But this isn't quite right and you should add a test for all cases that make sense: that includes -1
.
It also will allow axis == 0.0
here which we should reject, maybe just check ar.ndim == 1
first and then call normalize_axis_index
to be definitely right.
(And since that is clearly easy to miss, maybe add a test for that too.)
EDIT: Actually, this just gets us back to what @ngoldbaum said: Use axis is None or ar.ndim == 1
, then just add a normalize_axis_index()
-- but you can ignore the result.
What was the Issue
Issue link :- #29336
Bug Description:- The equal_nan parameter in numpy.unique does not work correctly in some cases. Specifically:
For 1D arrays, np.nan values are not treated as equal even when equal_nan=True.
For 2D arrays with axis=0, the issue is similar — np.nan values are not treated as equal.
What This PR Fixes
This pull request fixes the issue for 1D arrays only.
Fixed: np.unique(..., equal_nan=True) now treats NaNs as equal in 1D arrays.
Notes
This PR does not address the 2D case (e.g., np.unique(array, axis=0, equal_nan=True)) described in the same issue.In coming days i will investigate and provide fix for this case as well