Missing Data and Outliers
The two data pathologies worth a decision
Missingness has a mechanism (MCAR/MAR/MNAR) and outliers have a cause (bug or signal) — analyzing both before deciding fixes separates thoughtful pipelines from delete-everything reflexes.
What you will learn
- Classify missingness mechanisms
- Detect outliers with z-scores and IQR
- Choose fixes that respect the cause
What is this?
Missing values and outliers are the two pathologies every dataset hides. Missingness falls into mechanisms: MCAR (random, ignorable), MAR (depends on observed variables), MNAR (missing because of the value itself) — each with different fix advice. Outliers are either data bugs (unit error, sensor fail) or real (fraud, whales); classify before removing because the fix differs diametrically.
Why do you need it?
- Deleting MCAR rows is wasteful but safe; deleting MNAR rows biases the sample toward survivors.
- Imputing with the mean shrinks variance and can hide the signal entirely.
- Outlier policy is a modeling decision — drop the whales and your revenue model predicts median customers.
- Interviewers reach for this exact taxonomy to test judgment, not recall.
Mental model
A diary with missing pages
Missing data mechanisms
Pages missing at random (MCAR — the wind took some), missing tied to your mood (MAR — you skip entries in bad weeks, a recorded variable), or missing precisely because those were terrible days (MNAR — you burned the pages). The remedy depends on WHY they're gone.
How it works
- Audit: df.isna().mean().sort_values() for rates; missingness heatmap for patterns.
- MCAR test: do missing rows differ on observed features? If not, plausibly random.
- MAR: missingness predicts from other observed columns (or it's close enough to treat that way).
- MNAR: missing because of the value itself — flag rates and distributions before and after missingness.
- Outliers: z = |x − mean|/std beyond ~3σ or IQR-based (x < Q1−1.5IQR or > Q3+1.5IQR); then classify bug vs signal.
Decision tree for each pathological column.
Examples
Detecting and classifying
import pandas as pd, numpy as nprng = np.random.default_rng(7)x = np.exp(rng.normal(0, 1, 1000))x[900] = 5e6 # planted outliers = pd.Series(x)q1, q3 = s.quantile(.25), s.quantile(.75)iqr = q3 - q1mask = (s < q1 - 1.5*iqr) | (s > q3 + 1.5*iqr)print(s[mask].tolist()[:5]) # the massive outlier flaggedprint(s.isna().sum())The IQR rule flags the planted 5e6 cleanly on a skewed column where ±3σ (mean/std) misfires because the tail inflates std. On skewed data the IQR fence is the robust default.
In production
- An anomaly team flags transactions beyond k·IQR as candidate fraud, not dropped — they're the signal.
- A telemetry pipeline marks a sensor's null stack as MNAR and models 'missing' as a state rather than imputing.
- A churn dataset splits missingness rate by plan — finding MAR prompted including 'has_gap' as a feature.
Best practices
- Classify every column's missingness mechanism before touching it.
- On skewed data prefer IQR fences over z-scores.
- Escalate outlier decisions to stakeholders when rows are few and large.
- Never drop missing rows wholesale — report how many and why for each column.
Common mistakes
- Imputing MNAR data as if it were random.
- z-score outlier removal on skewed data (std inflation hides real outliers).
- Dropping rows with missing target — often the most interesting ones.
- Removing 'outliers' before the model is built and never documenting the oxbow criteria.
Debugging tips
- Compare column distributions with and without missing rows — divergence implies MAR.
- If z-based outlier removal barely changes anything, the tail was already compressing — use IQR.
- Recompute missing rates per group; a group-specific rate reveals the mechanism.
Interview checkpoint
Hands-on lab
- Audit missing rates and patterns
- Detect outliers with IQR vs z-score on skewed data
- Make cause-based fix decisions
Classify the Pathologies
30 min · intermediate
Quick quiz
Income column is missing precisely for the highest earners. That's:
The IQR-based outlier fence is more robust than ±3σ for skewed columns.