Skip to main content

Missing Values

Missingness is a property of the data-generating process, not merely a value to replace. Pandas provides a common API over several representations, including pd.NA, NaN, and NaT.

Detect

missing_by_column = df.isna().sum()
complete_rows = df.notna().all(axis="columns")

Use isna and notna; equality with a missing sentinel is not a reliable test. Inspect both counts and rates, and split them by meaningful cohorts when aggregate missingness may hide a systematic pattern.

Choose a policy

ActionAppropriate when
Preserveabsence itself is meaningful or later logic handles it
Dropthe row/column is unusable and deletion bias is acceptable
Fill a constanta domain-specific sentinel has explicit meaning
Forward/back fillordering is valid and values persist across adjacent observations
Interpolatea defensible model connects neighboring points
Model/imputeuncertainty and leakage are controlled explicitly
df = df.dropna(subset=["required_key"])
df["country"] = df["country"].fillna("unknown")
df["balance"] = df.groupby("account_id")["balance"].ffill()

Sort by the relevant entity and time keys before directional filling. Never fill across group boundaries accidentally.

Dtypes and reductions

Nullable extension dtypes such as Int64, boolean, and string can preserve semantic types while representing missing data. Specify them at important boundaries rather than depending on inference.

Many reductions skip missing values by default. Set skipna or required counts (min_count) explicitly when that default changes the meaning of the result.

Record the decision

A cleaning pipeline should state which columns may be missing, why, and how each policy affects downstream analysis. Keep an indicator column when imputation may itself carry predictive or diagnostic information.

Source