Build a Tukey fence sensitivity table across IQR multipliers in pandas

Quick Overview

Implement a pandas function that sweeps the Tukey fence multiplier from 1.0 to 3.0 and reports, for each step, the fences, the outlier count, the outlier percentage and the change from the previous step. It tests quartile handling with nulls, strict boundary comparisons and dtype control in the result.

Build a Tukey fence sensitivity table across IQR multipliers in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write a pandas function `fence_sensitivity(df, col)` that reports how the Tukey outlier count for one numeric column changes as the fence multiplier grows. Sweep the multipliers `[1.0, 1.5, 2.0, 2.5, 3.0]` in that exact order. For each multiplier `k`, compute the fences `Q1 - k*IQR` and `Q3 + k*IQR`, with the quartiles taken at the default `interpolation='linear'`, and return the outlier-count sensitivity table with one row per multiplier. The returned DataFrame must meet these requirements: - Columns are exactly `['multiplier', 'lower_fence', 'upper_fence', 'n_outliers', 'pct_outliers', 'marginal_drop']`, rows are in sweep order, and the index is reset with `reset_index(drop=True)`. - `n_outliers` is the count of values strictly outside that `k`'s fences. - `pct_outliers` is `100 * n_outliers / (count of non-null values)`, rounded to 2 decimal places. - `marginal_drop` is the previous multiplier's `n_outliers` minus this row's, with `0` in the first row. It is `int64` with no `NaN`. - Nulls are excluded from the quartiles and from the non-null count. ```hint What changes between rows Only `k` moves from one row to the next. Work out which quantities are fixed for the whole column and compute them once. ``` ```hint Watch the last column's dtype A quick `diff()` gives the wrong sign, puts `NaN` in the first row and turns the column into floats. Decide how you will get a first row of `0` and keep it `int64`. ``` ### Constraints & Assumptions - `df` is a pandas DataFrame and `col` holds numeric values, possibly with `NaN`. - The sweep is fixed at five multipliers, so the number of passes does not grow with the data. - Quartiles use the default linear interpolation, so `Q1` and `Q3` can fall between observed values. ### Clarifying Questions to Ask - What should `pct_outliers` be when `col` has no non-null values and the division is undefined? - Is `col` guaranteed to be numeric, or can it hold strings that need coercion first? - Should the fences be returned as floats even when the column holds integers? ### What a Strong Answer Covers - Which values the quartiles are computed from, with which interpolation, and why they are computed only once. - How a value that sits exactly on a fence is treated. - The denominator and the rounding used for `pct_outliers`. - How `marginal_drop` gets its first row of `0` and stays `int64`, along with the exact column order and the reset index. - Edge cases such as an all-null column or an IQR of zero, and the time and space cost. ### Follow-up Questions - How would you extend this to report a recommended multiplier automatically? - Why count values strictly outside the fences instead of using inclusive bounds? - The column is heavily right-skewed. Would you still trust Tukey fences, and what would you try instead?

Overview: Implement a pandas function that sweeps the Tukey fence multiplier from 1.0 to 3.0 and reports, for each step, the fences, the outlier count, the outlier percentage and the change from the previous step. It tests quartile handling with nulls, strict boundary comparisons and dtype control in the result.

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

Write a pandas function fence_sensitivity(df, col) that reports how the Tukey outlier count for one numeric column changes as the fence multiplier grows. Sweep the multipliers [1.0, 1.5, 2.0, 2.5, 3.0] in that exact order. For each multiplier k, compute the fences Q1 - k*IQR and Q3 + k*IQR, with the quartiles taken at the default interpolation='linear', and return the outlier-count sensitivity table with one row per multiplier.

The returned DataFrame must meet these requirements:

  • Columns are exactly ['multiplier', 'lower_fence', 'upper_fence', 'n_outliers', 'pct_outliers', 'marginal_drop'] , rows are in sweep order, and the index is reset with reset_index(drop=True) .
  • n_outliers is the count of values strictly outside that k 's fences.
  • pct_outliers is 100 * n_outliers / (count of non-null values) , rounded to 2 decimal places.
  • marginal_drop is the previous multiplier's n_outliers minus this row's, with 0 in the first row. It is int64 with no NaN .
  • Nulls are excluded from the quartiles and from the non-null count.

Constraints & Assumptions

  • df is a pandas DataFrame and col holds numeric values, possibly with NaN .
  • The sweep is fixed at five multipliers, so the number of passes does not grow with the data.
  • Quartiles use the default linear interpolation, so Q1 and Q3 can fall between observed values.

Clarifying Questions to Ask Guidance

  • What should pct_outliers be when col has no non-null values and the division is undefined?
  • Is col guaranteed to be numeric, or can it hold strings that need coercion first?
  • Should the fences be returned as floats even when the column holds integers?

What a Strong Answer Covers Guidance

  • Which values the quartiles are computed from, with which interpolation, and why they are computed only once.
  • How a value that sits exactly on a fence is treated.
  • The denominator and the rounding used for pct_outliers .
  • How marginal_drop gets its first row of 0 and stays int64 , along with the exact column order and the reset index.
  • Edge cases such as an all-null column or an IQR of zero, and the time and space cost.

Follow-up Questions Guidance

  • How would you extend this to report a recommended multiplier automatically?
  • Why count values strictly outside the fences instead of using inclusive bounds?
  • The column is heavily right-skewed. Would you still trust Tukey fences, and what would you try instead?