Skip to content
Learn Netverks

Lesson

Step 27/36 75% through track

nan-handling

NaN handling

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches NaN handling: NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

NaN poisons aggregations unless you use `np.nanmean` and explicit masks—production pipelines depend on this.

You will apply NaN handling in contexts like: Notebooks, feature engineering pipelines, and custom numerical code.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

IEEE NaN (Not a Number) propagates through math. NumPy provides np.isnan, np.nanmean, and masking patterns for missing numeric data—common after loading real datasets from data science pipelines.

Detecting NaN

import numpy as np
a = np.array([1.0, np.nan, 3.0])
print(np.isnan(a))
print(a[~np.isnan(a)])

NaN-aware aggregations

  • np.nanmean, np.nansum, np.nanstd
  • np.where(np.isnan(a), fill, a) — impute
  • Regular mean() returns NaN if any NaN present

Pandas bridge

Pandas uses NaN for missing floats; df.to_numpy() may expose NaN in ndarray columns—handle before sklearn.

Important interview questions and answers

  1. Q: NaN == NaN?
    A: False—use np.isnan, not equality checks.
  2. Q: nanmean vs mean with mask?
    A: nanmean is concise; boolean mask gives full control.

Self-check

  1. Compute mean ignoring NaN values.
  2. Filter out NaN from an array.

Tip: Never test x == np.nan—use np.isnan.

Interview prep

isnan?

Correct way to detect NaN—equality fails.

nanmean?

Aggregate ignoring missing values.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • nanmean vs mean?
  • np.isnan mask?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump