# Python NumPy For Your Grandma - 3.5 nan

Contents

In this section, we’ll see how you can use `nan` to represent missing or invalid values.

`nan` is a floating point constant that numpy reserves and treats specially. For example, consider this array called `bot` which contains 2 missing values.

``````import numpy as np

bot = np.ones(shape = (3, 4))
bot[[0, 2], [1, 2]] = np.nan
print(bot)
## [[ 1. nan  1.  1.]
##  [ 1.  1.  1.  1.]
##  [ 1.  1. nan  1.]]
``````

If you want to identify which elements of `bot` are `nan`, you might be inclined to do something like `bot == np.nan` but the result may surprise you.

``````bot == np.nan
## array([[False, False, False, False],
##        [False, False, False, False],
##        [False, False, False, False]])
``````

NumPy designed nan so that `nan == nan` returns False, but `nan != nan` returns True.

``````np.nan == np.nan
## False
np.nan != np.nan
## True
``````

This is because equivalence between missing or invalid values is not well defined. In practice, this behavior prevents silent bugs from creeping into your program.

In order to see which elements of `bot` are `nan`, you can use NumPy’s `isnan()` function.

``````np.isnan(bot)
## array([[False,  True, False, False],
##        [False, False, False, False],
##        [False, False,  True, False]])
``````

It’s important to note that `nan` is a special floating point constant, so it can only exist inside an array of floats. If you try inserting `nan` into an array of integers, booleans, or strings, you’ll get an error or unexpected behavior.

``````foo = np.array([1, 2, 3], dtype = 'int64')
foo = np.nan  # error
``````