How to assign NaN to a variable in Python

Key takeaways

  • NaN stands for “Not a Number,” which represents unrepresentable numeric values in Python.

  • NaN is commonly used in data analysis to represent missing or undefined data.

  • We can use float("nan"), Decimal("nan"), math.nan, or numpy.nan to assign NaN to a variable.

  • NaN passed to float() or Decimal() is case insensitive.

Handling missing or undefined data is a common challenge in data analysis and scientific computing. Python provides several methods to represent these missing values including NaN.

Why do we need NaN?

NaN (Not a Number) is a numeric “data type” used to represent any value that’s undefined or unpresentable. It’s a special floating-point value defined by the IEEE 754 standard in 1985. Here are some examples of NaN:

  • The result of division by 0 is undefined as a real number and is, therefore, represented by NaN.
  • “Square root of a negative number” is an imaginary number that cannot be represented as a real number, so, it is represented by NaN.
  • The Python NaN is also assigned to variables, in a computation, that do not have values or where the values have yet to be computed. This is especially useful in several analytical tasks with missing data.
Use cases of Nan in Python
Use cases of Nan in Python

NaN is not the same as infinity in Python.

Assigning a NaN value to Python variables

Python offers different ways to assign NaN to a variable. Here’s how we can do it:

Note: Multiple methods exist to provide flexibility and consistency. Whether we’re working in core Python or using specialized libraries like numpy or math, we can use the respective NaN to keep things consistent with the rest of your codebase. Various libraries can also optimize how NaN is managed internally.

Using the float("nan") method

We can create a NaN value using float("nan") in Python, as shown below:

Note that the “NaN” passed to the float is not case sensitive. All of the four variables come out as NaN.

n1 = float("nan")
n2 = float("Nan")
n3 = float("NaN")
n4 = float("NAN")
print n1, n2, n3, n4

Using the Decimal("nan") method

We can also use Python’s decimal library instead of floats. For example, we can use the Decimal("Nan") method instead of float("Nan").

from decimal import *
n1 = Decimal("nan")
n2 = Decimal("Nan")
n3 = Decimal("NaN")
n4 = Decimal("NAN")
print n1, n2, n3, n4

Using math.nan

NaN is also part of the math module in Python 3.5 and onward. This can be used as shown below.

import math
n1 = math.nan
print(n1)
print(math.isnan(n1))

We can use math.isnan to check whether a certain variable is NaN or not. We cannot use the regular comparison operator, ==, to check for NaN because NaN is not equal to anything (not even itself!).

Using the NumPy library

NumPy, introduced in 2005 by Travis Oliphant, provides floating point representation of NaN by using numpy.nan. Let’s understand how to use it through the following code example:

import numpy as np
n1 = np.nan
# Check if a value is NaN
print(np.isnan(n1))

Let’s quickly assess our understanding of NaN values ​​in Python by trying the following quiz:

Quiz!

1

What is a correct way to assign NaN to a variable in Python?

A)

float("nan")

B)

float("NaN")

C)

float("Nan")

D)

All of the above

Question 1 of 50 attempted

In conclusion, we can use Python’s built-in tools and libraries like math, Decimal, and NumPy to manage NaN values effectively, preventing errors and facilitating comprehensive data analysis.

Become a Data Analyst with our comprehensive learning path!

If you're ready to kickstart your career as a data analyst, then our Become a Data Analyst path is designed to take you from your first line of code to landing your first job.

Whether you’re a beginner or looking to transition into a data-driven career, this step-by-step journey will equip you with the skills to turn raw data into actionable insights. Develop expertise in data cleaning, analysis, and storytelling to make informed decisions and drive business success. With our AI mentor by your side, you’ll tackle challenges with personalized guidance. Start your data analytics career today and make your mark in the world of data!

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we assign NaN in Python?

  • Using the float("nan") method
  • Using the Decimal("nan") method
  • Using math.nan
  • Using the NumPy library

How can we assign NaN values in a pandas DataFrame?

We can assign NaN values in a pandas DataFrame using np.nan.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'X': [1, 2, np.nan],
    'Y': [4, np.nan, 6]
})

print(df)

How do we add NaN values to a list in Python?

We can add NaN values to a list in Python by appending float("nan") or numpy.nan.

import numpy as np

py_list = [8, 9, 10]
py_list.append(float('nan'))
py_list.append(np.nan)

print(py_list)  # Output: [8, 9, 10, nan, nan]

How can we replace a string NaN in Python?

We can replace a string NaN with any value in Python using the enumerate() function.

py_list = ['Educative', 'NaN', 'Platform']
for i, value in enumerate(py_list):
    if value == 'NaN':
        py_list[i] = 0

print(py_list) 

How can we replace NaN with 0 in a pandas DataFrame?

We can replace NaN with 0 or none in a pandas DataFrame using the df.fillna() method.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'X': [1, 2, np.nan],
    'Y': [4, np.nan, 6]
})

df_filled = df.fillna(0) #Or df.fillna("none")

print(df_filled)

How does pandas check if a value is NaN?

pandas can check if a value is NaN using the isna() and isnull() methods.


How can we remove NaN values from a NumPy array?

We can remove NaN values from a NumPy array using the numpy.isnan() method with boolean indexing to filter out NaN values.

import numpy as np

nparr = np.array([21,22, 24, np.nan, 6])
filtered_arr = nparr[~np.isnan(nparr)]

print(filtered_arr) 

How can we count NaN values in a pandas DataFrame?

We can count NaN values ​​in a specific pandas DataFrame column using the isna() and sum() functions.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'X': [1, 2, np.nan],
    'Y': [4, np.nan, 6]
})

print(df.isna().sum())

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved