Dataset-wide outlier triage report with Tukey fences in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write a function `outlier_report(df)` that takes a pandas DataFrame and returns one row per numeric column describing that column's outliers. Select the numeric columns with `df.select_dtypes(include='number')` and ignore every other column. Flag outliers with Tukey fences, `lower = Q1 - 1.5 * IQR` and `upper = Q3 + 1.5 * IQR`, where `IQR = Q3 - Q1` and the quartiles come from `Series.quantile` with `interpolation='linear'` (the default).
The returned DataFrame must follow these rules:
- The columns are exactly `['column', 'lower_fence', 'upper_fence', 'n_outliers', 'pct_outliers']`.
- A value is an outlier if and only if it is strictly below `lower_fence` or strictly above `upper_fence`. NaN values are never outliers, and they are left out of the denominator.
- `n_outliers` has dtype `int64`. `pct_outliers` is `100 * n_outliers / (non-null count)`, rounded to 2 decimals.
- Rows are sorted by `pct_outliers` descending, then by `column` ascending, and the index is reset with `reset_index(drop=True)`.
- A column with zero non-null values must not cause a division by zero. Choose the `pct_outliers` value for that case and document it.
Example:
```python
df = pd.DataFrame({
'a': [1, 2, 3, 4, 100], # 100 is a high outlier
'b': [10, 11, 12, 13, 14],
'name': ['w', 'x', 'y', 'z', 'q'], # not numeric, ignored
})
outlier_report(df)
```
```text
column lower_fence upper_fence n_outliers pct_outliers
0 a -1.0 7.0 1 20.0
1 b 8.0 16.0 0 0.0
```
For `a`, the quartiles are 2 and 4, so the IQR is 2 and the fences are -1 and 7. Only 100 falls outside them, which is 1 of 5 values. For `b`, the quartiles are 11 and 13, the fences are 8 and 16, and every value lies inside.
```hint Missing values
Check what `Series.quantile` and the comparison operators do with NaN, and make sure the count you divide by covers exactly the values you compared.
```
### Constraints & Assumptions
- `df` mixes numeric and non-numeric columns. Only the numeric ones appear in the output.
- Numeric columns may be integer or float and may contain NaN. A column may have no non-null values at all.
- The fence multiplier is fixed at 1.5.
### What a Strong Answer Covers
- Restricting the work to numeric columns and computing linear-interpolated quartiles and fences for each one.
- Strict comparisons against both fences, with NaN kept out of the outlier count and out of the denominator.
- A defined and documented result for a column with no non-null values, with no division by zero.
- The exact output schema and dtypes, the two-key sort and the index reset.
- Edge cases such as a constant column (an IQR of zero) and a DataFrame with no numeric columns.
### Follow-up Questions
- How would you make the fence multiplier configurable while keeping 1.5 as the default?
- IQR-based fences can misbehave on skewed distributions. What would you add so that naturally skewed columns do not dominate the triage?
- How would you compute the same report without a Python loop over the columns?
Overview: Write a pandas function that builds a per-column outlier report from Tukey fences on linear-interpolated quartiles. It tests careful handling of missing values, strict fence comparisons, empty columns without division by zero, exact output dtypes and a deterministic two-key sort.