Element-wise NaN test

EasyNumPy Programs · 4 of 100

Write a NumPy program to test element-wise for NaN.

Advertisement
Input
[2, 4, np.nan, 7]
Output
[False False  True False] [False False False False]

Approach

  1. np.isnan(); x == np.nan never works because NaN is not equal to itself.

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment