Clip Outliers to IQR Fences

Quick Overview

Implement a pandas function that winsorizes a numeric column at the Tukey 1.5*IQR fences, capping out-of-range values at the nearest fence while keeping every row and the original index. It tests quartile computation with linear interpolation, Series.clip, and edge cases such as NaN values and a zero IQR.

Clip Outliers to IQR Fences

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `clip_to_iqr(df: pd.DataFrame, col: str) -> pd.Series` that winsorizes `df[col]` at the Tukey 1.5*IQR fences. Values outside the fences are capped to the nearest fence, and no rows are dropped. Requirements: - Compute the quartiles with `df[col].quantile(0.25)` and `df[col].quantile(0.75)` (default `interpolation='linear'`). - Set `IQR = Q3 - Q1`, `lower = Q1 - 1.5*IQR` and `upper = Q3 + 1.5*IQR`. - Return `df[col].clip(lower=lower, upper=upper)`, so values inside the fences come back unchanged. - Preserve the original index and row count. Example: ```python df = pd.DataFrame({"x": [10, 12, 11, 13, 100]}) clip_to_iqr(df, "x") # Q1=11, Q3=13, IQR=2 -> lower=8, upper=16 # 100 -> 16; the other values are unchanged # index: 0 1 2 3 4 # value: 10 12 11 13 16 ``` ```hint Check the fences by hand With five values and linear interpolation, the 0.25 and 0.75 quantiles land exactly on sorted positions 1 and 3 (counting from 0). Work out both fences for the example before writing code, then confirm that only one value moves. ``` ### What a Strong Answer Covers - Derives Q1, Q3 and both fences with pandas' default linear interpolation and checks them against the example. - Caps values element-wise so the index, row order and row count stay the same. - Explains what NaN values do to the quartiles and to the returned Series. - Handles degenerate inputs: a zero IQR, an empty or all-NaN column, and the dtype of a clipped integer column. ### Follow-up Questions - How would you clip several columns at once and return a DataFrame? - How should NaN values be handled? - When would you winsorize a feature instead of dropping the outlier rows? - If the fences are learned on a training set, how do you apply them to new data?

Overview: Implement a pandas function that winsorizes a numeric column at the Tukey 1.5*IQR fences, capping out-of-range values at the nearest fence while keeping every row and the original index. It tests quartile computation with linear interpolation, Series.clip, and edge cases such as NaN values and a zero IQR.

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

Implement clip_to_iqr(df: pd.DataFrame, col: str) -> pd.Series that winsorizes df[col] at the Tukey 1.5*IQR fences. Values outside the fences are capped to the nearest fence, and no rows are dropped.

Requirements:

  • Compute the quartiles with df[col].quantile(0.25) and df[col].quantile(0.75) (default interpolation='linear' ).
  • Set IQR = Q3 - Q1 , lower = Q1 - 1.5*IQR and upper = Q3 + 1.5*IQR .
  • Return df[col].clip(lower=lower, upper=upper) , so values inside the fences come back unchanged.
  • Preserve the original index and row count.

Example:

df = pd.DataFrame({"x": [10, 12, 11, 13, 100]})
clip_to_iqr(df, "x")
# Q1=11, Q3=13, IQR=2 -> lower=8, upper=16
# 100 -> 16; the other values are unchanged
# index:  0   1   2   3   4
# value: 10  12  11  13  16

What a Strong Answer Covers Guidance

  • Derives Q1, Q3 and both fences with pandas' default linear interpolation and checks them against the example.
  • Caps values element-wise so the index, row order and row count stay the same.
  • Explains what NaN values do to the quartiles and to the returned Series.
  • Handles degenerate inputs: a zero IQR, an empty or all-NaN column, and the dtype of a clipped integer column.

Follow-up Questions Guidance

  • How would you clip several columns at once and return a DataFrame?
  • How should NaN values be handled?
  • When would you winsorize a feature instead of dropping the outlier rows?
  • If the fences are learned on a training set, how do you apply them to new data?