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
Situation
A 7-day rolling mean of sales is computed and joined to the same row as the target.
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.
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.
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
Pandas rolling windows are trailing by default and can be centred, which places later observations in the window.