0

While comparing arr == float('nan'), numpy is returning False instead of True.

In Out[2], value at index 3 should be True.

In [1]: arr = np.array([0, 5.3, 6, float('nan'), float('INF')])
        arr
Out[1]: array([0. , 5.3, 6. , nan, inf])

In [2]: (arr == float('nan'))

Actual Output:

array([False, False, False, False, False])

Expected Output:

array([False, False, False, True, False])
palash
  • 479
  • 4
  • 15
  • 6
    By [definition](https://en.wikipedia.org/wiki/NaN#Comparison_with_NaN) `nan` will not compare equal with itself – roganjosh Feb 25 '20 at 10:25
  • 2
    because `float('nan') != float('nan')` By definition, `nan == x` is false for all `x` – juanpa.arrivillaga Feb 25 '20 at 10:36
  • @roganjosh Thanks for the definition, it makes sense now. [How to get the indices list of all NaN value in numpy array?](https://stackoverflow.com/questions/37754948/how-to-get-the-indices-list-of-all-nan-value-in-numpy-array) - It helped but did not completely answers the question Why '==' operator did not work here. – palash Feb 25 '20 at 11:00

1 Answers1

4

You could use:

np.isnan(arr)

Output:

array([False, False, False,  True, False])
FabZanna
  • 181
  • 1
  • 10