Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
Python NumPy - Replace NaN with zero and fill positive infinity for complex input values
Next article icon

Python NumPy - Replace NaN with zero and fill positive infinity for complex input values

Last Updated : 25 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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]

Next Article
Python NumPy - Replace NaN with zero and fill positive infinity for complex input values

S

sarahjane3102
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-Mathematical Function
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences