What is the numpy.isnan() Method in Python

Numpy.isnan() method is “used to check element-wise whether it is NaN or not and returns the result as a boolean array.”

Syntax

numpy.isnan(input_array or the scalar value, out(output array))

Parameters

input_array: The first parameter is the input array or the input for which we want to check whether it is NaN. The second one is the n-dimensional array, which is optional. Finally, it is the output array that is placed with the result.

Return Value

The Numpy isnan() method returns a Boolean array, which has the result if we pass the array and Boolean value true or false if we pass a scalar value according to the value passed.

Example 1: How to Use numpy.isnan() method

import numpy as np

print("NaN value - : ", np.isnan(933), "\n") # Scalar Values

print("NaN value - : ", np.isnan(444), "\n")

print("NaN value - : ", np.isnan(np.inf), "\n") # checking for infinity value

print("NaN value - : ", np.isnan(np.NINF), "\n")

print("NaN value - : ", np.isnan(np.nan)) # Checking for nan values

Output

NaN value - : False

NaN value - : False

NaN value - : False

NaN value - : False

NaN value - : True

Example 2

import numpy as np

arr = np.arange(24).reshape(6, 4)
print("List = ", arr)
print("\n")
print("Is NaN - ", np.isnan(arr))

Output

List = [[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]


Is NaN - [[False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]]

That’s it.

Related posts

Python math.isnan()

Check for nan values

Check if a string is a nan

Leave a Comment

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