Flag outliers with a z-score mask using the sample standard deviation

Quick Overview

Write a pandas function that flags outliers in a column where the absolute z-score exceeds 3, using the sample standard deviation and a strict comparison. It tests ddof defaults, index-preserving boolean output, edge cases like constant columns, and why small samples and masking limit the three-sigma rule.

Flag outliers with a z-score mask using the sample standard deviation

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `zscore_mask_sample(df: pd.DataFrame, col: str) -> pd.Series` that flags outliers in `df[col]`. Compute `z = (x - mean) / std` and return a boolean mask that is `True` where `|z| > 3`. Requirements: - Use the sample standard deviation (`ddof=1`, the `Series.std()` default), not the population standard deviation. - The comparison is strict: `|z| == 3` is `False`. - Return a Series of dtype exactly `bool` with the same index as `df`. It is graded with `pandas.testing.assert_series_equal`. Examples: ```python df = pd.DataFrame({"x": [10, 11, 9, 10, 11, 9, 10, 11, 9, 10, 11, 9, 500]}) mask = zscore_mask_sample(df, "x") mask.dtype # dtype('bool') mask[mask].index.tolist() # [12] (z of the 500 is about 3.33) df_small = pd.DataFrame({"x": [10, 11, 9, 10, 500]}) zscore_mask_sample(df_small, "x").any() # False (z of the 500 is only about 1.79 in a sample of five) ``` ```hint Which standard deviation pandas and numpy use different defaults: `Series.std()` divides by `n - 1`, while `np.std` divides by `n`. Check which one your code actually calls. ``` ### What a Strong Answer Covers - Uses the sample standard deviation and the strict `>` comparison exactly as specified. - Returns a `bool` Series that keeps the original index and name, so `assert_series_equal` passes. - Says what happens with a constant column, missing values and very small samples. - Explains why one extreme value can stay unflagged, both in small samples and when several outliers distort the mean and standard deviation. ### Follow-up Questions - How does using `ddof=0` instead of `ddof=1` change which rows are flagged? - Why can a single extreme outlier hide others with this method, and what would you use instead? - How would you compute the same mask within groups, for example per user or per day?

Overview: Write a pandas function that flags outliers in a column where the absolute z-score exceeds 3, using the sample standard deviation and a strict comparison. It tests ddof defaults, index-preserving boolean output, edge cases like constant columns, and why small samples and masking limit the three-sigma rule.

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

Implement zscore_mask_sample(df: pd.DataFrame, col: str) -> pd.Series that flags outliers in df[col]. Compute z = (x - mean) / std and return a boolean mask that is True where |z| > 3.

Requirements:

  • Use the sample standard deviation ( ddof=1 , the Series.std() default), not the population standard deviation.
  • The comparison is strict: |z| == 3 is False .
  • Return a Series of dtype exactly bool with the same index as df . It is graded with pandas.testing.assert_series_equal .

Examples:

df = pd.DataFrame({"x": [10, 11, 9, 10, 11, 9, 10, 11, 9, 10, 11, 9, 500]})
mask = zscore_mask_sample(df, "x")
mask.dtype                    # dtype('bool')
mask[mask].index.tolist()     # [12]   (z of the 500 is about 3.33)

df_small = pd.DataFrame({"x": [10, 11, 9, 10, 500]})
zscore_mask_sample(df_small, "x").any()
# False   (z of the 500 is only about 1.79 in a sample of five)

What a Strong Answer Covers Guidance

  • Uses the sample standard deviation and the strict > comparison exactly as specified.
  • Returns a bool Series that keeps the original index and name, so assert_series_equal passes.
  • Says what happens with a constant column, missing values and very small samples.
  • Explains why one extreme value can stay unflagged, both in small samples and when several outliers distort the mean and standard deviation.

Follow-up Questions Guidance

  • How does using ddof=0 instead of ddof=1 change which rows are flagged?
  • Why can a single extreme outlier hide others with this method, and what would you use instead?
  • How would you compute the same mask within groups, for example per user or per day?