Not a number#

Not a number is a special floating point value to signal that the result of a floating point calculation is invalid.

In text we usually use NaN to refer to the Not-a-Number value.

For example, dividing 0 by 0 is invalid, and returns a NaN value:

import numpy as np
np.array(0) / 0
/tmp/ipykernel_3927/2135315712.py:1: RuntimeWarning: invalid value encountered in divide
  np.array(0) / 0
nan

As you see above, Numpy uses all lower-case: nan for the NaN value.

You can also find the NaN value in the Numpy module:

np.nan
nan

NaN values are not equal to anything#

The NaN value has some specific properties.

It is not equal to anything, even itself:

np.nan == 0
False
np.nan == np.nan
False

Detecting NaN values#

You have found above that you cannot look for NaN values by using == np.nan.

To allow for this, use np.isnan to tell you whether a number or an array element is NaN.

np.isnan([0, np.nan])
array([False,  True])