Masking Diagnostic for the Z-Score Outlier Rule in pandas

Quick Overview

Write a pandas diagnostic that runs a three-sigma z-score outlier screen on a column twice, once on all values and once without the most extreme point, and reports both results side by side. It tests population standard deviation, exact tie-breaking on the index and an understanding of outlier masking.

Masking Diagnostic for the Z-Score Outlier Rule in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `masking_report(df, col)` to expose masking: extreme points inflate the standard deviation, so the z-score rule misses outliers. Run the z-score screen on the non-null values of numeric column `col` twice: scenario `'full'` on all values, and `'minus_top1'` with the single most extreme point removed. Return a summary DataFrame. Requirements: - Flag a value when `abs((x - mean) / std) > 3.0`, using population std (`ddof=0`, not the `Series.std()` default `ddof=1`). - The most extreme point is the one with the largest `abs(x - mean)` in the full data. On ties, remove the point with the smallest original index. - Columns are exactly `['scenario', 'n', 'mean', 'std', 'n_flagged']`, with two rows in order: `'full'`, then `'minus_top1'`. - `n` and `n_flagged` are `int64`, `mean` and `std` are `float64`, all recomputed on each scenario's own values. - Never return the reduced dataset. The removal exists only to re-run the diagnostic. - Assume `col` exists and is numeric. Ignore NaN values everywhere. The function is a pure diagnostic and persists nothing. Example: ```python df = pd.DataFrame({"x": [0] * 10 + [10, 20, np.nan]}) masking_report(df, "x") ``` ```text scenario n mean std n_flagged 0 full 12 2.5000 5.9512 0 1 minus_top1 11 0.9091 2.8748 1 ``` With all 12 values, the 20 stretches the std enough that its own z-score is only about 2.94 and the 10 sits near 1.26. Removing the 20 shrinks the std to about 2.87, and the 10 now has a z-score of about 3.16. ```hint One screen, two inputs Write a single helper that takes a set of values and returns n, mean, population std and the flag count, and recompute all of them inside it for each scenario. ``` ```hint Watch the tie rule Check what `idxmax` returns when the index is unsorted, and what `drop` does when an index label appears more than once. ``` ### Clarifying Questions to Ask - What should happen when the column has no non-null values, or only one? - If every value is the same, the std is 0 and the z-scores are undefined. Should those count as not flagged? - Can the DataFrame index be unsorted or contain duplicate labels, and does "smallest original index" mean the smallest label? ### What a Strong Answer Covers - Population std, with mean and std recomputed on each scenario's own values. - Picking the point to remove from deviations on the full data, with the smallest-index tie rule implemented exactly. - The output contract: column order, row order, dtypes, and an input DataFrame left untouched. - Edge cases such as zero std, very small n, and an empty column. - Why the flag count can rise after removing a point, in terms of how the std is computed. ### Follow-up Questions - Why does removing a single point make the rule flag more outliers? - How would MAD-based scores avoid this, and where do they break down? - When several outliers hide each other, how would you extend the diagnostic beyond dropping one point?

Overview: Write a pandas diagnostic that runs a three-sigma z-score outlier screen on a column twice, once on all values and once without the most extreme point, and reports both results side by side. It tests population standard deviation, exact tie-breaking on the index and an understanding of outlier masking.

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

Write masking_report(df, col) to expose masking: extreme points inflate the standard deviation, so the z-score rule misses outliers. Run the z-score screen on the non-null values of numeric column col twice: scenario 'full' on all values, and 'minus_top1' with the single most extreme point removed. Return a summary DataFrame.

Requirements:

  • Flag a value when abs((x - mean) / std) > 3.0 , using population std ( ddof=0 , not the Series.std() default ddof=1 ).
  • The most extreme point is the one with the largest abs(x - mean) in the full data. On ties, remove the point with the smallest original index.
  • Columns are exactly ['scenario', 'n', 'mean', 'std', 'n_flagged'] , with two rows in order: 'full' , then 'minus_top1' .
  • n and n_flagged are int64 , mean and std are float64 , all recomputed on each scenario's own values.
  • Never return the reduced dataset. The removal exists only to re-run the diagnostic.
  • Assume col exists and is numeric. Ignore NaN values everywhere. The function is a pure diagnostic and persists nothing.

Example:

df = pd.DataFrame({"x": [0] * 10 + [10, 20, np.nan]})
masking_report(df, "x")
     scenario   n    mean     std  n_flagged
0        full  12  2.5000  5.9512          0
1  minus_top1  11  0.9091  2.8748          1

With all 12 values, the 20 stretches the std enough that its own z-score is only about 2.94 and the 10 sits near 1.26. Removing the 20 shrinks the std to about 2.87, and the 10 now has a z-score of about 3.16.

Clarifying Questions to Ask Guidance

  • What should happen when the column has no non-null values, or only one?
  • If every value is the same, the std is 0 and the z-scores are undefined. Should those count as not flagged?
  • Can the DataFrame index be unsorted or contain duplicate labels, and does "smallest original index" mean the smallest label?

What a Strong Answer Covers Guidance

  • Population std, with mean and std recomputed on each scenario's own values.
  • Picking the point to remove from deviations on the full data, with the smallest-index tie rule implemented exactly.
  • The output contract: column order, row order, dtypes, and an input DataFrame left untouched.
  • Edge cases such as zero std, very small n, and an empty column.
  • Why the flag count can rise after removing a point, in terms of how the std is computed.

Follow-up Questions Guidance

  • Why does removing a single point make the rule flag more outliers?
  • How would MAD-based scores avoid this, and where do they break down?
  • When several outliers hide each other, how would you extend the diagnostic beyond dropping one point?