About this question
NaN values often appear in datasets when data is missing or undefined. Learn how to identify these values using built-in Python functions or libraries like math, numpy, and pandas for accurate data handling.
NaN values often appear in datasets when data is missing or undefined. Learn how to identify these values using built-in Python functions or libraries like math, numpy, and pandas for accurate data handling.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Answered on Jul 27, 2025
To check for NaN (Not a Number) values in Python, especially when working with data, there are several ways depending on the libraries you're using. NaN typically indicates missing or undefined data, and catching it early is key to maintaining data integrity.
Here are some common and effective ways to check for NaN values:
Using the math module
import math
x = float('nan')
print(math.isnan(x)) # TrueUsing numpy
import numpy as np
arr = np.array([1, 2, np.nan])
print(np.isnan(arr)) # [False False True]Using pandas
import pandas as pd
data = pd.Series([1, 2, None, float('nan')])
print(data.isna()) Key Notes:
In short, use math.isnan() for simple checks, numpy.isnan() for numerical arrays, and pandas.isna() for structured data. These tools make NaN detection both simple and powerful.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…