← Back to NumPy Course | Chapter 10: Boolean & Comparison | Lesson 4 of 6

np.isnan/isinf

These functions find missing values (NaN) and infinite values inside numeric arrays.

In this page:

  1. np.isnan/isinf
Syntax
python
np.isnan(arr)
np.isinf(arr)

np.isnan/isinf

NaN means "not a number" and marks missing or invalid results, while inf marks overflow or division by a float zero. np.isnan, np.isinf and np.isfinite return boolean masks. Note that nan == nan is False, so you must use isnan to detect it.

Note: np.nanmean, np.nansum and friends ignore NaN values.

Example: np.isnan/isinf

python
import numpy as np

a = np.array([1.0, np.nan, 3.0, np.inf])
print(np.isnan(a))
print(np.isinf(a))
print(np.isfinite(a))
print(np.nanmean(a[np.isfinite(a)]))

# Output:
# [False  True False False]
# [False False False  True]
# [ True False  True False]
# 2.0
Related Topics
Common Mistakes
  1. Testing NaN with ==
  2. Letting NaN silently poison a mean
  3. Forgetting inf and nan are different
Chapter Summary
  • NaN is not equal to itself
  • Use isnan to detect it
  • isinf finds infinities
  • nan-prefixed functions ignore NaN
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.