🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.isfinite()

AI & DATA SCIENCE // np-isfinite

np.isfinite() tests each element of an array and returns a boolean array indicating which elements are finite — neither infinite nor NaN.

Syntax

np.isfinite(arr)

Deep Dive Course

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.

editor.html
import numpy as np

arr = np.array([1.0, np.inf, np.nan, -5.0])
print(np.isfinite(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.isfinite() showing how it is used in production NumPy code.

editor.html
import numpy as np

results = np.array([1.5, 2.0, np.inf, 4.0])
clean_results = results[np.isfinite(results)]
print(clean_results)
localhost:3000

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.

editor.html
import numpy as np

arr = np.array([1.0, np.inf, np.nan, -5.0])
print(np.isfinite(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1.0, np.inf, np.nan, -5.0])
print(np.isfinite(arr))
Example 02Advanced Example
import numpy as np

results = np.array([1.5, 2.0, np.inf, 4.0])
clean_results = results[np.isfinite(results)]
print(clean_results)

Best Practices

  • Use np.isfinite() as a combined validity check after computations that might produce inf or nan, instead of two separate isinf()/isnan() checks
  • Combine np.isfinite() with boolean indexing to filter out any inf/nan values before further processing
  • Check np.all(np.isfinite(result)) as a quick sanity assertion after a numerically sensitive calculation, to catch problems early

Interview Question

Why is np.isfinite() considered more convenient than manually checking both np.isinf() and np.isnan() separately?

Hint: Think about what result you'd have to combine manually otherwise.

Checking whether a value is a normal, usable number actually requires ruling out both infinity and nan, which would otherwise mean computing the negation of isinf() combined with the negation of isnan() by hand, combining two separate boolean arrays with a negation and an AND. np.isfinite() does exactly that combined check internally in a single function call, which is both more concise to write and communicates the actual intent, checking that this is a valid, usable number, more directly than composing two unrelated-sounding checks yourself.

Exercises

MediumPractice using np.isfinite() in a real scenario.
View Solution
import numpy as np

arr = np.array([1.0, np.inf, np.nan, -5.0])
print(np.isfinite(arr))

Frequently Asked Questions

Why is np.isfinite() considered more convenient than manually checking both np.isinf() and np.isnan() separately?

Checking whether a value is a normal, usable number actually requires ruling out both infinity and nan, which would otherwise mean computing the negation of isinf() combined with the negation of isnan() by hand, combining two separate boolean arrays with a negation and an AND. np.isfinite() does exactly that combined check internally in a single function call, which is both more concise to write and communicates the actual intent, checking that this is a valid, usable number, more directly than composing two unrelated-sounding checks yourself.

Related Functions

np-isinfnp-isnanboolean-indexing