How do you check for nan and infinity in python?
Show
How to check if a single value is NaN in python. There are approaches are using libraries (pandas, math and numpy) and without using libraries.NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results. Finding and dealing with NaN within an array, series or dataframe is easy. However, identifying a stand alone NaN value is tricky. In this article I explain five methods to deal with NaN in python. The first three methods involves in-built functions from libraries. The last two relies on properties of NaN for finding NaN values. Method 1: Using Pandas Libraryisna() in pandas library can be used to check if the value is null/NaN. It will return True if the value is NaN/null. import pandas as pd Method 2: Using Numpy Libraryisnan() in numpy library can be used to check if the value is null/NaN. It is similar to isna() in pandas. import numpy as np Method 3: Using math libraryMath library provides has built-in mathematical functions. The library is applicable to all real numbers.
cmath library can be used if dealing with complex numbers. import math Method 4: Comparing with itselfWhen I started my career working with big IT company, I had to undergo a training for the first month. The trainer, when introducing the concept of NaN
values mentioned that they are like aliens we know nothing about. These aliens are constantly shapeshifting, and hence we cannot compare NaN value against itself. def isNaN(num): Method 5: Checking the rangeAnother property of NaN which can be used to check for NaN is the range. All floating point values fall within the range of minus infinity to infinity. infinity < any number< infinity However, NaN values does not come within this range. Hence, NaN can be identified if the value does not fall within the range from minus infinity to infinity. This can be implemented as below: def isNaN(num): I hope you have found the above article helpful. I am sure there would be many other techniques to check for NaN values based on various other logics. Please share the other methods you have come across to check for NaN/ Null values. Cheers! Become a MemberI hope you like the article, I would highly recommend signing up for Medium Membership to read more articles by me or stories by thousands of other
authors on variety of topics.
Yes, in fact there are several ways. A few work without any imports, while others require The following table summarizes the ways how one can create a not-a-number or a positive or negative infinity
A couple remarks to the table:
Yes there is - in fact there are several functions for NaN, Infinity, and neither Nan nor Inf. However these predefined functions are not built-in, they always require an
Again a couple of remarks:
To summarize the expected results for these functions (assuming the input is a float):
In a list it's no problem, you can always include NaN (or Infinity) there:
However if you want to include it in an
How do you check if a value is infinity in Python?The math. isinf() method checks whether a number is infinite or not. This method returns True if the specified number is a positive or negative infinity, otherwise it returns False.
What is NaN and INF in Python?inf is infinity - a value that is greater than any other value. -inf is therefore smaller than any other value. nan stands for Not A Number, and this is not equal to 0 .
How do you know if a text is NaN in Python?The math. isnan() method checks whether a value is NaN (Not a Number), or not. This method returns True if the specified value is a NaN, otherwise it returns False.
How do you check if there are infinity values in Dataframe?Method 1: Use DataFrame. isinf() function to check whether the dataframe contains infinity or not. It returns boolean value. If it contains any infinity, it will return True.
|