IQR Outlier Boolean Mask

Quick Overview

Implement a pandas function that returns a boolean mask marking the rows of a numeric column that fall strictly outside the Tukey 1.5*IQR fences. It tests quartile computation, strict comparisons, and keeping the index and Series name intact for an exact equality check.

IQR Outlier Boolean Mask

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `iqr_outlier_mask(df: pd.DataFrame, col: str) -> pd.Series` returning a boolean mask that is `True` exactly at IQR-outlier rows of numeric column `col`. A row is an outlier when `value < Q1 - 1.5*IQR` or `value > Q3 + 1.5*IQR`. Requirements: - `Q1 = df[col].quantile(0.25)`, `Q3 = df[col].quantile(0.75)` (default `interpolation='linear'`), `IQR = Q3 - Q1`. - Use strict comparisons: values equal to a fence are not outliers. - Return a `pd.Series` of dtype `bool` with the same index as `df` and the column's name. The result is graded with `pandas.testing.assert_series_equal` and `check_names=True`. Example: ```python df = pd.DataFrame({"x": [10, 12, 11, 13, 100]}) iqr_outlier_mask(df, "x") # Q1=11, Q3=13, IQR=2 -> fences [8, 16] # 0 False # 1 False # 2 False # 3 False # 4 True # Name: x, dtype: bool ``` ```hint Index and name The grader checks the index and the Series name as well as the values. Some ways of building a boolean array, for example going through NumPy and wrapping the result in a new Series, lose one or both. ``` ### What a Strong Answer Covers - Derives Q1, Q3 and both fences with linear interpolation, checked against the example. - Uses strict comparisons on both sides, so values on a fence stay False. - Returns a bool Series with the original index and the column name, and explains why the operations used keep them. - Explains what NaN values and a zero IQR do to the mask. ### Follow-up Questions - How would you generalize the fence multiplier `1.5` into a parameter? - How should `NaN` values in the column be handled? - How would you use the mask to drop the outlier rows, or to flag them in a new column?

Overview: Implement a pandas function that returns a boolean mask marking the rows of a numeric column that fall strictly outside the Tukey 1.5*IQR fences. It tests quartile computation, strict comparisons, and keeping the index and Series name intact for an exact equality check.

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

Implement iqr_outlier_mask(df: pd.DataFrame, col: str) -> pd.Series returning a boolean mask that is True exactly at IQR-outlier rows of numeric column col. A row is an outlier when value < Q1 - 1.5*IQR or value > Q3 + 1.5*IQR.

Requirements:

  • Q1 = df[col].quantile(0.25) , Q3 = df[col].quantile(0.75) (default interpolation='linear' ), IQR = Q3 - Q1 .
  • Use strict comparisons: values equal to a fence are not outliers.
  • Return a pd.Series of dtype bool with the same index as df and the column's name. The result is graded with pandas.testing.assert_series_equal and check_names=True .

Example:

df = pd.DataFrame({"x": [10, 12, 11, 13, 100]})
iqr_outlier_mask(df, "x")
# Q1=11, Q3=13, IQR=2 -> fences [8, 16]
# 0    False
# 1    False
# 2    False
# 3    False
# 4     True
# Name: x, dtype: bool

What a Strong Answer Covers Guidance

  • Derives Q1, Q3 and both fences with linear interpolation, checked against the example.
  • Uses strict comparisons on both sides, so values on a fence stay False.
  • Returns a bool Series with the original index and the column name, and explains why the operations used keep them.
  • Explains what NaN values and a zero IQR do to the mask.

Follow-up Questions Guidance

  • How would you generalize the fence multiplier 1.5 into a parameter?
  • How should NaN values in the column be handled?
  • How would you use the mask to drop the outlier rows, or to flag them in a new column?