There is:

Copyfrom numpy import inf
x[x == -inf] = 0
Answer from pv. on Stack Overflow
🌐
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.
🌐
AskPython
askpython.com › home › usage of nan_to_num in replacing nan and infinity
Usage of nan_to_num in replacing NaN and Infinity - AskPython
February 23, 2023 - 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 ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan() | note.nkmk.me
January 23, 2024 - Note that filling with the mean of the non-NaN values is not possible during the initial read with np.genfromtxt(). For this, refer to the method described below. You can use np.nan_to_num() to replace NaN. ... Note that np.nan_to_num() also ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.4 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.
🌐
TutorialsPoint
tutorialspoint.com › replace-infinity-with-large-finite-numbers-but-fill-nan-values-in-python
Replace infinity with large finite numbers but fill NaN values in Python
February 25, 2022 - To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python. The method returns, x, with the non-finite values replaced. If copy is False, this may be x itself. The 1st parameter is the input data. The 2nd parameter is copy, whether to create ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.1 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.
🌐
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.
Find elsewhere
🌐
Codepointtech
codepointtech.com › home › mastering numpy: handling nan, inf, and finite values in python
Mastering NumPy: Handling NaN, Inf, and Finite Values in Python - codepointtech.com
January 18, 2026 - Common Causes: Dividing zero by zero (0/0), taking the square root of a negative number (in real numbers), or performing operations like infinity - infinity. Key Behavior: A unique property of NaN is that it is not equal to anything, including itself (NaN != NaN). This characteristic is vital for its detection. import numpy as np # Examples of NaN generation nan_result_1 = 0 / 0 # In Python, this raises ZeroDivisionError, but in NumPy context: nan_result_2 = np.inf - np.inf nan_result_3 = np.sqrt(-1) # For complex numbers, this would be `1j` print(f"0 / 0 (NumPy context): {np.array([0.0]) / np.array([0.0])}") print(f"inf - inf: {nan_result_2}") print(f"sqrt(-1): {nan_result_3}") print(f"NaN == NaN: {np.nan == np.nan}")
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.3 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.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-replace-infinity-with-large-finite-numbers-and-fill-nan-values-in-python
5 Best Ways to Replace Infinity with Large Finite Numbers and Fill NaN Values in Python – Be on the Right Side of Change
March 1, 2024 - This example starts by obtaining the maximum finite float number representable by NumPy to replace ‘inf’ values. Using a boolean mask with np.isinf, the ‘infinity’ values in the array are replaced. To handle ‘NaN’ values, np.nan_to_num is called, which fills ‘NaN’ with the specified fill value.
🌐
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.16 Manual
February 18, 2020 - If x is inexact, NaN is replaced by zero, and infinity and -infinity replaced by the respectively largest and most negative finite floating point values representable by x.dtype.
🌐
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 - ... # import package import numpy ... # 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 ...
🌐
w3resource
w3resource.com › python-exercises › pandas › python-pandas-data-frame-exercise-52.php
Pandas: Remove infinite values from a given DataFrame - w3resource
import pandas as pd import numpy as np df = pd.DataFrame([1000, 2000, 3000, -4000, np.inf, -np.inf]) print("Original DataFrame:") print(df) print("Removing infinite values:") df = df.replace([np.inf, -np.inf], np.nan) print(df)
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.0 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.