What is nan in Python (float('nan'), math.nan, np.nan)

Modified: | Tags: Python

In Python, the float type has nan. nan stands for "not a number" and is defined by the IEEE 754 floating-point standard.

In the sample code of this article, math, pandas, and NumPy are imported as follows.

import math

import numpy as np
import pandas as pd
source: nan_usage.py

Note that None, which represents the absence of a value, is different from nan. For more information on None, see the following article.

See the following articles about how to remove and replace nan in NumPy and pandas.

nan is a float value in Python

In Python, the float type includes nan, which can be created using float('nan'). Other creation methods will be described later.

print(float('nan'))
# nan

print(type(float('nan')))
# <class 'float'>
source: nan_usage.py

For example, when reading a CSV file with missing values in NumPy or pandas, nan is generated to represent these values. In pandas, this is denoted as NaN, but it also represents the missing value.

a = np.genfromtxt('data/src/sample_nan.csv', delimiter=',')
print(a)
# [[11. 12. nan 14.]
#  [21. nan nan 24.]
#  [31. 32. 33. 34.]]

df = pd.read_csv('data/src/sample_pandas_normal_nan.csv')[:3]
print(df)
#       name   age state  point  other
# 0    Alice  24.0    NY    NaN    NaN
# 1      NaN   NaN   NaN    NaN    NaN
# 2  Charlie   NaN    CA    NaN    NaN
source: nan_usage.py

Create nan: float('nan'), math.nan, np.nan

As described above, you can create nan with float('nan'). It is case-insensitive, so you can use 'NaN' and 'NAN'.

print(float('nan'))
# nan

print(float('NaN'))
# nan

print(float('NAN'))
# nan
source: nan_usage.py

In addition, nan is defined in math (standard library) and NumPy; both NaN and NAN are defined as aliases in NumPy.

print(math.nan)
# nan

print(np.nan)
# nan

print(np.NaN)
# nan

print(np.NAN)
# nan
source: nan_usage.py

They are equivalent regardless of the method used for creation.

Check if a value is nan: math.isnan(), np.isnan()

You can check if a value is nan or not with math.isnan().

print(math.isnan(float('nan')))
# True

print(math.isnan(math.nan))
# True

print(math.isnan(np.nan))
# True
source: nan_usage.py

np.isnan() is also provided.

In addition to scalar values, array-like objects, such as lists and NumPy arrays (ndarray), can also be passed as arguments.

print(np.isnan(float('nan')))
# True

print(np.isnan([float('nan'), math.nan, np.nan, 0]))
# [ True  True  True False]
source: nan_usage.py

pandas.DataFrame and Series have the method isna() and its alias isnull(), which return True for nan and None.

An error is raised if None is specified for math.isnan() or np.isnan().

Behavior for comparison operators (<, >, ==, !=) with nan

When comparing with nan, <, >, ==, <=, and >= always return False, and != always returns True.

print(10 < float('nan'))
# False

print(10 > float('nan'))
# False

print(10 == float('nan'))
# False

print(10 != float('nan'))
# True
source: nan_usage.py

The same is true for nan and nan comparisons. Note that == and != gives counter-intuitive results.

Any ordered comparison of a number to a not-a-number value is false. A counter-intuitive implication is that not-a-number values are not equal to themselves. For example, if x = float('NaN'), 3 < x, x < 3 and x == x are all false, while x != x is true. This behavior is compliant with IEEE 754. 6. Expressions - Value comparisons — Python 3.11.3 documentation

print(float('nan') == float('nan'))
# False

print(float('nan') != float('nan'))
# True
source: nan_usage.py

To check if a value is nan, use math.isnan() and np.isnan() instead of ==.

Check nan in the if statement

In Python, objects other than True and False are also evaluated as true or false in the conditions of if statements. For example, the empty string '' or the number 0 is considered false, and other strings or numbers are considered true.

As you can see with bool(), nan is evaluated as True.

print(bool(float('nan')))
# True
source: nan_usage.py

Use math.isnan() or np.isnan().

x = float('nan')

if math.isnan(x):
    print('This is nan.')
else:
    print('This is not nan.')
# This is nan.
source: nan_usage.py
x = 100

if math.isnan(x):
    print('This is nan.')
else:
    print('This is not nan.')
# This is not nan.
source: nan_usage.py

Remove and replace nan in a list

If you want to remove or replace nan in a list, use list comprehensions, conditional expressions (ternary operators), and math.isnan(), np.isnan().

l = [float('nan'), 0, 1, 2]
print(l)
# [nan, 0, 1, 2]

print([x for x in l if not math.isnan(x)])
# [0, 1, 2]

print([-100 if math.isnan(x) else x for x in l])
# [-100, 0, 1, 2]
source: nan_usage.py

Just use math.isnan() and np.isnan() for check, and the concept is the same as other cases of removing and replacing values. See the following article for details.

See the following articles about how to remove and replace nan in NumPy and pandas.

Operations with nan

Operations such as +, -, *, /, and ** with nan result nan.

print(float('nan') + 100)
# nan

print(float('nan') - 100)
# nan

print(float('nan') - 100)
# nan

print(float('nan') / 100)
# nan

print(float('nan') ** 100)
# nan
source: nan_usage.py

Related Categories

Related Articles