diff options
author | Matti Picus <matti.picus@gmail.com> | 2022-06-01 20:03:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-01 20:03:15 +0300 |
commit | 6cada27f1744d004a6d8ca7731c9a6d5dfed9b3a (patch) | |
tree | b11873b378b3d89584b6fb57cd534f040eacbd80 /numpy/lib/arraysetops.py | |
parent | 66070156f2006a5a1a551573d9df189e2675b456 (diff) | |
parent | 7a880a65a2b519995e2a1a4c911380170d38ae1b (diff) | |
download | numpy-6cada27f1744d004a6d8ca7731c9a6d5dfed9b3a.tar.gz |
Merge pull request #21623 from rjeb/unique-equalnans
ENH: Add equals_nans kwarg to np.unique Fixes #20326
Diffstat (limited to 'numpy/lib/arraysetops.py')
-rw-r--r-- | numpy/lib/arraysetops.py | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index d44e1a983..6d36fdcbd 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -131,13 +131,13 @@ def _unpack_tuple(x): def _unique_dispatcher(ar, return_index=None, return_inverse=None, - return_counts=None, axis=None): + return_counts=None, axis=None, *, equal_nans=None): return (ar,) @array_function_dispatch(_unique_dispatcher) def unique(ar, return_index=False, return_inverse=False, - return_counts=False, axis=None): + return_counts=False, axis=None, *, equal_nans=True): """ Find the unique elements of an array. @@ -162,8 +162,10 @@ def unique(ar, return_index=False, return_inverse=False, return_counts : bool, optional If True, also return the number of times each unique item appears in `ar`. + equal_nans : bool, optional + If True, collapses multiple NaN values in return array into 1 - .. versionadded:: 1.9.0 + .. versionchanged: 1.24 axis : int or None, optional The axis to operate on. If None, `ar` will be flattened. If an integer, @@ -269,7 +271,8 @@ def unique(ar, return_index=False, return_inverse=False, """ ar = np.asanyarray(ar) if axis is None: - ret = _unique1d(ar, return_index, return_inverse, return_counts) + ret = _unique1d(ar, return_index, return_inverse, return_counts, + equal_nans = equal_nans) return _unpack_tuple(ret) # axis was specified and not None @@ -312,13 +315,13 @@ def unique(ar, return_index=False, return_inverse=False, return uniq output = _unique1d(consolidated, return_index, - return_inverse, return_counts) + return_inverse, return_counts, equal_nans = equal_nans) output = (reshape_uniq(output[0]),) + output[1:] return _unpack_tuple(output) def _unique1d(ar, return_index=False, return_inverse=False, - return_counts=False): + return_counts=False, *, equal_nans=True): """ Find the unique elements of an array, ignoring shape. """ @@ -334,7 +337,8 @@ def _unique1d(ar, return_index=False, return_inverse=False, aux = ar mask = np.empty(aux.shape, dtype=np.bool_) mask[:1] = True - if aux.shape[0] > 0 and aux.dtype.kind in "cfmM" and np.isnan(aux[-1]): + if (equal_nans and aux.shape[0] > 0 and aux.dtype.kind in "cfmM" and + np.isnan(aux[-1])): if aux.dtype.kind == "c": # for complex all NaNs are considered equivalent aux_firstnan = np.searchsorted(np.isnan(aux), True, side='left') else: |