Content-Length: 341286 | pFad | http://github.com/numpy/numpy/pull/29372

64 BUG: Fix np.unique with axis=0 and 1D input not collapsing NaNs with equal_nan=True #29336 by AnkitAhlawat7742 · Pull Request #29372 · numpy/numpy · GitHub
Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AnkitAhlawat7742
Copy link

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

corrected white space
@AnkitAhlawat7742 AnkitAhlawat7742 force-pushed the AnkitAhlawat_fix-unique-equal_nan-axis0-1d branch from 63104ca to 7f04ede Compare July 16, 2025 05:56
@AnkitAhlawat7742
Copy link
Author

Hi @ngoldbaum ,
I noticed the PR hasn't been merged yet — just wanted to check if there's anything I might have missed or need to update.

@seberg
Copy link
Member

seberg commented Jul 21, 2025

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).

@AnkitAhlawat7742
Copy link
Author

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 ,
I noticed there are quite a few code changes in PR #27345, but just to clarify — the solution I proposed was specifically targeted at handling the 1D array case with axis=0.However I do see changes related to 1D arrays in the PR as well.

@AnkitAhlawat7742
Copy link
Author

@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):
Copy link
Member

@seberg seberg Jul 24, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/numpy/numpy/pull/29372

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy