Replace IQR Outliers With Median

Quick Overview

Write a pandas function that flags 1.5×IQR outliers in a column and replaces them with the median of the original data, returning a Series with the same index. It tests quantile fences, masking with where and fillna, and side effects on missing values and dtypes.

Replace IQR Outliers With Median

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `outliers_to_median(df: pd.DataFrame, col: str) -> pd.Series` that detects IQR outliers in `df[col]`, replaces them with the column median, and returns the cleaned Series. Requirements: - Compute `Q1` and `Q3` with `interpolation='linear'`, and set `IQR = Q3 - Q1`. - A value is an outlier when `value < Q1 - 1.5*IQR` or `value > Q3 + 1.5*IQR`. - Take `med = df[col].median()` from the original data, before any replacement. - Return `df[col].where(~mask).fillna(med)` as a `pd.Series`. It must keep the original index and every row. - Do not mutate the input. - The result is graded with `numpy.testing.assert_allclose`, so float tolerance applies. Example: ```text df[col] = [10, 12, 11, 13, 999] The quartiles flag 999 as an outlier, and the median of the original column is 12. Output: [10, 12, 11, 13, 12] ``` ```hint Side effects of the fill Think about what `fillna` does to values that were already missing before you masked anything, and what dtype an integer column has after NaN passes through it. ``` ### What a Strong Answer Covers - Quartiles with linear interpolation and strict 1.5×IQR fences. - A median taken from the original column, before any value is replaced. - A Series with the original index, name and length, built without touching `df`. - The side effects of `where(...).fillna(...)` on values that were already missing and on an integer column's dtype. ### Follow-up Questions - Why is the median taken from the original column, and how would the fill value change if it were computed after the outliers were removed? - How would you change this to cap outliers at the fences? - How would you replace each outlier with the median of its own group? - When is replacing outliers with the median a bad idea for a downstream model?

Overview: Write a pandas function that flags 1.5×IQR outliers in a column and replaces them with the median of the original data, returning a Series with the same index. It tests quantile fences, masking with where and fillna, and side effects on missing values and dtypes.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write outliers_to_median(df: pd.DataFrame, col: str) -> pd.Series that detects IQR outliers in df[col], replaces them with the column median, and returns the cleaned Series.

Requirements:

  • Compute Q1 and Q3 with interpolation='linear' , and set IQR = Q3 - Q1 .
  • A value is an outlier when value < Q1 - 1.5*IQR or value > Q3 + 1.5*IQR .
  • Take med = df[col].median() from the original data, before any replacement.
  • Return df[col].where(~mask).fillna(med) as a pd.Series . It must keep the original index and every row.
  • Do not mutate the input.
  • The result is graded with numpy.testing.assert_allclose , so float tolerance applies.

Example:

df[col] = [10, 12, 11, 13, 999]
The quartiles flag 999 as an outlier, and the median of the original column is 12.
Output: [10, 12, 11, 13, 12]

What a Strong Answer Covers Guidance

  • Quartiles with linear interpolation and strict 1.5×IQR fences.
  • A median taken from the original column, before any value is replaced.
  • A Series with the original index, name and length, built without touching df .
  • The side effects of where(...).fillna(...) on values that were already missing and on an integer column's dtype.

Follow-up Questions Guidance

  • Why is the median taken from the original column, and how would the fill value change if it were computed after the outliers were removed?
  • How would you change this to cap outliers at the fences?
  • How would you replace each outlier with the median of its own group?
  • When is replacing outliers with the median a bad idea for a downstream model?