Quantify Outlier Impact on Headline Stats
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `outlier_impact(df, col)` to quantify how outliers in the numeric column `col` distort headline statistics. Outliers are non-null values strictly outside `[Q1 - 1.5*IQR, Q3 + 1.5*IQR]`, with `Q1` and `Q3` from the default `interpolation='linear'` and `IQR = Q3 - Q1`. The trimmed data is never returned.
The output must follow these rules:
- Return a `float64` Series whose index is exactly `['mean_with', 'mean_without', 'mean_pct_shift', 'median_with', 'median_without', 'median_pct_shift', 'std_with', 'std_without', 'n_outliers']`.
- `*_with` statistics use all non-null values, and `*_without` statistics exclude the outliers.
- `*_pct_shift = 100 * (with - without) / abs(without)`, rounded to 2 decimal places.
- All standard deviations use `ddof=1`.
- `n_outliers` is the outlier count cast to float.
For example, with `col` values `[1, 2, 3, 4, 100]`, the quartiles are 2 and 4, the upper fence is 7, and 100 is the only outlier. The expected Series is below; the two standard deviations are shown rounded to 4 decimals, and the function returns them unrounded.
```text
mean_with 22.0
mean_without 2.5
mean_pct_shift 780.0
median_with 3.0
median_without 2.5
median_pct_shift 20.0
std_with 43.6177
std_without 1.2910
n_outliers 1.0
```
One extreme value moves the mean by 780% and the median by 20%.
```hint Sign of the shift
Think about why the formula divides by `abs(without)`, and what the sign of `*_pct_shift` should tell a reader when the trimmed statistic is negative.
```
### What a Strong Answer Covers
- How the quartiles, IQR and fences are computed, and how a value exactly on a fence is treated
- Mean, median and sample standard deviation computed on both the full and the trimmed values
- The shift formula, why its denominator is an absolute value, and the rounding
- A `float64` Series in the exact index order, with the trimmed data kept internal
- Degenerate inputs: a zero trimmed statistic, an IQR of 0, very few non-null values
### Follow-up Questions
- What should happen when a `without` statistic is 0 and the shift has a zero denominator?
- How would you make the fence multiplier configurable without breaking existing callers?
- Why does the median move so much less than the mean in the example, and when would you report it as the headline number?
Overview: Write a pandas function that measures how Tukey IQR outliers in one column change its mean, median and standard deviation. It tests quartile fences with strict boundaries, signed percentage shifts with rounding, and returning a float Series with a fixed index.