Compute Tukey IQR Outlier Fences for a pandas Column

Quick Overview

Write a pandas function that returns the lower and upper Tukey outlier fences for a numeric column, using the quartiles and 1.5 times the interquartile range. It tests quantile interpolation, return types, and how the fences behave with missing values, constant columns and small samples.

Compute Tukey IQR Outlier Fences for a pandas Column

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `iqr_bounds(df: pd.DataFrame, col: str) -> tuple[float, float]` returning the Tukey outlier fences `(lower, upper)` for the numeric column `col`. Requirements: - `Q1 = df[col].quantile(0.25)` and `Q3 = df[col].quantile(0.75)` (default `interpolation='linear'`), with `IQR = Q3 - Q1`. - `lower = Q1 - 1.5*IQR` and `upper = Q3 + 1.5*IQR`. - Return each bound as a plain Python `float`. The result is graded with `numpy.testing.assert_allclose`. - Assume `col` is numeric. Handling NaN beyond the default behavior of pandas `quantile` is out of scope. Example: ```python df = pd.DataFrame({"x": [1, 2, 3, 4, 5]}) iqr_bounds(df, "x") # Q1 = 2.0, Q3 = 4.0, IQR = 2.0 # Output: (-1.0, 7.0) ``` ```hint Types and defaults Check which interpolation method pandas uses by default, and what type `quantile` actually returns before you build the tuple. ``` ### What a Strong Answer Covers - Q1 and Q3 from pandas `quantile` with linear interpolation. - The 1.5×IQR fence formula applied on both sides. - A tuple of plain Python floats in `(lower, upper)` order. - How the fences behave with NaN values, a constant column and very small samples. ### Follow-up Questions - How would the fences change with another interpolation method, such as `'midpoint'` or `'nearest'`? - Why is the multiplier 1.5, and when would you use 3×IQR or another rule? - How would you compute the fences for every numeric column at once, or for each group of a categorical column? - How do IQR fences compare with a mean plus or minus three standard deviations rule on data with a few huge values?

Overview: Write a pandas function that returns the lower and upper Tukey outlier fences for a numeric column, using the quartiles and 1.5 times the interquartile range. It tests quantile interpolation, return types, and how the fences behave with missing values, constant columns and small samples.

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

Implement iqr_bounds(df: pd.DataFrame, col: str) -> tuple[float, float] returning the Tukey outlier fences (lower, upper) for the numeric column col.

Requirements:

  • Q1 = df[col].quantile(0.25) and Q3 = df[col].quantile(0.75) (default interpolation='linear' ), with IQR = Q3 - Q1 .
  • lower = Q1 - 1.5*IQR and upper = Q3 + 1.5*IQR .
  • Return each bound as a plain Python float . The result is graded with numpy.testing.assert_allclose .
  • Assume col is numeric. Handling NaN beyond the default behavior of pandas quantile is out of scope.

Example:

df = pd.DataFrame({"x": [1, 2, 3, 4, 5]})
iqr_bounds(df, "x")
# Q1 = 2.0, Q3 = 4.0, IQR = 2.0
# Output: (-1.0, 7.0)

What a Strong Answer Covers Guidance

  • Q1 and Q3 from pandas quantile with linear interpolation.
  • The 1.5×IQR fence formula applied on both sides.
  • A tuple of plain Python floats in (lower, upper) order.
  • How the fences behave with NaN values, a constant column and very small samples.

Follow-up Questions Guidance

  • How would the fences change with another interpolation method, such as 'midpoint' or 'nearest' ?
  • Why is the multiplier 1.5, and when would you use 3×IQR or another rule?
  • How would you compute the fences for every numeric column at once, or for each group of a categorical column?
  • How do IQR fences compare with a mean plus or minus three standard deviations rule on data with a few huge values?