There is:
Copyfrom numpy import inf
x[x == -inf] = 0
Answer from pv. on Stack OverflowNumPy
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.
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 - Replace nan with zero and inf with finite numbers.
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.
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}")
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v1.10 Manual
Replace nan with zero and inf with finite numbers.
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.
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 - Replace nan with zero and inf with finite numbers.
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.
Top answer 1 of 2
20
You have to save the operation in your dataframe. One way is to use the parameter inplace=True:
df_fund['dly_retn'].replace(np.inf, 0, inplace=True)
na_fund['dly_retn'].replace(np.inf, 0, inplace=True)
2 of 2
6
you can do it for an entire dataframe as follow:
new_df.replace(np.inf, 0, inplace=True)
Top answer 1 of 3
12
You can just replace NaN and infinite values with the following mask:
output[~np.isfinite(output)] = 0
>>> output
array([[1. , 0.5 , 1. , 1. , 0. ,
1. ],
[1. , 1. , 0.5 , 1. , 0.46524064,
1. ],
[1. , 1. , 1. , 0. , 1. ,
1. ]])
2 of 3
6
There is a special function just for that:
numpy.nan_to_num(x_arr, copy=False, nan=0.0, posinf=0.0, neginf=0.0)
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.