[Numpy-discussion] Revert the return of a single NaN for `np.unique` with floating point numbers?

Ralf Gommers ralf.gommers at gmail.com
Mon Aug 2 13:49:08 EDT 2021


On Mon, Aug 2, 2021 at 7:04 PM Sebastian Berg <sebastian at sipsolutions.net>
wrote:

> Hi all,
>
> In NumPy 1.21, the output of `np.unique` changed in the presence of
> multiple NaNs.  Previously, all NaNs were returned when we now only
> return one (all NaNs were considered unique):
>
>     a = np.array([1, 1, np.nan, np.nan, np.nan])
>
> Before 1.21:
>
>     >>> np.unique(a)
>     array([ 1., nan, nan, nan])
>
> After 1.21:
>
>     array([ 1., nan])
>
>
> This change was requested in an old issue:
>
>      https://github.com/numpy/numpy/issues/2111
>
> And happened here:
>
>      https://github.com/numpy/numpy/pull/18070
>
> While, it has a release note.  I am not sure the change got the
> attention it deserved.  This would be especially worrying if it is a
> regression for anyone?
>

I think it's now the expected answer, not a regression. `unique` is not an
elementwise function that needs to adhere to IEEE-754 where nan != nan. I
can't remember reviewing this change, but it makes perfect sense to me.

Cheers,
Ralf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/numpy-discussion/attachments/20210802/c334de63/attachment.html>


More information about the NumPy-Discussion mailing list