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 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions numpy/lib/_arraysetops_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np
from numpy._core import overrides
from numpy._core._multiarray_umath import _array_converter, _unique_hash
from numpy.lib.array_utils import normalize_axis_index

array_function_dispatch = functools.partial(
overrides.array_function_dispatch, module='numpy')
Expand Down Expand Up @@ -248,14 +249,17 @@ def unique(ar, return_index=False, return_inverse=False,
>>> a = np.array([[1, 1], [2, 3]])
>>> np.unique(a)
array([1, 2, 3])

Return the unique rows of a 2D array
>>> a = np.array([np.nan,0,0,np.nan])
>>> u = np.unique(a, axis=0, equal_nan=True)
>>> u
array([ 0., nan])
# Return the unique rows of a 2D array

>>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
>>> np.unique(a, axis=0)
array([[1, 0, 0], [2, 3, 4]])

Return the indices of the original array that give the unique values:
# Return the indices of the original array that give the unique values:

>>> a = np.array(['a', 'b', 'b', 'c', 'a'])
>>> u, indices = np.unique(a, return_index=True)
Expand Down Expand Up @@ -290,7 +294,9 @@ def unique(ar, return_index=False, return_inverse=False,

"""
ar = np.asanyarray(ar)
if axis is None:
if axis is None or ar.ndim == 1:
if axis is not None:
normalize_axis_index(axis, ar.ndim)
ret = _unique1d(ar, return_index, return_inverse, return_counts,
equal_nan=equal_nan, inverse_shape=ar.shape, axis=None,
sorted=sorted)
Expand Down
18 changes: 18 additions & 0 deletions numpy/lib/tests/test_arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,3 +1256,21 @@ def test_unique_with_matrix(self, data, transpose, dtype):
u = np.unique(mat)
expected = np.unique(np.asarray(mat))
assert_array_equal(u, expected, strict=True)

def test_unique_axis0_equal_nan_on_1d_array(self):
# Test Issue #29336
arr1d = np.array([np.nan, 0, 0, np.nan])
expected = np.array([0., np.nan])
result = np.unique(arr1d, axis=0, equal_nan=True)
assert_array_equal(result, expected)

def test_unique_axis_minus1_eq_on_1d_array(self):
arr1d = np.array([np.nan, 0, 0, np.nan])
expected = np.array([0., np.nan])
result = np.unique(arr1d, axis=-1, equal_nan=True)
assert_array_equal(result, expected)

def test_unique_axis_float_raises_typeerror(self):
arr1d = np.array([np.nan, 0, 0, np.nan])
with pytest.raises(TypeError, match="integer argument expected"):
np.unique(arr1d, axis=0.0, equal_nan=False)
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy