A value is finite if it's an ordinary, well-defined number — isfinite() returns False specifically for inf, -inf, and nan, and True for every regular number, including 0 and very large or small, but not infinite, floats. This makes it a convenient single check to validate an entire array's numerical health after a computation that might have produced inf, from overflow or division by zero, or nan, from an invalid operation like 0 divided by 0, without needing two separate checks for isinf() and isnan().
1Understanding np.isfinite()
A value is finite if it's an ordinary, well-defined number — isfinite() returns False specifically for inf, -inf, and nan, and True for every regular number, including 0 and very large or small, but not infinite, floats. This makes it a convenient single check to validate an entire array's numerical health after a computation that might have produced inf, from overflow or division by zero, or nan, from an invalid operation like 0 divided by 0, without needing two separate checks for isinf() and isnan().
Use np.isfinite() as a single combined check instead of separately checking np.isinf() and np.isnan() when you just need to know whether a value is a normal, valid number — it covers both problematic cases at once.
import numpy as np
arr = np.array([1.0, np.inf, np.nan, -5.0])
print(np.isfinite(arr))2Practical Example
Here is a real-world application of np.isfinite() showing how it is used in production NumPy code.
import numpy as np
results = np.array([1.5, 2.0, np.inf, 4.0])
clean_results = results[np.isfinite(results)]
print(clean_results)3Best Practices
Follow these guidelines when working with np.isfinite():
1. Use np.isfinite() as a combined validity check after computations that might produce inf or nan, instead of two separate isinf()/isnan() checks
2. Combine np.isfinite() with boolean indexing to filter out any inf/nan values before further processing
3. Check np.all(np.isfinite(result)) as a quick sanity assertion after a numerically sensitive calculation, to catch problems early
Tip: Use np.isfinite() as a single combined check instead of separately checking np.isinf() and np.isnan() when you just need to know whether a value is a normal, valid number — it covers both problematic cases at once.
import numpy as np
arr = np.array([1.0, np.inf, np.nan, -5.0])
print(np.isfinite(arr))