04-09-2022, 06:56 AM
Check for NaN Values in Python
[b]Overview[/b]
Problem: How to check if a given value is?
Here’s a quick look at the solutions to follow:
So, what is a value?
is a constant value that indicates that the given value is Not a Number. It’s a floating-point value, hence cannot be converted to any other type other than float. We should know that and Null are two different things in Python. The Null values indicate something which does not exist, i.e. is empty. But that is not the case with .
We have to deal with values frequently in Python especially when we deal with array objects or DataFrames. So, without further delay, let us dive into our mission critical question and have a look at the different methods to solve our problem.
[b]Method 1: Using math.isnan()[/b]
The simplest solution to check for NaN values in Python is to use the mathematical function.
is a function of the math module in Python that checks for constants in float objects and returns True for every NaN value encountered and returns False otherwise.
Example:
Output:
In the above example, since represents a NaN value, hence, the method returns but in case of , returns and prints the type of the variable as an output.
[b]Method 2: Hack NaN Using != Operator[/b]
The most unique thing about values is that they are constantly shapeshifting. This means we cannot compare the value even against itself. Hence, we can use the (not equal to) operator to check for the values. Thus, the idea is to check if the given variable is equal to itself. If we consider any object other than , the expression will always return . If it’s not equal, then it is a value.
Example 1:
Example 2:
Output:
[b]Method 3: Using numpy.isnan()[/b]
We can also use the library to check whether the given value is or not. We just need to ensure that we import the library at the start of the program and then use its method.
The function checks whether the element in a Numpy array is or not. It then returns the result as a boolean array.
Example: In the following example we have a Numpy Array and then we will check the type of each value. We will also check if it is a value or not.
Output:
TRIVIA
Let us try to perform some basic functions on an numpy array that involves values and find out what happens to it.
Output:
Now this can be a problem in many cases. So, do we have a way to eliminate the NaN values from our array object and then perform the mathematical operations upon the array elements? Yes! Numpy facilitates us with methods like and that help us to calculate the sum and maximum values in the array by ignoring the presence of values in the array.
Example:
Output:
[b][b]Method 4: Using pandas.isna()[/b][/b]
Another way to solve our problem is to use the method of the Pandas module. is a function that detects missing values in an array-like object. It returns True if any value is encountered.
Example 1:
Output:
Example 2: In the following example we will have a look at a Pandas DataFrame and detect the presence of NaN values in the DataFrame.
Output:
[b]Method 5: By Checking The Range[/b]
We can check for the values by using another NaN special property: limited range. The range of all the floating-point values falls within negative infinity to infinity. However, values do not fall within this range.
Hence, the idea is to check whether a given value lies within the range of and . If yes , then it is not a value else it is a value.
Example:
Output:
Recommended read: Python Infinity
[b]Conclusion[/b]
In this article, we learned how we can use the various methods and modules (, , and ) in Python to check for the values. I hope this article was able to answer your queries. Please stay tuned and subscribe for more such articles.
Authors: SHUBHAM SAYON and RASHI AGARWAL
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)
[b]Overview[/b]
Problem: How to check if a given value is
Code:
NaNHere’s a quick look at the solutions to follow:
Code:
import math
import numpy as np
import pandas as pd x = float('nan')
print(math.isnan(x))
print(x != x)
print(np.isnan(x))
print(pd.isna(x))
print(not(float('-inf') < x < float('inf')))So, what is a
Code:
NaNCode:
NaNCode:
NaNCode:
NaNWe have to deal with
Code:
NaN[b]Method 1: Using math.isnan()[/b]
The simplest solution to check for NaN values in Python is to use the mathematical function
Code:
math.isnan()Code:
math.isnan()Code:
NaNExample:
Code:
# Importing the math module
import math # Function to check for NaN values
def isNaN(a): # Using math.isnan() if math.isnan(a): print("NaN value encountered!") else: print("Type of Given Value: ", type(a)) # NaN value
x = float('NaN')
isNaN(x)
# Floating value
y = float("5.78")
isNaN(y)Output:
Code:
NaN value encountered!
Type of Given Value: <class 'float'>In the above example, since
Code:
xCode:
isNaNCode:
TrueCode:
yCode:
isNanCode:
FalseCode:
y[b]Method 2: Hack NaN Using != Operator[/b]
The most unique thing about
Code:
NaNCode:
NaNCode:
!=Code:
NaNCode:
NaNCode:
(x == x)Code:
TrueCode:
NaNExample 1:
Code:
print(5 == 5)
# True
print(['a', 'b'] == ['a', 'b'])
# True
print([] == [])
# True
print(float("nan") == float("nan"))
# False
print(float("nan") != float("nan"))
# TrueExample 2:
Code:
# Function to check for NaN values
def not_a_number(x): # Using != operator if x != x: print("Not a Number!") else: print(f'Type of {x} is {type(x)}') # Floating value
x = float("7.8")
not_a_number(x)
# NaN value
y = float("NaN")
not_a_number(y)Output:
Code:
Type of 7.8 is <class 'float'>
Not a Number!We can also use the
Code:
NumPyCode:
NaNCode:
np.isnan(x)The
Code:
np.isnan(number)Code:
NaNExample: In the following example we have a Numpy Array and then we will check the type of each value. We will also check if it is a
Code:
NaNCode:
import numpy as np arr = np.array([10, 20, np.nan, 40, np.nan])
for x in arr: if np.isnan(x): print("Not a Number!") else: print(x, ":", type(x))Output:
Code:
10.0 : <class 'numpy.float64'>
20.0 : <class 'numpy.float64'>
Not a Number!
40.0 : <class 'numpy.float64'>
Not a Number!
TRIVIALet us try to perform some basic functions on an numpy array that involves
Code:
NaNCode:
import numpy as np arr = np.array([10, 20, np.nan, 40, np.nan])
print(arr.sum())
print(arr.max())Output:
Code:
nan
nanNow this can be a problem in many cases. So, do we have a way to eliminate the NaN values from our array object and then perform the mathematical operations upon the array elements? Yes! Numpy facilitates us with methods like
Code:
np.nansum()Code:
np.nanmax()Code:
NaNExample:
Code:
import numpy as np arr = np.array([10, 20, np.nan, 40, np.nan])
print(np.nansum(arr))
print(np.nanmax(arr))Output:
Code:
70.0
40.0Another way to solve our problem is to use the
Code:
isna()Code:
pandas.isna()Code:
NaNExample 1:
Code:
import pandas as pd x = float("nan")
y = 25.75
print(pd.isna(x))
print(pd.isna(y))Output:
Code:
True
FalseExample 2: In the following example we will have a look at a Pandas DataFrame and detect the presence of NaN values in the DataFrame.
Code:
import pandas as pd df = pd.DataFrame([['Mercury', 'Venus', 'Earth'], ['1', float('nan'), '2']])
print(pd.isna(df))Output:
Code:
0 1 2
0 False False False
1 False True FalseWe can check for the
Code:
NaNCode:
NaNHence, the idea is to check whether a given value lies within the range of
Code:
-infCode:
infCode:
NaNCode:
NaNExample:
Code:
li = [25.87, float('nan')]
for i in li: if float('-inf') < float(i) < float('inf'): print(i) else: print("Not a Number!")Output:
Code:
25.87
Not a Number!Recommended read: Python Infinity
[b]Conclusion[/b]
In this article, we learned how we can use the various methods and modules (
Code:
pandasCode:
NumPyCode:
mathCode:
NaNAuthors: SHUBHAM SAYON and RASHI AGARWAL
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

