The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array. Syntax :
numpy.isnan(array [, out])
Parameters :
array : [array_like]Input array or object whose elements, we need to test for infinity
out : [ndarray, optional]Output array placed with result.
Its type is preserved and it must be of the right shape to hold the output.
Return :
boolean array containing the result. For scalar input, the result is a new boolean with value
True if the input is positive or negative infinity; otherwise the value is False.
For array input, the result is a boolean array with the same shape as the input and the values
are True where the corresponding element of the input is positive or negative infinity;
elsewhere the values are False.
Code 1 :
Python
# Python Program illustrating
# numpy.isnan() method
import numpy as geek
print("Is NaN : ", geek.isnan(1), "\n")
print("Is NaN : ", geek.isnan(0), "\n")
# not a number
print("Is NaN : ", geek.isnan(geek.nan), "\n")
# infinity
print("Is NaN : ", geek.isnan(geek.inf), "\n")
print("Is NaN : ", geek.isnan(geek.NINF), "\n")
x = geek.array([-geek.inf, 0., geek.inf])
y = geek.array([2, 2, 2])
print("Checking for NaN : ", geek.isnan(x, y))
Output :
Is NaN : False
Is NaN : False
Is NaN : True
Is NaN : False
Is NaN : False
Checking for NaN : [0 0 0]
Code 2 :
Python
# Python Program illustrating
# numpy.isnan() method
import numpy as geek
# Returns True/False value for each element
b = geek.arange(20).reshape(5, 4)
print("\n",b)
print("\nIs NaN(Not a Number): \n", geek.isnan(b))
b = [[1j],
[geek.nan]]
print("\nIs NaN(Not a Number) : \n", geek.isnan(b))
Output :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
Is NaN(Not a Number):
[[False False False False]
[False False False False]
[False False False False]
[False False False False]
[False False False False]]
Is NaN(Not a Number) :
[[False]
[ True]]
Note : These codes won't run on online IDE's. So please, run them on your systems to explore the working.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice