numpy.array_equal() returns False on apparently identical arrays with NaN values
See original GitHub issueOS: 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:
- Created 6 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Might be good to add an
equal_nan
argument.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 aboutnp.testing.assert_array_equal
, feel free to re-open or, better yet, open a new issue that links to the relevant comments here.