Skip to content
UniCircle
MediumConceptualData preprocessing

Handling missing values without leaking information

Question DS-PRE-005

Your data set has missing values in several columns. Describe the options for dealing with them, how you would choose between them, and how preprocessing can cause data leakage.

Show the explanation

1. First understand why the data is missing

  • MCAR (missing completely at random): unrelated to anything, such as a random sensor glitch. Dropping rows is unbiased, but you lose data.
  • MAR (missing at random): depends on other observed columns. Example: older users skip the "income" field more often. Imputation that uses those columns works well.
  • MNAR (missing not at random): depends on the missing value itself. Example: people with very high incomes do not disclose them. This is hard to correct, and the fact that a value is missing is itself informative.

2. Options

  • Drop rows (dropna) when only a few are affected and the data is plausibly MCAR. Drop a column when most of it is empty and it is not important.
  • Simple imputation: median for skewed numbers, mean for symmetric ones, the most frequent category or a new "Unknown" category for categorical data.
  • Add a missing-indicator column (income_missing = 1) so the model can learn from the pattern.
  • Model-based imputation: KNN imputation, iterative imputation (each column predicted from the others).
  • Time series: forward fill or interpolation. Never fill backwards from future values in a forecasting problem.
  • Some models, such as gradient-boosted trees in XGBoost, LightGBM and scikit-learn's HistGradientBoosting, handle missing values natively.

3. Data leakage
Leakage happens when information that would not be available at prediction time gets into training. The model then looks great in evaluation and fails in production. Preprocessing causes it like this:

  • Computing the imputation median, scaling statistics or encodings on the whole data set before splitting. Test-set information then shapes the training features.
  • Filling values using future rows in time-ordered data.
  • Target leakage: features created from the label or after the outcome, such as "refund issued" when predicting fraud.

The fix: split first, then fit the preprocessing on the training data only and apply it to validation and test data. With cross-validation, put the imputer and scaler inside a scikit-learn Pipeline, so they are re-fitted within each fold.

Original UniCircle question. Last reviewed 22 Oct 2026.