Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Check for NaN Values in Python
1
Check for NaN Values in Python

[b]Overview[/b]

Problem: How to check if a given value is
Code:
NaN
?

Here’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:
NaN
value?

Code:
NaN
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
Code:
NaN
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
Code:
NaN
.

We have to deal with
Code:
NaN
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
Code:
math.isnan()
.

Code:
math.isnan()
is a function of the math module in Python that checks for
Code:
NaN
constants in float objects and returns True for every NaN value encountered and returns False otherwise.

Example:

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:
x
represents a NaN value, hence, the
Code:
isNaN
method returns
Code:
True
but in case of
Code:
y
,
Code:
isNan
returns
Code:
False
and prints the type of the variable
Code:
y
as an output.

[b]Method 2: Hack NaN Using != Operator[/b]

The most unique thing about
Code:
NaN
values is that they are constantly shapeshifting. This means we cannot compare the
Code:
NaN
value even against itself. Hence, we can use the
Code:
!=
(not equal to) operator to check for the
Code:
NaN
values. Thus, the idea is to check if the given variable is equal to itself. If we consider any object other than
Code:
NaN
, the expression
Code:
(x == x)
will always return
Code:
True
. If it’s not equal, then it is a
Code:
NaN
value.

Example 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")) # True

Example 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!
[b]Method 3: Using numpy.isnan()[/b]

We can also use the
Code:
NumPy
library to check whether the given value is
Code:
NaN
or not. We just need to ensure that we import the library at the start of the program and then use its
Code:
np.isnan(x)
method.

The
Code:
np.isnan(number)
function checks whether the element in a Numpy array is
Code:
NaN
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
Code:
NaN
value or not.

Code:
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!

[Image: 1f4a1.png]TRIVIA

Let us try to perform some basic functions on an numpy array that involves
Code:
NaN
values and find out what happens to it.

Code:
import numpy as np arr = np.array([10, 20, np.nan, 40, np.nan]) print(arr.sum()) print(arr.max())

Output:

Code:
nan nan

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
Code:
np.nansum()
and
Code:
np.nanmax()
that help us to calculate the sum and maximum values in the array by ignoring the presence of
Code:
NaN
values in the array.

Example:

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.0
[b][b]Method 4: Using pandas.isna()[/b][/b]

Another way to solve our problem is to use the
Code:
isna()
method of the Pandas module.
Code:
pandas.isna()
is a function that detects missing values in an array-like object. It returns True if any
Code:
NaN
value is encountered.

Example 1:

Code:
import pandas as pd x = float("nan") y = 25.75 print(pd.isna(x)) print(pd.isna(y))

Output:

Code:
True False

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.

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 False
[b]Method 5: By Checking The Range[/b]

We can check for the
Code:
NaN
values by using another NaN special property: limited range. The range of all the floating-point values falls within negative infinity to infinity. However,
Code:
NaN
values do not fall within this range.

Hence, the idea is to check whether a given value lies within the range of
Code:
-inf
and
Code:
inf
. If yes , then it is not a
Code:
NaN
value else it is a
Code:
NaN
value.

Example:

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:
pandas
,
Code:
NumPy
, and
Code:
math
) in Python to check for the
Code:
NaN
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.)

[Image: Cover_Coffee_Break_NumPy-200x300.jpg]
Reply

Users browsing this thread:
2 Guest(s)


Forum Jump: