8000 BUG: Fix unique handling of nan entries. by ftrojan · Pull Request #18070 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix unique handling of nan entries. #18070

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

Merged
merged 11 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added tests for the datetime like dtypes
  • Loading branch information
filip_trojan authored and filip_trojan committed Dec 26, 2020
commit 4cb1ac90e3400c0d3be1213cd7bb22ffbd4e7ac9
2 changes: 1 addition & 1 deletion numpy/lib/arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def _unique1d(ar, return_index=False, return_inverse=False,
mask[:1] = True
if aux.shape[0] > 0 and aux.dtype.kind in "cfmM" and np.isnan(aux[-1]):
# Ensure that `NaT` is used for time-like dtypes
nan = np.array(np.nan, dtype=aux.dtype)
nan = np.full(shape=1, fill_value=np.nan, dtype=aux.dtype)[0]
aux_firstnan = np.searchsorted(aux, nan, side='left')
mask[1:aux_firstnan] = (aux[1:aux_firstnan] != aux[:aux_firstnan - 1])
mask[aux_firstnan] = True
Expand Down
30 changes: 27 additions & 3 deletions numpy/lib/tests/test_arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,33 @@ def check_all(a, b, i1, i2, c, dt):
assert_equal(a3_idx.dtype, np.intp)
assert_equal(a3_inv.dtype, np.intp)

# test for ticket 2111
a = [2, np.nan, 1, np.nan]
ua = [1, 2, np.nan]
# test for ticket 2111 - float
a = [2.0, np.nan, 1.0, np.nan]
ua = [1.0, 2.0, np.nan]
ua_idx = [2, 0, 1]
ua_inv = [1, 2, 0, 2]
ua_cnt = [1, 1, 2]
assert_equal(np.unique(a), ua)
assert_equal(np.unique(a, return_index=True), (ua, ua_idx))
assert_equal(np.unique(a, return_inverse=True), (ua, ua_inv))
assert_equal(np.unique(a, return_counts=True), (ua, ua_cnt))

# test for ticket 2111 - datetime64
nat = np.datetime64('nat')
a = [np.datetime64('2020-12-26'), nat, np.datetime64('2020-12-24'), nat]
ua = [np.datetime64('2020-12-24'), np.datetime64('2020-12-26'), nat]
ua_idx = [2, 0, 1]
ua_inv = [1, 2, 0, 2]
ua_cnt = [1, 1, 2]
assert_equal(np.unique(a), ua)
assert_equal(np.unique(a, return_index=True), (ua, ua_idx))
assert_equal(np.unique(a, return_inverse=True), (ua, ua_inv))
assert_equal(np.unique(a, return_counts=True), (ua, ua_cnt))

# test for ticket 2111 - timedelta
nat = np.timedelta64('nat')
a = [np.timedelta64(1, 'D'), nat, np.timedelta64(1, 'h'), nat]
ua = [np.timedelta64(1, 'h'), np.timedelta64(1, 'D'), nat]
ua_idx = [2, 0, 1]
ua_inv = [1, 2, 0, 2]
ua_cnt = [1, 1, 2]
Expand Down
0