What is np.nan in Python

The np.nan is a constant representing a missing or undefined numerical value in a NumPy array. It stands for “not a number” and has a float type. The np.nan is equivalent to NaN and NAN.

Syntax and Examples


Example 1: Basic use of the np.nan

import numpy as np

myarr = np.array([1, 0, np.nan, 3])



[ 1. 0. nan 3.]

The important thing I would like you to take away from this is that all of our integers have been converted to floats, and that’s because NumPy has defined the NaN data type as a float.

Due to implicit upcasting, all our elements have been converted to float data types. 

NaN items also precede every other item when mathematical operations are used.

Example 2: nan value in the console

import numpy as np




Example 3: Comparing np.nan values in Python

You can use the double equal(==) operator to compare two nan values in Python.

import numpy as np

print(np.nan == np.nan)



What is the np.isnan() function?

The np.isnan() is a NumPy library function that tests element-wise for NaN and returns the result as a boolean array. It takes an array-like input and an optional output parameter. For example, if you have an array x = [1, 2, np.nan], you can use the np.isnan(x) function to get [False, False, True].

import numpy as np





How is np.nan different from None?

The main difference between None and np.nan is that None is used in Python to represent the absence of a value, and np.nan is specifically used in NumPy to represent missing or null numerical values.

Unlike None, np.nan is a floating-point value, and its type is float.

How to check for np.nan values in a NumPy array?

You can use the np.isnan() function to check for np.nan values in a NumPy array. The np.isnan() function returns a Boolean array, with True values where the elements in the input array are np.nan and False values where the elements are not np.nan.

How to replace np.nan values in a NumPy array?

You can use the np.where() function to replace np.nan values with a specified value in a Numpy array.

import numpy as np

arr = np.array([1, 2, np.nan, 4, 5])
result = np.where(np.isnan(arr), 0, arr)


[1. 2. 0. 4. 5.]

How np.nan can be handled in computations?

When you use np.nan in numerical operations, it propagates through the computation and results in another np.nan value.


The np.nan stands for “Not a Number “, a special floating-point value used in Python’s NumPy library to represent missing or undefined values. The syntax is np.nan, which returns the nan value.

Related posts

Python isnan()

Numpy isfinite()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.