- Using the
float("nan")
method - Using the
Decimal("nan")
method - Using
math.nan
- Using the NumPy library
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
, ornumpy.nan
to assignNaN
to a variable.NaN passed to
float()
orDecimal()
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.
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:
NaN is not the same as infinity
in Python.
Python offers different ways to assign NaN to a variable. Here’s how we can do it:
float("nan")
methodDecimal("nan")
methodmath.nan
Note: Multiple methods exist to provide flexibility and consistency. Whether we’re working in core Python or using specialized libraries like
numpy
ormath
, 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.
float("nan")
methodWe 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
Decimal("nan")
methodWe 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
math.nan
NaN is also part of the math module in Python 3.5 and onward. This can be used as shown below.
import mathn1 = math.nanprint(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!).
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 npn1 = np.nan# Check if a value is NaNprint(np.isnan(n1))
Let’s quickly assess our understanding of NaN values in Python by trying the following quiz:
Quiz!
What is a correct way to assign NaN to a variable in Python?
float("nan")
float("NaN")
float("Nan")
All of the above
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.
If you're eager to deepen your understanding of Python and apply it to cutting-edge technologies, our Become a Machine Learning Engineer path is the perfect next step. With this structured path, you'll move beyond foundational programming and dive into machine learning concepts like supervised learning, neural networks, and model optimization that will prepare you for a successful career in AI and data science.
Don’t just learn the basics—become proficient in machine learning and ready to solve real-world problems with confidence.
Haven’t found what you were looking for? Contact Us
How can we assign NaN in Python?
How can we assign NaN values in a pandas DataFrame?
How do we add NaN values to a list in Python?
How can we replace a string NaN in Python?
How can we replace NaN with 0 in a pandas DataFrame?
How does pandas check if a value is NaN?
How can we remove NaN values from a NumPy array?
How can we count NaN values in a pandas DataFrame?