Skip to content

Commit

Permalink
BUG: Do not raise deprecation warning for all nans in unique (numpy#1…
Browse files Browse the repository at this point in the history
…9301)

This PR adjusts np.unique for the edge cases where all values are nan.

Fixes numpygh-19300
  • Loading branch information
thomasjpfan authored and charris committed Jul 1, 2021
1 parent 52247be commit 844571f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion numpy/lib/arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions numpy/lib/tests/test_arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,17 @@ def check_all(a, b, i1, i2, c, dt):
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,
Expand Down

0 comments on commit 844571f

Please sign in to comment.