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
use aux[-1] as nan, ..versionchanged, release note
  • Loading branch information
filip_trojan authored and filip_trojan committed Dec 28, 2020
commit 8b294c576fc67559767db6cce7da3b57844a9cc2
6 changes: 6 additions & 0 deletions doc/release/upcoming_changes/18070.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``np.unique`` now returns single ``NaN``
----------------------------------------
When ``np.unique`` operated on an array with multiple ``NaN`` entries,
its return included a ``NaN`` for each entry that was ``NaN`` in the original array.
This is now improved such that the returned array contains just one ``NaN`` as the
last element.
11 changes: 6 additions & 5 deletions numpy/lib/arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,11 @@ def unique(ar, return_index=False, return_inverse=False,
effect that we end up with a 1-D array of structured types that can be
treated in the same way as any other 1-D array. The result is that the
flattened subarrays are sorted in lexicographic order starting with the
first element. If nan values are in the input array, a single nan is put
to the end of the sorted unique values.
first element.

.. versionchanged: NumPy 1.21
If nan values are in the input array, a single nan is put
to the end of the sorted unique values.

Examples
--------
Expand Down Expand Up @@ -326,9 +329,7 @@ def _unique1d(ar, return_index=False, return_inverse=False,
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]):
# Ensure that `NaT` is used for time-like dtypes
nan = np.array(np.nan).astype(aux.dtype)
aux_firstnan = np.searchsorted(aux, nan, side='left')
aux_firstnan = np.searchsorted(aux, aux[-1], side='left')
mask[1:aux_firstnan] = (aux[1:aux_firstnan] != aux[:aux_firstnan - 1])
mask[aux_firstnan] = True
mask[aux_firstnan + 1:] = False
Expand Down
0