Skip to content
ML Academy

Time series and forecasting · insight-0241

Shift before rolling so window features exclude the target period

Evidence type
documented fact
Difficulty
easy
Roles
Data scientist, Data analyst, ML engineer
Industries
cross-industry

Summary: situation, action, reason, boundary

  1. Situation

    A 7-day rolling mean of sales is computed and joined to the same row as the target.

  2. Action

    Group by series, shift by at least the horizon, then compute trailing windows; assert in a unit test that each feature uses only timestamps before the origin.

  3. Reason

    Pandas rolling windows end at the current row by default, and centred windows include later rows, so a shift is needed to respect the origin.

  4. Boundary

    All lag and rolling features in pandas, SQL window functions and feature stores.

Symptom or risk

The window includes the target day itself, or centred windows include future days, so the model learns from the answer.

Exceptions

Future-known variables such as scheduled capacity may be aligned to the target row.

Trade-offs

Larger shifts reduce recency for short horizons; build features per horizon if needed.

Pitfalls

Rolling across series boundaries when the frame is not grouped by series.

Lessons and concepts

Claims and their sources