np.isnan/isinf
These functions find missing values (NaN) and infinite values inside numeric arrays.
In this page:
Syntax
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
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
- Testing NaN with ==
- Letting NaN silently poison a mean
- 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: