Explain Codes LogoExplain Codes Logo

How can I check for NaN values?

python
nan
pandas
dataframe
Nikita BarsukovbyNikita Barsukov·Jul 22, 2024
TLDR

Using the pd.isna() function from pandas or np.isnan() from NumPy allows you to quickly spot those elusive NaN values.

import pandas as pd import numpy as np # Pandas' way of saying "Tag, you're it!" to NaN print(pd.isna(float('nan'))) # True # And NumPy chiming in with "Gotcha!" print(np.isnan(np.nan)) # True

These functions present a boolean mask, the perfect tool for exposing NaNs in arrays or dataframes.

In the realm of Python

Python, the dragon language of programming, offers its own ways to sense the presence of NaNs.

Detecting NaN in Python standard library

Python brings its own touch with math.isnan(). This function is your faithful scout, looking for NaN values in the palm trees of your code.

import math # Meet the NaN catcher of the Python realm value = float('nan') print(math.isnan(value)) # Gotcha! I mean... True

Good to remember: a NaN is the introvert of number land - it doesn't even equal itself! This quirk can be used to detect a NaN.

is_shy = float('nan') print(is_shy != is_shy) # True. Told you, it's shy!

NaN in pandas - the King of dataville

In the kingdom of data manipulation known as pandas, NaN is viewed as part of the royal family - the missing king. Here’s how to find him:

import pandas as pd df = pd.DataFrame([1, 2, np.nan, 4]) print(df.isna()) # Assembling the royal search party

A NaN detector, tailored for you

Living with NaN means occasional encounters, and creating a function like isNaN could be your royal guard against frequent NaN ambushes:

# NaN detector at your service def isNaN(num): return num != num # Putting the detector to the test print(isNaN(float('nan'))) # Affirmative! NaN detected!

Those NaN quirks that make them unique

Looks can deceive

NaN might look like a number but it follows its own rules. But remember, NaN doesn't equal even itself:

nan_number = float('nan') print(nan_number == nan_number) # False. You got pranked!

Consistency? Check!

Detecting NaN might differ in terms of function calls, but math.isnan(), np.isnan(), and pd.isna() all agree when faced with a NaN:

# The NaN suspect nan_suspect = float('nan') # The detective squad print(math.isnan(nan_suspect)) # True. Fingerprints match! print(np.isnan(nan_suspect)) # True. DNA confirmed! print(pd.isna(nan_suspect)) # True. The hat fits perfectly!

In the name of performance

In this speed-obsessed world, simplicity trumps complexity. For NaN detection, x != x might outrun other methods. Time it, I dare you:

import timeit # The race is on, and...it's a simple check by a nose! simple_check_time = timeit.timeit('x != x', setup='x = float("nan")', number=1000000) math_check_time = timeit.timeit('math.isnan(x)', setup='import math; x = float("nan")', number=1000000) # Catch a breath and print the times print(f"Simple check: {simple_check_time}") print(f"math.isnan(): {math_check_time}")