Numpy.isnan() is a universal function (ufunc) that performs an element-wise test to check if values in an input array or scalar are NaN (Not a Number).
NaN is an exceptional floating-point value representing undefined or unrepresentable results, such as the outcome of operations like division by zero (e.g., 0/0) or (infinity – infinity).
import numpy as np print(np.isnan(np.nan)) # Output: True (It detects NaN correctly!) print(np.isnan(5.0)) # Output: False (Finite number) print(np.isnan(np.inf)) # Output: False
In this code, only np.nan returns the actual NaN value, so np.isnan() returned True for that value and False for all other values because they are not NaNs.
Syntax
numpy.isnan(x, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])
Parameters
Argument | Description |
x (array_like, required) | It represents input data to test. It can be a scalar value or an array. |
out (ndarray or None, optional) | It represents an existing array to store the result. |
where (array_like, optional) | It represents a boolean array or condition broadcastable to the input shape. |
casting (str, optional) | It controls type casting rules. Defaults to ‘same_kind’. |
order (str, optional) | It represents the memory layout for the output array. |
dtype | It represents the desired data type. |
subok | If it is True (default), subclasses of ndarray are allowed in the output. |
Checking NaN values in a Numpy Array
If the input is a Numpy array, the output will be an array with True or False values. If the array element is NaN, it returns True; otherwise, it returns False.
import numpy as np np_arr = np.array([1.0, np.nan, np.inf, 0.0]) print(np.isnan(np_arr)) # Output: [False True False False]
Only the NaN element returns True.
Multi-Dimensional Array
Array dimensions don’t matter when it comes to this method. If the input array is 2D, the output array will also be 2D. But the array contains True or False values based on the result of the isnan() method.
import numpy as np array_2d = np.array([[19.0, np.nan], [np.inf, 21.0]]) print(np.isnan(array_2d)) # Output: # [[False True] # [False False]]
Empty array
If the array is empty, the output will be empty too because there is nothing to check.
import numpy as np empty_array = np.array([]) print(np.isnan(empty_array)) # Output: # []
Using the “out” Parameter
If you want to store the result in another variable, you can use the “out” argument.
import numpy as np arr = np.array([1.0, np.nan]) out_arr = np.zeros_like(arr, dtype=bool) np.isnan(arr, out=out_arr) print(out_arr) # Output: [False True]
Using the “where” argument
For efficient masked computations without touching all elements, you can use the “where” argument. It provides controls for which elements are calculated. It applies selectively.
import numpy as np arr = np.array([1.0, np.nan]) where_cond = np.array([True, False]) out_arr = np.array([True, True], dtype=bool) np.isnan(arr, out=out_arr, where=where_cond) print(out_arr) # Output: [False True]
Counting NaN Values
For counting NaN values, you can use the np.sum() and np.isnan() methods.
import numpy as np arr = np.array([11.0, np.nan, np.nan, 12.0]) print(np.sum(np.isnan(arr))) # Output: 2
That’s all!