As far as I understand it, ==
checks for equality of value, and is
checks for identity of structure behind value (as, say ===
in some other languages).
Given that, I don't understand the following:
np.isnan(30) == False
Out[19]:
True
np.isnan(30) is False
Out[20]:
False
It appears not to be the case with other identity checks:
(5 == 4) == False
Out[22]:
True
(5 == 4) is False
Out[23]:
True
It appears as if np.isnan()
returns False
as a value, but not as identity. Why is that the case?
numpy.isnan()
returns a compatible type object:
>>> import numpy
>>> type(numpy.isnan(0))
<class 'numpy.bool_'>
This is a custom boolean that can be stored efficiently in numpy arrays, see Numpy's Data Types documentation. The numpy.isnan()
function can also operate on arrays, producing another array with results:
>>> numpy.isnan(numpy.array([1, 2]))
array([False, False], dtype=bool)
where again the dtype
is the Numpy boolean object.
Python makes no guarantees that boolean operations must always return a singleton boolean value. You should never test for is True
or is False
anyway. Use numpy.isnan()
output directly in boolean operations, use not
to test for false values:
if numpy.isnan(foo):
and
if not numpy.isnan(bar):
np.isnan(30)
return
s np.False_
which has a different identity from False
; don't rely on this though.
>>> import numpy as np
>>> np.isnan(30) is np.False_
True
>>> np.False_ is False
False
>>>