What is nan in Python (float('nan'), math.nan, np.nan) | note.nkmk.me (2024)

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

Contents

  • nan is a float value in Python
  • Create nan: float('nan'), math.nan, np.nan
  • Check if a value is nan: math.isnan(), np.isnan()
  • Behavior for comparison operators (<, >, ==, !=) with nan
  • Check nan in the if statement
  • Remove and replace nan in a list
  • Operations with nan

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

import mathimport numpy as npimport 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.

  • None in Python

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

  • NumPy: Remove NaN (np.nan) from an array
  • NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan()
  • pandas: Remove NaN (missing values) with dropna()
  • pandas: Replace NaN (missing values) with fillna()

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'))# nanprint(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.

  • NumPy: Read and write CSV files (np.loadtxt, np.genfromtxt, np.savetxt)
  • pandas: Read CSV into DataFrame with read_csv()
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, 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'))# nanprint(float('NaN'))# nanprint(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)# nanprint(np.nan)# nanprint(np.NaN)# nanprint(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')))# Trueprint(math.isnan(math.nan))# Trueprint(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')))# Trueprint(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.

  • Missing values in pandas (nan, None, pd.NA)
  • pandas: Detect and count NaN (missing values) with isnull(), isna()

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'))# Falseprint(10 > float('nan'))# Falseprint(10 == float('nan'))# Falseprint(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'))# Falseprint(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.

  • Convert between bool (True/False) and other types in Python

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 = 100if 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.

  • Extract, replace, convert elements of a list in Python

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

  • NumPy: Remove NaN (np.nan) from an array
  • NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan()
  • pandas: Remove NaN (missing values) with dropna()
  • pandas: Replace NaN (missing values) with fillna()

Operations with nan

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

print(float('nan') + 100)# nanprint(float('nan') - 100)# nanprint(float('nan') - 100)# nanprint(float('nan') / 100)# nanprint(float('nan') ** 100)# nan

source: nan_usage.py

What is nan in Python (float('nan'), math.nan, np.nan) | note.nkmk.me (2024)

References

Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5583

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.