Missing Values (NaN)

Python · NumPy Library

Theory

NumPy marks a missing number with np.nan (Not a Number), which is a special float value. An integer array cannot hold nan, so adding one turns the whole array into float64. nan spreads through maths: anything plus nan is nan, so np.mean of an array with one gap returns nan. It is also not equal to anything, even itself, so arr == np.nan is always False.

Use np.isnan(arr) to get a mask of the gaps; np.isnan(arr).sum() counts them and arr[~np.isnan(arr)] drops them. The nan-aware functions np.nanmean, np.nansum, np.nanmin, np.nanmax and np.nanstd ignore the gaps. To fill gaps, np.where(np.isnan(arr), fill, arr) replaces them with a value such as the mean, and np.nan_to_num(arr, nan=0) replaces them with 0.

Why we use it

Real data from sensors, forms and files has gaps. If you do not handle nan, one missing value silently turns totals and averages into nan, which then breaks later steps such as model training.

When to use it

Check np.isnan(arr).sum() right after loading numeric data. Use nan-aware functions for summaries, fill with the mean or median when a model needs complete data, and drop rows when gaps are few. For tables, pandas isna, fillna and dropna are easier.

How it works

  1. Numeric array loaded
  2. Count gaps: np.isnan(a).sum()
  3. Any gaps? Yes: Decide how to handle them. No: Use normal functions.
  4. Just need a summary? Yes: Use np.nanmean / np.nansum. No: Fill with np.where or drop with a[~np.isnan(a)].
  5. Results that are not silently nan

Syntax

np.nan
np.isnan(a)  np.isnan(a).sum()
a[~np.isnan(a)]
np.nanmean(a)  np.nansum(a)  np.nanmax(a)
np.where(np.isnan(a), fill, a)

Example

temps = np.array([31.5, np.nan, 29.0])
print(np.mean(temps))     # nan
print(np.nanmean(temps))  # 30.25

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic