Assuming this is the numpy array, with shape (3,3):

ar = np.array([1, 2, 3, 4, np.nan, 5, np.nan, 6, np.inf]).reshape((3,3))
print (ar)
[[ 1.  2.  3.]
 [ 4. nan  5.]
 [nan  6. inf]]

To check for NaN, positive infinity, negative infinity, or different combinations of those, we can use:

numpy.isnan(ar)     # True wherever nan
numpy.isposinf(ar)  # True wherever pos-inf
numpy.isneginf(ar)  # True wherever neg-inf
numpy.isinf(ar)     # True wherever pos-inf or neg-inf
~numpy.isfinite(ar) # True wherever pos-inf or neg-inf or nan

respectively. Each of these returns a bool array, and passing the bool array to numpy.where() gives us two index arrays (one index array per dimension of ar):

ar_nan = np.where(np.isnan(ar))
print (ar_nan)

(array([1, 2], dtype=int64), array([1, 0], dtype=int64)) # Means, nans at (1,1) and (2,0)

and

ar_inf = np.where(np.isinf(ar))
print (ar_inf)

(array([2], dtype=int64), array([2], dtype=int64)) # Means, inf is at (2,2)

Also, to see the limits of float64:

np.finfo(np.float64)

finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)

Answer from fountainhead on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.6.dev0 Manual
Test element-wise for NaN and return result as a boolean array. ... Input array. outndarray, None, or tuple of ndarray and None, optional · A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.5 Manual
Test element-wise for NaN and return result as a boolean array. ... Input array. outndarray, None, or tuple of ndarray and None, optional · A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.1 Manual
Test element-wise for NaN and return result as a boolean array. ... Input array. outndarray, None, or tuple of ndarray and None, optional · A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
🌐
NumPy
numpy.org › doc › stable › user › misc.html
Miscellaneous — NumPy v2.5 Manual
isinf - True if value is inf · isfinite - True if not nan or inf · nan_to_num - Map nan to 0, inf to max float, -inf to min float · The following corresponds to the usual functions except that nans are excluded from the results: nansum · nanmax · nanmin ·
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.2 Manual
Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized. ... For other keyword-only arguments, see the ufunc docs. ... True where x is NaN, false otherwise. This is a scalar if x is a scalar. ... NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity...
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-check-if-a-number-is-NAN-or-INF-in-python-
How to check if a number is NAN or INF in python ?
July 26, 2020 - Another solution, it is possible ... >>> np.isfinite(y) False · To check if a variable is a number (int or float for example) a solution is to use isinstance: x = 1.2 isinstance(x, (int, float)) gives here ·...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isfinite.html
numpy.isfinite — NumPy v2.5 Manual
But infinity is equivalent to positive infinity. Errors result if the second argument is also supplied when x is a scalar input, or if first and second arguments have different shapes. ... Try it in your browser! >>> import numpy as np >>> np.isfinite(1) True >>> np.isfinite(0) True >>> np.isfinite(np.nan) False >>> np.isfinite(np.inf) False >>> np.isfinite(-np.inf) False >>> np.isfinite([np.log(-1.),1.,np.log(0)]) array([False, True, False])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.5 Manual
Errors result if the second argument is supplied when the first argument is a scalar, or if the first and second arguments have different shapes. Examples · Try it in your browser! >>> import numpy as np >>> np.isinf(np.inf) True >>> np.isinf(np.nan) False >>> np.isinf(-np.inf) True >>> np.isinf([np.inf, -np.inf, 1.0, np.nan]) array([ True, True, False, False]) >>> x = np.array([-np.inf, 0., np.inf]) >>> y = np.array([2, 2, 2]) >>> np.isinf(x, y) array([1, 0, 1]) >>> y array([1, 0, 1]) Go BackOpen In Tab ·
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v1.10 Manual
Test element-wise for NaN and return result as a boolean array. ... Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.6.dev0 Manual
Errors result if the second argument is supplied when the first argument is a scalar, or if the first and second arguments have different shapes. Examples · Try it in your browser! >>> import numpy as np >>> np.isinf(np.inf) True >>> np.isinf(np.nan) False >>> np.isinf(-np.inf) True >>> np.isinf([np.inf, -np.inf, 1.0, np.nan]) array([ True, True, False, False]) >>> x = np.array([-np.inf, 0., np.inf]) >>> y = np.array([2, 2, 2]) >>> np.isinf(x, y) array([1, 0, 1]) >>> y array([1, 0, 1]) Go BackOpen In Tab ·
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.2 Manual
Errors result if the second argument is supplied when the first argument is a scalar, or if the first and second arguments have different shapes. Examples · >>> import numpy as np >>> np.isinf(np.inf) True >>> np.isinf(np.nan) False >>> np.isinf(-np.inf) True >>> np.isinf([np.inf, -np.inf, 1.0, np.nan]) array([ True, True, False, False]) >>> x = np.array([-np.inf, 0., np.inf]) >>> y = np.array([2, 2, 2]) >>> np.isinf(x, y) array([1, 0, 1]) >>> y array([1, 0, 1]) On this page
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to check NaN or infinity using NumPy? - Python - Data Science Dojo Discussions
February 21, 2023 - You can use the isfinite() method too for doing the same thing, it is an alternate method for your task and this function returns a Boolean array of the same shape as the input array, where each element is True if the corresponding element in ...
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.0 Manual
Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized. ... For other keyword-only arguments, see the ufunc docs. ... True where x is NaN, false otherwise. This is a scalar if x is a scalar. ... NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity...
🌐
GeeksforGeeks
geeksforgeeks.org › check-if-the-value-is-infinity-or-nan-in-python
Check if the value is infinity or NaN in Python - GeeksforGeeks
March 24, 2023 - In this article, we will cover how to replace NaN with zero and fill negative infinity values in Python using NumPy. Example Input: [ nan -inf  5.] Output: [0.00000e+00 9.99999e+05 5.00000e+00] Explanation: Replacing NaN with 0 and negative inf with any value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-if-the-value-is-infinity-or-nan-in-python
Check if the value is infinity or NaN in Python - GeeksforGeeks
July 23, 2025 - In this article, we will check whether the given value is NaN or Infinity. This can be done using the math module.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isfinite.html
numpy.isfinite — NumPy v2.2 Manual
But infinity is equivalent to positive infinity. Errors result if the second argument is also supplied when x is a scalar input, or if first and second arguments have different shapes. ... >>> import numpy as np >>> np.isfinite(1) True >>> np.isfinite(0) True >>> np.isfinite(np.nan) False >>> np.isfinite(np.inf) False >>> np.isfinite(-np.inf) False >>> np.isfinite([np.log(-1.),1.,np.log(0)]) array([False, True, False])
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.3 Manual
Test element-wise for NaN and return result as a boolean array. ... Input array. outndarray, None, or tuple of ndarray and None, optional · A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
🌐
SciPy
docs.scipy.org › doc › numpy-1.9.2 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v1.9 Manual
Test element-wise for NaN and return result as a boolean array. ... Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.