How can I check if any value in a given array is NaN?

+3 votes

Best answer

You can use Numpy functions *isnan()* and *any()* to check if any value in a given array is NaN.

Here is an example:

>>> import numpy as np

>>> aa=np.array([[np.nan, 0, 1],[4,6,np.nan],[8,np.nan,9]])

>>> aa

array([[nan, 0., 1.],

[ 4., 6., nan],

[ 8., nan, 9.]])

>>> np.isnan(aa)

array([[ True, False, False],

[False, False, True],

[False, True, False]])

>>> np.any(np.isnan(aa))True

>>>