Consider the following script:
import numpy as np
a = np.array([np.nan], dtype=float)
b = np.array([np.nan], dtype=float)
print a == b
a = np.array([np.nan], dtype=object)
b = np.array([np.nan], dtype=object)
print a == b
On my machine this prints out
[False]
[ True]
The first case is clear (as per IEEE-754), but what's going on in the second case? Why are the two NaNs comparing equal?
Python 2.7.3, Numpy 1.6.1 on Darwin.
On newer versions of numpy you get this warning:
my guess is that numpy is using
id
test as a shortcut, forobject
types before falling back to__eq__
test, and sinceit returns true.
if you use
float('nan')
instead ofnp.nan
the result would be different: