Count Tukey outlier severity buckets (inside, mild, extreme) for a pandas column

Quick Overview

Implement a pandas function that sorts every non-null value of a numeric column into inside, mild and extreme buckets using Tukey's inner and outer fences, and returns the counts as a fixed-order Series. It tests quartile computation, inclusive and strict boundary rules and a complete, zero-filled output.

Count Tukey outlier severity buckets (inside, mild, extreme) for a pandas column

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `outlier_severity_counts(df, col)` that classifies every non-null value of the numeric column `col` into Tukey severity buckets and returns the counts. With `IQR = Q3 - Q1`, the inner fences are `Q1 - 1.5*IQR` and `Q3 + 1.5*IQR`, and the outer fences are `Q1 - 3.0*IQR` and `Q3 + 3.0*IQR`. Both multipliers are fixed. Requirements: - `'inside'`: within the inner fences (inclusive). - `'mild'`: outside an inner fence but within the corresponding outer fence (inclusive). - `'extreme'`: outside an outer fence. - Return an `int64` Series named `'n'` with the index order `['inside', 'mild', 'extreme']`. - The counts sum to the non-null count, and empty buckets appear with `0`. Example: ```python df = pd.DataFrame({"value": [12, 30, None, 4, 10, 26, 13, -5, 14, 11]}) outlier_severity_counts(df, "value") # inside 6 # mild 1 # extreme 2 # Name: n, dtype: int64 ``` The nine non-null values give `Q1 = 10`, `Q3 = 14` and `IQR = 4`, so the inner fences are 4 and 20 and the outer fences are -2 and 26. The value 4 sits on an inner fence and counts as inside, 26 sits on an outer fence and counts as mild, and -5 and 30 are extreme. ```hint Test the boundaries Decide which bucket a value exactly on an inner fence and a value exactly on an outer fence belong to, then make each comparison match that. ``` ### Constraints & Assumptions - `df` is a pandas DataFrame and `col` is numeric, possibly with `NaN`. - Both fence pairs use the same IQR, and the quartiles use the pandas default `interpolation='linear'`. ### What a Strong Answer Covers - Which values the quartiles are computed from, and with which interpolation. - The inclusive and strict comparisons at each fence, and where a value exactly on a fence lands. - Why the three buckets never overlap and together cover every non-null value. - The output contract: `int64` dtype, the name `'n'`, the fixed index order and zero-filled empty buckets. - Edge cases such as an all-null column or an IQR of zero, and the time and space cost. ### Follow-up Questions - How does the choice of quartile interpolation method affect the fences? - What happens to the buckets when the IQR is zero, and how would you report that column? - How would you produce the same census for each group of a categorical column, such as per country?

Overview: Implement a pandas function that sorts every non-null value of a numeric column into inside, mild and extreme buckets using Tukey's inner and outer fences, and returns the counts as a fixed-order Series. It tests quartile computation, inclusive and strict boundary rules and a complete, zero-filled output.

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

Implement outlier_severity_counts(df, col) that classifies every non-null value of the numeric column col into Tukey severity buckets and returns the counts. With IQR = Q3 - Q1, the inner fences are Q1 - 1.5*IQR and Q3 + 1.5*IQR, and the outer fences are Q1 - 3.0*IQR and Q3 + 3.0*IQR. Both multipliers are fixed.

Requirements:

  • 'inside' : within the inner fences (inclusive).
  • 'mild' : outside an inner fence but within the corresponding outer fence (inclusive).
  • 'extreme' : outside an outer fence.
  • Return an int64 Series named 'n' with the index order ['inside', 'mild', 'extreme'] .
  • The counts sum to the non-null count, and empty buckets appear with 0 .

Example:

df = pd.DataFrame({"value": [12, 30, None, 4, 10, 26, 13, -5, 14, 11]})
outlier_severity_counts(df, "value")
# inside     6
# mild       1
# extreme    2
# Name: n, dtype: int64

The nine non-null values give Q1 = 10, Q3 = 14 and IQR = 4, so the inner fences are 4 and 20 and the outer fences are -2 and 26. The value 4 sits on an inner fence and counts as inside, 26 sits on an outer fence and counts as mild, and -5 and 30 are extreme.

Constraints & Assumptions

  • df is a pandas DataFrame and col is numeric, possibly with NaN .
  • Both fence pairs use the same IQR, and the quartiles use the pandas default interpolation='linear' .

What a Strong Answer Covers Guidance

  • Which values the quartiles are computed from, and with which interpolation.
  • The inclusive and strict comparisons at each fence, and where a value exactly on a fence lands.
  • Why the three buckets never overlap and together cover every non-null value.
  • The output contract: int64 dtype, the name 'n' , the fixed index order and zero-filled empty buckets.
  • Edge cases such as an all-null column or an IQR of zero, and the time and space cost.

Follow-up Questions Guidance

  • How does the choice of quartile interpolation method affect the fences?
  • What happens to the buckets when the IQR is zero, and how would you report that column?
  • How would you produce the same census for each group of a categorical column, such as per country?