First of all, 1.79769313486e+308 is not the same as +inf. The former is the largest number which can be expressed with a 64-bit float, the latter is a special float.

If you just have very large numbers in your array, then:

A[A > 1e308] = 0

is sufficient. Thet'll replace oll elements above 1e308 with 0.

It is also possible to operate with the inf's. For example:

>>> fmax = np.finfo(np.float64).max
>>> pinf = float('+inf')
>>> ninf = float('-inf')
>>> fnan = float('nan')
>>> print fmax, pinf, ninf, fnan
1.79769313486e+308 inf -inf nan

So, these are completely different things. You may compare some of them:

>>> pinf > fmax
True
>>> ninf < 0.0
True
>>> pinf == pinf
True
>>> pinf == ninf
False

This looks good! However, nan acts differently:

>>> fnan > 0
False
>>> fnan < 0
False
>>> fnan == 0
False
>>> fnan < pinf
False
>>> fnan == fnan
False

You may use positive and negativi infinities with Numpy ndarraywithout any problems. This will work:

A[A == pinf] = 0.0

But if you have nans in the array, you'll get some complaints:

>>> np.array([fnan, pinf, ninf]) < 0
RuntimeWarning: invalid value encountered in less
[False, False, True]

So, it works but complains => do not use. The same without the nan:

>>> np.array([0.0, pinf, ninf]) < 0
[False, False, True]

If you want to do something with the nans (should you have them), use numpy.isnan:

A[np.isnan(A)] = 0.0

will change all nans into zeros.


And -- this you did not ask -- here is one to surprise your friends (*):

>>> [float('-0.0'), 0.0] * 3
[-0.0, 0.0, -0.0, 0.0, -0.0, 0.0]

Yep, float64 (and float32) have even a separate -0.0. In calculations it acts as an ordinary zero, though:

>>> float('-0.0') == 0.0
True

(*) Depending on the kind of people you call friends.

Answer from DrV on Stack Overflow
Top answer
1 of 2
23

First of all, 1.79769313486e+308 is not the same as +inf. The former is the largest number which can be expressed with a 64-bit float, the latter is a special float.

If you just have very large numbers in your array, then:

A[A > 1e308] = 0

is sufficient. Thet'll replace oll elements above 1e308 with 0.

It is also possible to operate with the inf's. For example:

>>> fmax = np.finfo(np.float64).max
>>> pinf = float('+inf')
>>> ninf = float('-inf')
>>> fnan = float('nan')
>>> print fmax, pinf, ninf, fnan
1.79769313486e+308 inf -inf nan

So, these are completely different things. You may compare some of them:

>>> pinf > fmax
True
>>> ninf < 0.0
True
>>> pinf == pinf
True
>>> pinf == ninf
False

This looks good! However, nan acts differently:

>>> fnan > 0
False
>>> fnan < 0
False
>>> fnan == 0
False
>>> fnan < pinf
False
>>> fnan == fnan
False

You may use positive and negativi infinities with Numpy ndarraywithout any problems. This will work:

A[A == pinf] = 0.0

But if you have nans in the array, you'll get some complaints:

>>> np.array([fnan, pinf, ninf]) < 0
RuntimeWarning: invalid value encountered in less
[False, False, True]

So, it works but complains => do not use. The same without the nan:

>>> np.array([0.0, pinf, ninf]) < 0
[False, False, True]

If you want to do something with the nans (should you have them), use numpy.isnan:

A[np.isnan(A)] = 0.0

will change all nans into zeros.


And -- this you did not ask -- here is one to surprise your friends (*):

>>> [float('-0.0'), 0.0] * 3
[-0.0, 0.0, -0.0, 0.0, -0.0, 0.0]

Yep, float64 (and float32) have even a separate -0.0. In calculations it acts as an ordinary zero, though:

>>> float('-0.0') == 0.0
True

(*) Depending on the kind of people you call friends.

2 of 2
15

To remove the very high values:

>>> a = numpy.array([1, 2, 1.8E308, 1.8E308, 42])
>>> a[a < 1E308] # use whatever threshold you like
array([  1.,   2.,  42.])

To set them 0:

