Skip to content
Related Articles

Related Articles

Check if the value is infinity or NaN in Python

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Oct, 2022
View Discussion
Improve Article
Save Article

In this article, we will check whether the given value is NaN or Infinity. This can be done using the math module. Let’s see how to check each value in detail.

Check for NaN values in Python

NaN Stands for “Not a Number” and it is a numeric datatype used as a proxy for values that are either mathematically undefined or cannot be represented.  There are various examples of them like:

  1. 0/0 is undefined and NaN is used for representing it.
  2. Sqrt(-ve number) cannot be stored as a real number so NaN is used for representing it.
  3. Log(-ve number) cannot be stored as a real number so NaN is used for representing it.
  4. Inverse sin or Inverse cos of a number < -1 or number > 1 is also NaN.
  5. 0 * inf also leads to NaN.

Since NaN is a type in itself It is used to assign variables whose values are not yet calculated.

Using math.isnan() to Check for NaN values in Python

To check for NaN we can use math.isnan() function as NaN cannot be tested using == operator.  

Python3




import math
 
 
x = math.nan
print(f"x contains {x}")
 
# checks if variable is equal to NaN
if(math.isnan(x)):
    print("x == nan")
else:
    print("x != nan")

Output

x contains nan
x == nan

Using np.isnan() to Check for NaN values in Python

Here, we use Numpy to test if the value is NaN in Python.

Python3




import numpy as np
 
x = float("nan")
print(f"x contains {x}")
 
# checks if variable is equal to NaN
if(np.isnan(x)):
    print("x == nan")
else:
    print("x != nan")

Output:

x contains nan
x == nan

Using pd.isna()  to Check for NaN values in Python

Here, we use Pandas to test if the value is NaN in Python.

Python3




import pandas as pd
 
x = float("nan")
x = 6
print(f"x contains {x}")
 
# checks if variable is equal to NaN
if(pd.isna(x)):
    print("x == nan")
else:
    print("x != nan")

Output:

x contains nan
x != nan

Check for Infinite values in Python

Using math.isinf() to Check for Infinite values in Python

To check for infinite in python the function used is math.isinf() which only checks for infinite. To distinguish between positive and negative infinite we can add more logic that checks if the number is greater than 0 or less than 0. The code shows this in action.

Python3




import math
 
# Function checks if negative or
# positive infinite.
def check(x):
   
    if(math.isinf(x) and x > 0):
        print("x is Positive inf")
     
    elif(math.isinf(x) and x < 0):
        print("x is negative inf")
     
    else:
        print("x is not inf")
 
 
# Creating inf using math module.
number = math.inf
check(number)
 
number = -math.inf
check(number)

Output

x is Positive inf
x is negative inf

Using np.isneginf() to Check for Infinite values in Python

Numpy also exposes two APIs to check for positive and negative infinite. which are np.isneginf() and np.isposinf()

Python3




# pip install numpy
import numpy as np
 
print(np.isneginf([np.inf, 0, -np.inf]))
print(np.isposinf([np.inf, 0, -np.inf]))

Output

[False False  True]
[ True False False]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!