Open In App

Replace NaN with zero and fill negative infinity values in Python

Last Updated : 25 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover how to replace NaN with zero and fill negative infinity values in Python using NumPy.

Example

Input: [ nan -inf   5.]

Output: [0.00000e+00 9.99999e+05 5.00000e+00]

Explanation: Replacing NaN with 0 and negative inf with any value.

numpy.nan_to_num method

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. neginf is the keyword used for this purpose.

Syntax: numpy.nan_to_num(arr, copy=True)

Parameter:

  • arr : [array_like] Input data.
  • copy : [bool, optional] Default is True.

Return: New Array with the same shape as arr and dtype of the element in arr with the greatest precision.

Example 1:

In this example, an array is created using numpy.array() method which consists of np.nan, negative infinity, and positive infinity. The shape, datatype, and dimensions of the array can be found by .shape, .dtype, and .ndim attributes. Here, np.nan is replaced with 100 using the nan parameter, and negative infinity is replaced with 999999 using the neginf parameter.

Python3




# import package
import numpy as np
 
# Creating an array of imaginary numbers
array = np.array([np.nan,-np.inf,5])
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 0 and
# negative infinity is replaced with 999999
print("After replacement the array is : ",
      np.nan_to_num(array,nan= 0, neginf=999999))


Output:

[ nan -inf   5.]

Shape of the array is :  (3,)

The dimension of the array is :  1

Datatype of our Array is :  float64

After replacement the array is :  [0.00000e+00 9.99999e+05 5.00000e+00]

Example 2:

In this example, we are creating an array with the help of complex numbers and integers. Here, np.nan is replaced with 100 using the nan parameter, np.inf is replaced with 100000 using the posinf parameter and negative infinity is replaced with 999999 using the neginf parameter.

Python3




# import package
import numpy as np
 
# Creating an array of imaginary numbers
array = np.array([complex(np.nan, -np.inf),1,2, 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 100 and np.inf is
# replaced with 100000 negative infinity is replaced with 999999
print("After replacement the array is: ",
      np.nan_to_num(array,nan= 100, posinf = 100000, neginf=999999))


Output:

[nan-infj  1. +0.j  2. +0.j inf +0.j]

Shape of the array is :  (4,)

The dimension of the array is :  1

Datatype of our Array is :  complex128

After replacement the array is :  [1.e+02+999999.j 1.e+00     +0.j 2.e+00     +0.j 1.e+05     +0.j]



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads