Comparison pd.isna
, math.isnan
and np.isnan
and their flexibility dealing with different type of objects.
The table below shows if the type of object can be checked with the given method:
+------------+-----+---------+------+--------+------+
| Method | NaN | numeric | None | string | list |
+------------+-----+---------+------+--------+------+
| pd.isna | yes | yes | yes | yes | yes |
| math.isnan | yes | yes | no | no | no |
| np.isnan | yes | yes | no | no | yes | <-- # will error on mixed type list
+------------+-----+---------+------+--------+------+
pd.isna
The most flexible method to check for different types of missing values.
None of the answers cover the flexibility of pd.isna
. While math.isnan
and np.isnan
will return True
for NaN
values, you cannot check for different type of objects like None
or strings. Both methods will return an error, so checking a list with mixed types will be cumbersom. This while pd.isna
is flexible and will return the correct boolean for different kind of types:
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: missing_values = [3, None, np.NaN, pd.NA, pd.NaT, '10']
In [4]: pd.isna(missing_values)
Out[4]: array([False, True, True, True, True, False])
В этом посте мы обсудим, как проверить NaN
(не число) в Python.
1. Использование math.isnan()
функция
Простое решение для проверки NaN
в Python используется математическая функция math.isnan()
. Он возвращается True
если указанный параметр является NaN
а также False
в противном случае.
import math if __name__ == ‘__main__’: x = float(‘nan’) isNaN = math.isnan(x) print(isNaN) # True |
Скачать Выполнить код
2. Использование numpy.isnan()
функция
Чтобы проверить NaN
с NumPy вы можете сделать так:
import numpy as np if __name__ == ‘__main__’: x = float(‘nan’) isNaN = np.isnan(x) print(isNaN) # True |
3. Использование pandas.isna()
функция
Если вы используете модуль pandas, рассмотрите возможность использования pandas.isna()
функция обнаружения NaN
ценности.
import pandas as pd if __name__ == ‘__main__’: x = float(‘nan’) isNaN = pd.isna(x) print(isNaN) # True |
4. Использование !=
оператор
Интересно, что благодаря спецификациям IEEE вы можете воспользоваться тем, что NaN никогда не равен самому себе.
def isNaN(num): return num != num if __name__ == ‘__main__’: x = float(‘nan’) isnan = isNaN(x) print(isnan) # True |
Скачать Выполнить код
Это все о проверке значений NaN в Python.
Спасибо за чтение.
Пожалуйста, используйте наш онлайн-компилятор размещать код в комментариях, используя C, C++, Java, Python, JavaScript, C#, PHP и многие другие популярные языки программирования.
Как мы? Порекомендуйте нас своим друзьям и помогите нам расти. Удачного кодирования 🙂
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:
- 0/0 is undefined and NaN is used for representing it.
- Sqrt(-ve number) cannot be stored as a real number so NaN is used for representing it.
- Log(-ve number) cannot be stored as a real number so NaN is used for representing it.
- Inverse sin or Inverse cos of a number < -1 or number > 1 is also NaN.
- 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}"
)
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}"
)
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}"
)
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
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"
)
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
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]
Check for finite values in Python
Using math.isfinite() to Check for finite values in Python
Checking for finite values finds values that are not NaN or infinite. Negating this function combines both the check for NaN and inf values into a single function call.
Python3
import
math
candidates
=
[
1
, math.nan, math.inf,
1
/
3
,
123
]
for
value_to_check
in
candidates:
print
(f
"{value_to_check} is NaN or inf: {not math.isfinite(value_to_check)}"
)
Output
1 is NaN or inf: False nan is NaN or inf: True inf is NaN or inf: True 0.3333333333333333 is NaN or inf: False 123 is NaN or inf: False
Using the decimal module:
Approach:
Import the decimal module.
Create a Decimal object from the value.
Use the Decimal.is_infinite() method to check if the value is infinity.
Use the Decimal.is_nan() method to check if the value is NaN.
Python3
import
decimal
x
=
decimal.Decimal(
'Infinity'
)
if
x.is_infinite():
print
(
"x is Positive inf"
)
if
x.is_finite()
and
x <
0
:
print
(
"x is negative inf"
)
Time Complexity: O(1)
Space Complexity: O(1)
Last Updated :
24 Mar, 2023
Like Article
Save Article
in this post, We’ll learn how to check NAN value in python. The NaN stands for ‘Not A Number’ which is a floating-point value that represents missing data.
You can determine in Python whether a single value is NaN or NOT. There are methods that use libraries (such as pandas, math, and numpy) and custom methods that do not use libraries.
NaN stands for Not A Number, is one of the usual ways to show a value that is missing from a set of data. It is a unique floating-point value and can only be converted to the float type.
In this article, I will explain four methods to deal with NaN in python.
In Python, we’ll look at the following methods for checking a NAN value.
- Check Variable Using Custom method
- Using math.isnan() Method
- Using numpy.nan() Method
- Using pd.isna() Method
What is NAN in Python
None is a data type that can be used to represent a null value or no value at all. None isn’t the same as 0 or False, nor is it the same as an empty string. In numerical arrays, missing values are NaN; in object arrays, they are None.
Using Custom Method
We can check the value is NaN or not in python using our own method. We’ll create a method and compare the variable to itself.
def isNaN(num): return num!= num data = float("nan") print(isNaN(data))
Output:
True
Using math.isnan()
The math.isnan()
is a Python function that determines whether a value is NaN
(Not a Number). If the provided value is a NaN, the isnan()
function returns True
. Otherwise, False
is returned.
The Syntax:
math.isnan(num)
Let’s check a variable is NaN using python script.
import math a = 2 b = -8 c = float("nan") print(math.isnan(a)) print(math.isnan(b)) print(math.isnan(c))
Output:
False False True
Using Numpy nan()
The numpy.nan() method checks each element for NaN and returns a boolean array as a result.
Let’s check a NaN variable using NumPy method:
import numpy as np a = 2 b = -8 c = float("nan") print(np.nan(a)) print(np.nan(b)) print(np.nan(c))
Output:
False False True
Using Pandas nan()
The pd.isna() method checks each element for NaN and returns a boolean array as a result.
The below code is used to check a variable NAN using the pandas method:
import pandas as pd a = 2 b = -8 c = float("nan") print(pd.isna(a)) print(pd.isna(b)) print(pd.isna(c))
Output:
False False True
Check for NaN Values in Python
Overview
Problem: How to check if a given value is NaN
?
Here’s a quick look at the solutions to follow:
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 NaN
value?
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 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 NaN
.
We have to deal with 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.
Method 1: Using math.isnan()
The simplest solution to check for NaN values in Python is to use the mathematical function math.isnan()
.
math.isnan()
is a function of the math module in Python that checks for NaN
constants in float objects and returns True for every NaN value encountered and returns False otherwise.
Example:
# 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:
NaN value encountered!
Type of Given Value: <class 'float'>
In the above example, since x
represents a NaN value, hence, the isNaN
method returns True
but in case of y
, isNan
returns False
and prints the type of the variable y
as an output.
Method 2: Hack NaN Using != Operator
The most unique thing about NaN
values is that they are constantly shapeshifting. This means we cannot compare the NaN
value even against itself. Hence, we can use the !=
(not equal to) operator to check for the NaN
values. Thus, the idea is to check if the given variable is equal to itself. If we consider any object other than NaN
, the expression (x == x)
will always return True
. If it’s not equal, then it is a NaN
value.
Example 1:
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:
# 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:
Type of 7.8 is <class 'float'>
Not a Number!
Method 3: Using numpy.isnan()
We can also use the NumPy
library to check whether the given value is NaN
or not. We just need to ensure that we import the library at the start of the program and then use its np.isnan(x)
method.
The np.isnan(number)
function checks whether the element in a Numpy array is 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 NaN
value or not.
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:
10.0 : <class 'numpy.float64'>
20.0 : <class 'numpy.float64'>
Not a Number!
40.0 : <class 'numpy.float64'>
Not a Number!
💡TRIVIA
Let us try to perform some basic functions on an numpy array that involves NaN
values and find out what happens to it.
import numpy as np arr = np.array([10, 20, np.nan, 40, np.nan]) print(arr.sum()) print(arr.max())
Output:
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 np.nansum()
and np.nanmax()
that help us to calculate the sum and maximum values in the array by ignoring the presence of NaN
values in the array.
Example:
import numpy as np arr = np.array([10, 20, np.nan, 40, np.nan]) print(np.nansum(arr)) print(np.nanmax(arr))
Output:
70.0
40.0
Method 4: Using pandas.isna()
Another way to solve our problem is to use the isna()
method of the Pandas module. pandas.isna()
is a function that detects missing values in an array-like object. It returns True if any NaN
value is encountered.
Example 1:
import pandas as pd x = float("nan") y = 25.75 print(pd.isna(x)) print(pd.isna(y))
Output:
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.
import pandas as pd df = pd.DataFrame([['Mercury', 'Venus', 'Earth'], ['1', float('nan'), '2']]) print(pd.isna(df))
Output:
0 1 2
0 False False False
1 False True False
Method 5: By Checking The Range
We can check for the 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, NaN
values do not fall within this range.
Hence, the idea is to check whether a given value lies within the range of -inf
and inf
. If yes , then it is not a NaN
value else it is a NaN
value.
Example:
li = [25.87, float('nan')] for i in li: if float('-inf') < float(i) < float('inf'): print(i) else: print("Not a Number!")
Output:
25.87
Not a Number!
Recommended read: Python Infinity
Conclusion
In this article, we learned how we can use the various methods and modules (pandas
, NumPy
, and math
) in Python to check for the 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.)
I am a professional Python Blogger and Content creator. I have published numerous articles and created courses over a period of time. Presently I am working as a full-time freelancer and I have experience in domains like Python, AWS, DevOps, and Networking.
You can contact me @:
UpWork
LinkedIn