Rolling Z-Score Outlier Detection
Problem
**[Asked at Tesla]**
Use a **5-row rolling window** (min_periods=3) to compute the rolling mean
and standard deviation, then flag rows where the absolute z-score
(`(value - mean) / std`) exceeds **1.5**.
Return columns: `date` (YYYY-MM-DD), `value`, `z_score` (rounded to 2 dp).
Order by date.
Input data
Example rows — the live problem includes the full dataset.
metrics
| date | value |
|---|---|
| 2023-01-01 | 100 |
| 2023-01-02 | 102 |
| 2023-01-03 | 98 |
| 2023-01-04 | 101 |
| 2023-01-05 | 99 |
Expected output
Your answer should return 1 row with the columns date, value, z_score.
Starter code (Pandas (Python))
import pandas as pd
def rolling_outliers(metrics: pd.DataFrame) -> pd.DataFrame:
# Your code here
return metricsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock