X Tutup
Skip to content

BUG: Unique with nans and with axis#30822

Open
caph1993 wants to merge 8 commits intonumpy:mainfrom
caph1993:unique_nan_with_axis
Open

BUG: Unique with nans and with axis#30822
caph1993 wants to merge 8 commits intonumpy:mainfrom
caph1993:unique_nan_with_axis

Conversation

@caph1993
Copy link

Support for equal_nan option in np.unique when axis!=None

fixes gh-23286.
fixes gh-20873.

This PR affects the np.unique function when equal_nan is set to true and when the input is either a multi-dimensional array with given axis (not None) or a structured 1-dimensional array.

Before this PR, the results of the following two expressions are the same:

np.unique([[0, np.nan], [0, np.nan], [np.nan, 1]], equal_nan=True, axis=0) # Expression 1
np.unique([[0, np.nan], [0, np.nan], [np.nan, 1]], equal_nan=False, axis=0) # Expression 2

This PR is an alternative to #27345 . It's simpler and does the job.

@caph1993
Copy link
Author

Fixes gh-29336 too

@caph1993
Copy link
Author

caph1993 commented Feb 12, 2026

If np.array_equal(x, y, equal_nan=True) is ever implemented for structured arrays, as suggested in gh-28920, the code for unique could be greatly simplified. The big if-elif-else of this PR would simply be reduced to one instruction: mask[1:] = ~np.array_equal(aux[1:], aux[:-1], equal_nan=equal_nan).

@caph1993
Copy link
Author

@caph1993 do you intend on completing this in the spirit of the review comment?

I encourage you to re-submit this PR with a simpler implementation, focusing only on fixing the bug.

Originally posted by @mattip in #27345 (comment)

I submitted this alternative simpler PR. Can you please aprove the workflows to verify that it can be merged?

@caph1993 caph1993 changed the title ENH: Unique with nans and with axis BUG: Unique with nans and with axis Feb 25, 2026
@caph1993
Copy link
Author

Could someone help triage this PR and suggest appropriate labels?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant

X Tutup