Let’s learn how to check for nan in Numpy array? We are going to use nan and isnan methods.
Looking for NaN values in the array
To check if there any NaN (Not a Number) values in the array you need to learn how to use Numpy nan and isnan functions.
First I created example array which does contain nan value.
Next I used a trick. NaN element will be the lowest. So it is enough for me to check if the lowest value is nan. That’s why I implemented np.isnan(np.min(my_array)) which finds me if min value is nan.
import numpy as np my_array = np.array([1, 2, 4, np.nan]) is_nan = np.isnan(np.min(my_array)) print(f"Is there NaN element is my array? \n {is_nan}")
Python printed out boolean if nan exists or not.