question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.array_equal() returns False on apparently identical arrays with NaN values

See original GitHub issue

OS: macOS Sierra (10.12.5) Python version: 2.7.12 numpy version: 1.13.0

Passing np.array_equal() two apparently identical arrays containing NaN values returns False. See the following example replicating this issue:

>>> a = np.array([1, 2, np.nan])
>>> np.array_equal(a, a)
False

It would make sense to me if this returned True, though I understand that the following comparison returns False by definition:

>>> np.nan == np.nan
False

I presume this is related to changes in np.equal and np.not_equal. If this is the desired behaviour, I wonder if it might be more helpful for the element-wise comparison to raise an exception on encountering NaN comparisons, as this has caused unit test failures for me and took a while to track down.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
charriscommented, Jun 8, 2017

Might be good to add an equal_nan argument.

In [3]: np.allclose(a, a, equal_nan=True)
Out[3]: True
2reactions
rossbarcommented, May 11, 2020

As @charris commented, it might be good to add an equal_nan argument, just as in allclose.

A particular reason why no one followed up on this suggestion?

The equal_nan kwarg has been added in #16128, which should be sufficient to close the issue. If there is still discussion to be had about np.testing.assert_array_equal, feel free to re-open or, better yet, open a new issue that links to the relevant comments here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Comparing numpy arrays containing NaN - Stack Overflow
This method only tests whether the two arrays without the NaN values are the same, but does NOT test if NaNs occurred in...
Read more >
numpy.array_equal — NumPy v1.24 Manual
True if two arrays have the same shape and elements, False otherwise. Input arrays. Whether to compare NaN's as equal.
Read more >
numpy.equal — NumPy v1.24 Manual
This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc...
Read more >
numpy.allclose — NumPy v1.24 Manual
Returns True if two arrays are element-wise equal within a tolerance. ... If True, NaN's in a will be considered equal to NaN's...
Read more >
Miscellaneous — NumPy v1.24 Manual
Special values defined in numpy: nan, inf,. NaNs can be used as a poor-man's mask (if you don't care what the original value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found