8000 WIP: handling of nan in unique, fixes #2111 by jaimefrio · Pull Request #5487 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

WIP: handling of nan in unique, fixes #2111 #5487

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

Closed
wants to merge 1 commit into from
Closed
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
< 8000 /details>
Diff view
Diff view
6 changes: 6 additions & 0 deletions numpy/lib/arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ def unique(ar, return_index=False, return_inverse=False, return_counts=False):
aux = ar
flag = np.concatenate(([True], aux[1:] != aux[:-1]))

# gh-2111: NaNs never compare equal, so they need special handling,
# but they always sort to the end
if issubclass(ar.dtype.type, np.inexact) and np.isnan(aux[-1]):
nanidx = np.searchsorted(aux, np.nan)
Copy link
Member
@mattip mattip Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you replace this with nanidx = np.nonzero(np.isnan(aux))[0] ?

Edit: ... and the next line with flag[nanindex[:-1] + 1] = False

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that creates an intermediate array of the same size as aux, so it's O(n) in both space and time. The solution above is O(log n) in time and O(1) in space, so it will almost certainly have better performance. It could be argued that your proposed code may be more understandable to a beginner, but I don't think np.searchsorted should be considered advanced or obscure, and should be perfectly fine to use, especially in numpy's source code.

So I guess I like my code better... ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattip's solution has the benefit of working for all types. So maybe we want both?

flag[nanidx+1:] = False

if not optional_returns:
ret = aux[flag]
else:
Expand Down
12 changes: 12 additions & 0 deletions numpy/lib/tests/test_arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ def check_all(a, b, i1, i2, c, dt):
a2, a2_inv = np.unique(a, return_inverse=True)
assert_array_equal(a2_inv, np.zeros(5))

# test for arrays with nans, gh-2111
a = [5, np.nan, 1, 2, 1, 5, np.nan]*10
b = [1, 2, 5, np.nan]
i1 = [2, 3, 0, 1]
i2 = [2, 3, 0, 1, 0, 2, 3]*10
c = np.multiply([2, 1, 2, 2], 10)
for dt in np.typecodes['Float']:
aa = np.array(a, dt)
bb = np.array(b, dt)
check_all(aa, bb, i1, i2, c, dt)


def test_intersect1d(self):
# unique inputs
a = np.array([5, 7, 1, 2])
Expand Down
0