There is:

from numpy import inf
x[x == -inf] = 0
Answer from pv. on Stack Overflow
🌐
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 - # Import numpy import numpy as np from numpy import inf # Creating a numpy array arr = np.array([-inf, -inf, 3,7,4,9,6,6,8,5,7,9]) # Display original array print("Original array:\n",arr,"\n") # replacing -inf with 0 arr[np.isneginf(arr)] = 0 # Display result print("Result:\n",arr)
🌐
bioinfo core
bioinfocore.com › home › python › python numpy replace nan in array to 0 or a number
numpy array replace nan inf to 0 or number | bioinfo core
October 21, 2021 - a = numpy.array([1,2,3,4,np.nan]) # if copy=False, the replace inplace, default is True, it will be changed to 0 by default a = numpy.nan_to_num(a, copy=True) # if you want it changed to any number, eg.
🌐
Statology
statology.org › home › pandas: how to replace inf with zero
Pandas: How to Replace inf with Zero
March 3, 2022 - #replace inf and -inf with zero df.replace([np.inf, -np.inf], 0, inplace=True) #view updated DataFrame df team points assists rebounds 0 A 18.0 5.0 0.0 1 B 0.0 7.0 8.0 2 C 19.0 7.0 10.0 3 D 0.0 9.0 6.0 4 E 14.0 12.0 6.0 5 F 11.0 9.0 0.0 6 G 20.0 9.0 9.0 7 H 28.0 0.0 12.0
🌐
Stack Overflow
stackoverflow.com › questions › 38012050 › replacing-column-of-inf-values-with-0
python - Replacing column of inf values with 0 - Stack Overflow
Copyimport numpy as np from numpy import inf x = np.array([inf, inf, 0]) # Create array with inf values print x # Show x array x[x == inf] = 0 # Replace inf by 0 print x # Show the result · Share · Share a link to this answer · Copy linkCC BY-SA 3.0 · Short permalink to this answer ·
🌐
AskPython
askpython.com › python-modules › numpy › replacing-nan-infinity-python
Usage of nan_to_num in replacing NaN and Infinity - AskPython
February 23, 2023 - We might come across some datasets with records or values that may not be represented or are usually undefined. Such values are termed as Not a Number, also known as NaN. The presence of such values can cause errors in calculations in a data analysis project. The function nan_to_num can eradicate these values making your project error-free. This function is available in the NumPy library and can also be used for replacing infinity with some number which we are going to see in the coming examples.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.6.dev0 Manual
If x is inexact, NaN is replaced by zero or by the user defined value in nan keyword, infinity is replaced by the largest finite floating point values representable by x.dtype or by the user defined value in posinf keyword and -infinity is replaced by the most negative finite floating point ...
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.13 Manual
June 10, 2017 - Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number. ... Shows which elements are positive or negative infinity. ... Shows which elements are negative infinity.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.1 Manual
If x is inexact, NaN is replaced by zero or by the user defined value in nan keyword, infinity is replaced by the largest finite floating point values representable by x.dtype or by the user defined value in posinf keyword and -infinity is replaced by the most negative finite floating point ...
🌐
SciPython
scipython.com › books › book2 › chapter-9-data-analysis-with-pandas › examples › replacing-nan-and-infinite-values-in-pandas
Replacing NaN and infinite values in pandas · scipython.com
Infinities (represented by the floating-point inf value) can be replaced with the replace method, which takes a scalar or sequence of values and substitutes them with another, single value: In [x]: ser2 = pd.Series([-3.4, 0, 0, 1], index=ser1.index) In [x]: ser2 Out[x]: a -3.4 b 0.0 c 0.0 d 1.0 dtype: float64 In [x]: ser3 = ser1 / ser2 In [x]: ser3 Out[x]: a -0.294118 b inf c -inf d 6.500000 dtype: float64 In [x]: ser3.replace([np.inf, -np.inf], 0) Out[x]: a -0.294118 b 0.000000 c 0.000000 d 6.500000 dtype: float64 · (Assuming NumPy has been imported with import numpy as np.)
🌐
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 - The numpy.nan_to_num method is used to replace Nan values with zero, fills positive infinity and negative infinity values with a user-defined value or a big positive number.
🌐
SciPy
docs.scipy.org › doc › numpy-1.12.0 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.12 Manual
January 16, 2017 - Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number. ... Shows which elements are positive or negative infinity. ... Shows which elements are negative infinity.
🌐
SciPy
docs.scipy.org › doc › numpy-1.9.2 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.9 Manual
October 18, 2015 - Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number. ... Shows which elements are negative or negative infinity. ... Shows which elements are negative infinity.
🌐
Arab Psychology
scales.arabpsychology.com › home › stats › how to replace inf with zero in pandas?
How To Replace Inf With Zero In Pandas?
November 30, 2025 - #replace inf and -inf with zero df.replace([np.inf, -np.inf], 0, inplace=True) #view updated DataFrame df team points assists rebounds 0 A 18.0 5.0 0.0 1 B 0.0 7.0 8.0 2 C 19.0 7.0 10.0 3 D 0.0 9.0 6.0 4 E 14.0 12.0 6.0 5 F 11.0 9.0 0.0 6 G 20.0 9.0 9.0 7 H 28.0 0.0 12.0
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.5 Manual
If x is inexact, NaN is replaced by zero or by the user defined value in nan keyword, infinity is replaced by the largest finite floating point values representable by x.dtype or by the user defined value in posinf keyword and -infinity is replaced by the most negative finite floating point ...
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.10 Manual
Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number. ... Shows which elements are negative or negative infinity. ... Shows which elements are negative infinity. ... Shows which elements are positive infinity. ... Shows which elements are Not a Number (NaN). ... Numpy ...
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-nan_to_num-in-python
numpy.nan_to_num() in Python - GeeksforGeeks
November 28, 2018 - numpy.nan_to_num() function is used when we want to replace nan(Not A Number) with zero and inf with finite numbers in an array.
🌐
GeeksforGeeks
geeksforgeeks.org › python › replace-nan-with-zero-and-fill-negative-infinity-values-in-python
Replace NaN with zero and fill negative infinity values in Python - GeeksforGeeks
July 23, 2025 - The numpy.nan_to_num method is used to replace Nan values with zero and it fills negative infinity values with a user-defined value or a big positive number.