Compute a 3-day calendar rolling error rate per model with gaps in the data

HardpandasPro

Monitoring dashboards smooth noisy daily error rates with a rolling mean. When some days are missing from the log, a window of 3 rows is not a window of 3 days, and that difference hides or invents incidents.

Write rolling_errors(df) where df has columns model (str), day ('YYYY-MM-DD' string) and error_rate (float), at most one row per (model, day), rows in arbitrary order, and some days missing.

Return a DataFrame with columns exactly [model, day, rolling] sorted by model then day, plain 0..n-1 index. day stays a 'YYYY-MM-DD' string. rolling is the mean of that model's error_rate over rows whose day is within the 3 calendar days ending at this row's day (this day and the 2 days before it). Rows of other models never mix in.

Signaturedef rolling_errors(df)

Example

model 'a' with 2026-01-01: 0.1, 2026-01-02: 0.3, 2026-01-05: 0.5 gives rolling 0.1, 0.2, 0.5 (the 5th has no data on the 3rd or 4th).

Understand the idea

A rolling mean over 3 rows is not the same as a rolling mean over 3 days when some days are missing from the log. A row-based window reaches further back across gaps and can hide or invent an incident. A time-based window looks only at rows whose date falls within the last 3 calendar days, so after a gap it simply holds fewer rows. Worked example for model a: 01-01: 0.2, 01-03: 0.4, 01-04: 0.6. On 01-01 the window holds only itself: 0.2. On 01-03 the window is 01-01 to 01-03, holding 0.2 and 0.4: mean 0.3. On 01-04 the window is 01-02 to 01-04, holding 0.4 and 0.6: mean 0.5. A 3-row window would have given (0.2 + 0.4 + 0.6) / 3 = 0.4 there. Rule: convert day to a datetime, sort by model then day, and for each model separately take a '3D' time window mean ending at each row (this day and the 2 days before), so models never mix; then turn day back into a 'YYYY-MM-DD' string and output [model, day, rolling] with a plain index. Used in model monitoring dashboards.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: examplerolling_errors(_d1()).to_dict('list')
  • Sample 2: two models, unsortedrolling_errors(_d2()).to_dict('list')