NumPy nan_to_num()

The numpy.nan_to_num() function replaces NaN (Not a Number) values with zero and positive or negative infinity with large or small finite numbers. Users can also specify custom replacement values for NaN, positive infinity, and negative infinity.

Syntax

</>
Copy
numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)

Parameters

ParameterTypeDescription
xscalar or array_likeInput data that may contain NaN or infinity values.
copybool, optionalIf True, a copy of x is created and modified. If False, modifications are done in-place if possible.
nanint, float, optionalValue to replace NaN. Default is 0.0.
posinfint, float, optionalValue to replace positive infinity. If not specified, it is replaced by the maximum representable finite float.
neginfint, float, optionalValue to replace negative infinity. If not specified, it is replaced by the minimum representable finite float.

Return Value

Returns an array where NaN and infinity values have been replaced based on the specified parameters. If copy=False, modifications may be performed in-place on the original array.


Examples

1. Replacing NaN and Infinity with Default Values

This example replaces NaN with 0.0, positive infinity with a large finite number, and negative infinity with a very small number.

</>
Copy
import numpy as np

# Define an array containing NaN, positive infinity, and negative infinity
arr = np.array([np.nan, np.inf, -np.inf, 1, -1])

# Replace non-finite values with default replacements
result = np.nan_to_num(arr)

# Print the results
print("Original array:", arr)
print("Processed array:", result)

Output:

Original array: [ nan  inf -inf   1.  -1.]
Processed array: [ 0.00000000e+000  1.79769313e+308 -1.79769313e+308  1.00000000e+000
 -1.00000000e+000]

2. Replacing NaN and Infinity with Custom Values

Users can specify custom values for replacing NaN, positive infinity, and negative infinity.

</>
Copy
import numpy as np

# Define an array with NaN and infinity values
arr = np.array([np.nan, np.inf, -np.inf, 5.5, -3.2])

# Replace NaN with -1, positive infinity with 100, and negative infinity with -100
result = np.nan_to_num(arr, nan=-1, posinf=100, neginf=-100)

# Print the results
print("Original array:", arr)
print("Processed array:", result)

Output:

Original array: [ nan  inf -inf   5.5  -3.2]
Processed array: [ -1.  100. -100.   5.5  -3.2]

3. Using copy=False to Modify the Array In-Place

Setting copy=False modifies the original array in-place if possible.

</>
Copy
import numpy as np

# Define an array with NaN and infinity values
arr = np.array([np.nan, np.inf, -np.inf, 2, -2])

# Replace values in-place
np.nan_to_num(arr, copy=False)

# Print the modified array
print("Modified array:", arr)

Output:

Modified array: [ 0.00000000e+000  1.79769313e+308 -1.79769313e+308  2.00000000e+000
 -2.00000000e+000]

4. Handling Complex Numbers

For complex numbers, NaN and infinity replacements are applied to both the real and imaginary parts separately.

</>
Copy
import numpy as np

# Define an array with complex numbers containing NaN and infinity
arr = np.array([complex(np.nan, 2), complex(np.inf, -np.inf), complex(1, np.nan)])

# Replace NaN and infinity values with custom values
result = np.nan_to_num(arr, nan=0, posinf=10, neginf=-10)

# Print the results
print("Original complex array:", arr)
print("Processed complex array:", result)

Output:

Original complex array: [ nan+2.j  inf-infj   1.+nanj]
Processed complex array: [  0. +2.j  10.-10.j   1. +0.j]

Here, NaN is replaced with 0, positive infinity with 10, and negative infinity with -10 in both real and imaginary parts.