Python Practice › NumPy Programs › Program 4
Element-wise NaN test
Write a NumPy program to test element-wise for NaN.
Input
[2, 4, np.nan, 7]
Output
[False False True False] [False False False False]
Approach
np.isnan();x == np.nannever works because NaN is not equal to itself.
Solution
import numpy as npa = np.array([2, 4, np.nan, 7])print(np.isnan(a), a == np.nan)Output
[False False True False] [False False False False]
Learn the concept
New to this topic? Read the lesson, then come back.