>>> a = numpy.array([1, 2, 1.8E308, 1.8E308, 42])
>>> a[a >= 1E308] = 0
>>> a
array([  1.,   2.,   0.,   0.,  42.])
🌐
GitHub
gist.github.com › korakot › 9103824e49af7477769d1312e0cf0a88
filter (remove) nan, inf from numpy array · GitHub
filter (remove) nan, inf from numpy array. GitHub Gist: instantly share code, notes, and snippets.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-remove-nan-values-from-a-given-numpy-array
How to remove NaN values from a given NumPy array? - GeeksforGeeks
January 14, 2026 - The result is flattened into a 1D array containing all valid numbers. This method removes NaN and infinite values from a NumPy array. np.isfinite() returns True for all finite numbers, allowing you to keep only valid numeric elements.
🌐
IncludeHelp
includehelp.com › python › replace-inf-with-zero-value-in-numpy-array.aspx
Python - Replace -inf with zero value in NumPy array
February 14, 2023 - Suppose that we are given a numpy array that contains some numerical values along with some -inf. We need to replace -inf with zeroes. To replace -inf with zero value, we will use numpy.isneginf() method which is used to return a boolean value as True if the negative infinity value is present.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-remove-rows-in-numpy-array-that-contains-non-numeric-values
How to Remove Rows in Numpy Array that Contains Non-Numeric Values? - GeeksforGeeks
December 13, 2025 - This method keeps only those rows where every element is a valid finite number. It works by detecting and removing rows that contain NaN, inf, or -inf in one check. ... import numpy as np a = np.array([[10.5, 22.5, 3.8], [41, np.nan, np.nan]]) res = a[np.isfinite(a).all(axis=1)] print(res)
🌐
GeeksforGeeks
geeksforgeeks.org › python › replace-infinity-with-large-finite-numbers-and-fill-nan-for-complex-input-values-using-numpy-in-python
Replace infinity with large finite numbers and fill NaN for complex input values using NumPy in Python - GeeksforGeeks
May 3, 2022 - [nan+infj] Shape of the array is : (1,) The dimension of the array is : 1 Datatype of our Array is : complex128 After replacement the array is : [1000.+1.79769313e+308j] In this example, positive infinity is replaced with a user-defined value. ... # import package import numpy as np # Creating an array of imaginary numbers array = np.array([complex(np.nan,np.inf)]) print(array) # shape of the array is print("Shape of the array is : ",array.shape) # dimension of the array print("The dimension of the array is : ",array.ndim) # Datatype of the array print("Datatype of our Array is : ",array.dtype) # np.nan is replaced with 1000 and # infinity is replaced with a large positive number print("After replacement the array is : ", np.nan_to_num(array,nan= 1000))
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.5.dev0 Manual
>>> 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])
🌐
CopyProgramming
copyprogramming.com › howto › python-remove-nan-or-inf-numpy-array
Eliminating NaN or Inf Values from a NumPy Array Using Python - Python 3 x
March 15, 2023 - Python - Why is `NaN` considered "smaller" than `-np.inf`, and therefore if you remove all " nan " in the array and then calculate min by " np.nanmin () " you get the output as expected " -inf " here no problem arises! So," Nan "is not smaller or greater than " inf " or " -inf " because actually it is not comparable with any of these or any number it will return " False " in …
🌐
Medium
medium.com › @whyamit404 › understanding-numpy-isfinite-with-examples-7305ff609ce9
Understanding numpy.isfinite() with Examples | by whyamit404 | Medium
March 6, 2025 - Works seamlessly on both 1D and multi-dimensional arrays. Helps in preprocessing data for ML models, finance, and analytics. Pro tip: Use it to replace or remove NaN and Inf values before calculations. “Data quality matters. The cleaner your data, the better your results.” · “Good programmers don’t just write code — they ask the right questions.” · You might be wondering: “What makes numpy.isfinite() different from other functions?" or "Can I use it with Pandas?"
🌐
CSDN
devpress.csdn.net › python › 63045c49c67703293080bd0a.html
how to remove positive infinity from numpy array...if it is already converted to a number?_python_Mangs-Python
August 23, 2022 - >>> a = numpy.array([1, 2, 1.8E308, 1.8E308, 42]) >>> a[a >= 1E308] = 0 >>> a array([ 1., 2., 0., 0., 42.]) ... 问题:如何重塑熊猫。系列 在我看来,它就像 pandas.Series 中的一个错误。 a = pd.Series([1,2,3,4]) b = a.reshape(2,2) b b 有类型 Series 但无法显示,最后一条语句给出异常,非常冗长,最后一行是“TypeError: %d format: a number is required, not numpy.ndarray”。 b.sha
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.5.dev0 Manual
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.4 Manual
>>> 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])
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to check NaN or infinity using NumPy? - Python - Data Science Dojo Discussions
February 21, 2023 - My dataset contains values like NaN, inf, etc. During data preprocessing, I want to remove these kinds of values that I have to check in my data. I’m a beginner and don’t know much about handling them. During the researc…
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.isinf.html
numpy.isinf — NumPy v2.1 Manual
>>> 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])
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-89.php
NumPy: Remove specific elements in a NumPy array - w3resource
August 29, 2025 - new_x = np.delete(x, index): Use the np.delete() function to delete the elements from 'x' at the specified indices. The result is a new array 'new_x' with the specified elements removed.