Skip to Content

Numpy – Check If Element is Finite or Not

In this tutorial, we will look at how to check if a value in a Numpy array is finite or not with the help of some examples.

How to test for finiteness in a Numpy array?

check if elements in numpy array are finite or not

You can use the numpy.isfinite() function to check (element-wise) if values in a Numpy array are finite or not. The following is the syntax –

# test for finiteness - pass scaler value or numpy array

It returns a boolean value (True if the value is finite otherwise False) if you pass a scaler value and a 1-D boolean array if you pass an array.


Let’s now look at some examples of using the above function to test for finiteness.

Example 1 – Check if a number is finite or not using numpy.isfinite()

First, let’s pass scaler values to the numpy.isfinite() function.

Let’s create three variables – one containing a finite value and the others set to positive and negative infinity respectively and then apply the numpy.isfinite() function on each of these values.

import numpy as np

# create three variables with scaler values
a = 21
b = np.inf
c = -np.inf

# check if finite



We get True as the output for the scaler value 21 and False as the output for both the positive and the negative infinity values.

Example 2 – Element-wise check for finiteness in a Numpy array using numpy.isfinite()

If you apply the numpy.isfinite() function on an array, it will return a boolean array containing with True for values that are finite and False for infinity values.

Let’s create a 1-D array and apply the numpy.isfinite() function to it.

# create a numpy array
ar = np.array([1, 2, np.inf, 4, 5, -np.inf, np.inf])
# element-wise check for finite value in ar


array([ True,  True, False,  True,  True, False, False])

We get a boolean array as an output. You can see that in the boolean array we get True for only the values that test as finite in the original array.


In this tutorial, we looked at how we can use the numpy.isfinite() function to check if an element is finite or not in a Numpy array. Keep in mind that if you pass a scaler value, it returns a boolean value and if you pass a 1-D array it returns a boolean array.

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.