nan (not a number) in 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
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'))) #
For example, if you read a CSV file in NumPy or pandas, the missing values are represented by nan ( NaN in pandas).
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
Create nan : float(‘nan’) , math.nan , numpy.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
In addition, nan can be created by 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
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
numpy.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]
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 , < , >, == , = 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
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
To check if a value is nan , use math.isnan() and numpy.isnan() instead of == .
Check nan in the if statement
In Python, objects other than True and False are also considered true or false in the conditional expression of the if statement. 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 .
Use math.isnan() or numpy.isnan() .
x = float('nan') if math.isnan(x): print('This is nan.') else: print('This is not nan.') # This is nan.
x = 100 if math.isnan(x): print('This is nan.') else: print('This is not nan.') # This is not nan.
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() , numpy.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]
Just use math.isnan() and numpy.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