Flag outliers with the MAD-based modified z-score in pandas

Quick Overview

Implement a pandas function that returns a boolean outlier mask using the median, the median absolute deviation and the Iglewicz-Hoaglin modified z-score. It tests vectorized column math, exact dtype and index handling, and reasoning about a zero MAD.

Flag outliers with the MAD-based modified z-score in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `mad_outlier_mask(df: pd.DataFrame, col: str) -> pd.Series`, which flags outliers in `df[col]` using the Iglewicz-Hoaglin modified z-score. Return a boolean mask where `True` marks an outlier. Requirements: - `med = df[col].median()` and `MAD = (df[col] - med).abs().median()`. - `modified_z = 0.6745 * (x - med) / MAD` for each row. - Flag rows where `|modified_z| > 3.5`. The comparison is strict: a score of exactly 3.5 is not flagged. - Return a Series of dtype `bool` with the same index as `df`. It is graded with `pandas.testing.assert_series_equal`. Example: ```python df = pd.DataFrame({"x": [10, 11, 9, 10, 100]}) mad_outlier_mask(df, "x") # 0 False # 1 False # 2 False # 3 False # 4 True # Name: x, dtype: bool ``` The median is 10 and the absolute deviations are 0, 1, 1, 0 and 90, so `MAD` is 1. Only the last row scores above 3.5. ```hint What the grader compares `assert_series_equal` checks the dtype, the index and the Series name as well as the values. Look at what a comparison on a Series already gives you before adding conversions. ``` ```hint A degenerate column Work out what `MAD` is when more than half the values in the column are equal, and what the division then produces for each row. ``` ### Constraints & Assumptions - `df[col]` is numeric. - The constants are fixed: 0.6745 for the scale and 3.5 for the cutoff. - The returned Series has dtype `bool`, the index of `df`, and the column name, as in the example. ### What a Strong Answer Covers - A vectorized implementation of the median, `MAD` and modified z-score exactly as specified. - The strict `>` threshold applied to the absolute score. - A result whose dtype, index and name match what `assert_series_equal` expects. - What the mask does when `MAD` is 0, and a reasoned way to handle that case. - Why the median and `MAD` hold up against outliers better than the mean and standard deviation. ### Follow-up Questions - What happens when `MAD` is 0, and how would you handle it? - How does this compare to a standard z-score for outlier detection? - Where do the constants 0.6745 and 3.5 come from, and when would you change the cutoff? - How would you compute the same mask separately within each group, for example per store?

Overview: Implement a pandas function that returns a boolean outlier mask using the median, the median absolute deviation and the Iglewicz-Hoaglin modified z-score. It tests vectorized column math, exact dtype and index handling, and reasoning about a zero MAD.

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

Implement mad_outlier_mask(df: pd.DataFrame, col: str) -> pd.Series, which flags outliers in df[col] using the Iglewicz-Hoaglin modified z-score. Return a boolean mask where True marks an outlier.

Requirements:

  • med = df[col].median() and MAD = (df[col] - med).abs().median() .
  • modified_z = 0.6745 * (x - med) / MAD for each row.
  • Flag rows where |modified_z| > 3.5 . The comparison is strict: a score of exactly 3.5 is not flagged.
  • Return a Series of dtype bool with the same index as df . It is graded with pandas.testing.assert_series_equal .

Example:

df = pd.DataFrame({"x": [10, 11, 9, 10, 100]})
mad_outlier_mask(df, "x")
# 0    False
# 1    False
# 2    False
# 3    False
# 4     True
# Name: x, dtype: bool

The median is 10 and the absolute deviations are 0, 1, 1, 0 and 90, so MAD is 1. Only the last row scores above 3.5.

Constraints & Assumptions

  • df[col] is numeric.
  • The constants are fixed: 0.6745 for the scale and 3.5 for the cutoff.
  • The returned Series has dtype bool , the index of df , and the column name, as in the example.

What a Strong Answer Covers Guidance

  • A vectorized implementation of the median, MAD and modified z-score exactly as specified.
  • The strict > threshold applied to the absolute score.
  • A result whose dtype, index and name match what assert_series_equal expects.
  • What the mask does when MAD is 0, and a reasoned way to handle that case.
  • Why the median and MAD hold up against outliers better than the mean and standard deviation.

Follow-up Questions Guidance

  • What happens when MAD is 0, and how would you handle it?
  • How does this compare to a standard z-score for outlier detection?
  • Where do the constants 0.6745 and 3.5 come from, and when would you change the cutoff?
  • How would you compute the same mask separately within each group, for example per store?