Python NumPy - Replace NaN with zero and fill positive infinity for complex input values
Last Updated : 25 Apr, 2022
In this article, we will see how to replace NaN with zero and fill positive infinity for complex input values in Python.
Numpy package provides us with the numpy.nan_to_num() method to replace NaN with zero and fill positive infinity for complex input values in Python. This method substitutes a nan value with a number and replaces positive infinity with the number of our choice. Let's see the syntax of the numpy.nan_to_num() in detail.
Syntax: numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)
Parameters:
- x: array like or scalar object. data given as input.
- copy: optional value, boolean. pass 'true' to create a copy of x , or 'false' to replace the values inplace. by default 'true'.
- nan: optional value, int or float.Fill NaN values with this value. NaN values will be substituted with 0.0 if no value is given.
- posinf: optional value, int or float. Fill positive infinity values with this value. Positive infinity values will be replaced with an extremely big number if no value is given.
- neginf: optional value, int or float.Fill in negative infinity values with this value. Negative infinity values will be replaced with a very small integer if no value is passed.
Returns: an array object.
Example 1:
In this example, we created an array of imaginary numbers with the help of np.nan and np.inf. The shape of the array is defined by the .shape attribute and the dimension of the array is defined by .ndim. Now we will use the posinf parameter to replace np.inf with the 999999 value.
Python3 import numpy as np # array of imaginary numbers array = np.array([complex(np.nan, 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 0.0 and np.inf # is replaced with 999999 print("After replacement the array is : ",np.nan_to_num(array, posinf = 999999))
Output:
[nan+infj] Shape of the array is : (1,) The dimension of the array is : 1 Datatype of our Array is : complex128 After replacement the array is : [0.+999999.j]
Example 2:
In this example, we are replacing nan with the value of 100.
Python3 import numpy as np # Creating an array of imaginary numbers array = np.array([complex(np.nan, 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 999999 print("After replacement the array is : ", np.nan_to_num(array,nan= 100, posinf = 999999))
Output:
[nan+infj] Shape of the array is : (1,) The dimension of the array is : 1 Datatype of our Array is : complex128 After replacement the array is : [100.+999999.j]
Example 3:
In this example, we are replacing nan= 100, posinf = 999999, neginf=0.
Python3 # import package import numpy as np # Creating an array of imaginary numbers array = np.array([complex(np.nan, np.inf),-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 999999 print("After replacement the array is : ", np.nan_to_num(array,nan= 100, posinf = 999999, neginf=0))
Output:
[ nan+infj -inf +0.j] Shape of the array is : (2,) The dimension of the array is : 1 Datatype of our Array is : complex128 After replacement the array is : [100.+999999.j 0. +0.j]
Similar Reads
Replace NaN with zero and fill negative infinity values in Python 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_
3 min read
Python | Replace negative value with zero in numpy array Given numpy array, the task is to replace negative value with zero in numpy array. Letâs see a few examples of this problem. Method #1: Naive Method Python3 # Python code to demonstrate # to replace negative value with 0 import numpy as np ini_array1 = np.array([1, 2, -3, 4, -5, -6]) # printing init
4 min read
Python NumPy - Return real parts if input is complex with all imaginary parts close to zero In this article, we will discuss how to return real parts if the input is complex with all imaginary parts close to zero in Python. The numpy np.real_if_close() method is used to return the real parts if the input is a complex number with all imaginary parts close to zero. âClose to zeroâ is defined
2 min read
Python Pandas: Replace Zeros with Previous Non-Zero Value When working with a dataset, it's common to encounter zeros that need to be replaced with non-zero values. This situation arises in various contexts, such as financial data, sensor readings, or any dataset where a zero might indicate missing or temporary invalid data. Python's Pandas library provide
4 min read
Replace all the NaN values with Zero's in a column of a Pandas dataframe Replacing the NaN or the null values in  a dataframe can be easily performed using a single line DataFrame.fillna() and DataFrame.replace() method. We will discuss these methods along with an example demonstrating how to use it.                            DataFrame.fillna()
3 min read