summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorThomas J. Fan <thomasjpfan@gmail.com>2021-06-29 14:19:02 -0400
committerGitHub <noreply@github.com>2021-06-29 13:19:02 -0500
commit6ff787b93d46cca6d31c370cfd9543ed573a98fc (patch)
tree058097f5e52f06b6b62597b339642cabcc18fde9 /numpy/lib
parent2f0ed6acea1308c89e6c814b1317d96362370703 (diff)
downloadnumpy-6ff787b93d46cca6d31c370cfd9543ed573a98fc.tar.gz
BUG: Do not raise deprecation warning for all nans in unique (#19301)
This PR adjusts np.unique for the edge cases where all values are nan. Fixes gh-19300
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/arraysetops.py4
-rw-r--r--numpy/lib/tests/test_arraysetops.py11
2 files changed, 14 insertions, 1 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py
index 7600e17be..bd56b6975 100644
--- a/numpy/lib/arraysetops.py
+++ b/numpy/lib/arraysetops.py
@@ -339,7 +339,9 @@ def _unique1d(ar, return_index=False, return_inverse=False,
aux_firstnan = np.searchsorted(np.isnan(aux), True, side='left')
else:
aux_firstnan = np.searchsorted(aux, aux[-1], side='left')
- mask[1:aux_firstnan] = (aux[1:aux_firstnan] != aux[:aux_firstnan - 1])
+ if aux_firstnan > 0:
+ mask[1:aux_firstnan] = (
+ aux[1:aux_firstnan] != aux[:aux_firstnan - 1])
mask[aux_firstnan] = True
mask[aux_firstnan + 1:] = False
else:
diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py
index d62da9efb..13385cd24 100644
--- a/numpy/lib/tests/test_arraysetops.py
+++ b/numpy/lib/tests/test_arraysetops.py
@@ -610,6 +610,17 @@ class TestUnique:
assert_equal(np.unique(a, return_inverse=True), (ua, ua_inv))
assert_equal(np.unique(a, return_counts=True), (ua, ua_cnt))
+ # test for gh-19300
+ all_nans = [np.nan] * 4
+ ua = [np.nan]
+ ua_idx = [0]
+ ua_inv = [0, 0, 0, 0]
+ ua_cnt = [4]
+ assert_equal(np.unique(all_nans), ua)
+ assert_equal(np.unique(all_nans, return_index=True), (ua, ua_idx))
+ assert_equal(np.unique(all_nans, return_inverse=True), (ua, ua_inv))
+ assert_equal(np.unique(all_nans, return_counts=True), (ua, ua_cnt))
+
def test_unique_axis_errors(self):
assert_raises(TypeError, self._run_axis_tests, object)
assert_raises(TypeError, self._run_axis_tests,