A rolling window selects the most recent fixed-size or fixed-duration observations, then moves forward as new observations arrive. It keeps comparisons focused on data likely to remain comparable. section 4.14 section 12.9
For outlier detection, a rolling window can define the data used to fit models or create features. Financial audits may compare the previous three to five years; purchase analysis may compare each unit cost with the previous ten records. section 4.14 section 12.9
Interpret rolling-window results with these practical constraints:
- Sort records in the relevant temporal order before calculating a window. section 12.9
- Expect missing values for the initial records when the full window has not yet been filled. section 12.9
- Use grouped windows when comparisons should remain specific to an entity, such as an individual product. section 12.9
- Treat older observations cautiously when the underlying system changes, because historical data may no longer be comparable. section 4.14
purchases_df = purchases_df.sort_values(['Date', 'Hour', 'Minute'])
purchases_df['Avg Unit Cost Prev 10'] =\
purchases_df['Unit Cost'].rolling(window=10).mean()
purchases_df['Daily Cummulative Total Cost'] =\
purchases_df.groupby('Date')['Total Cost'].cumsum()
The listing shows how to sort purchases, calculate a ten-record rolling unit-cost average, and add a daily cumulative cost feature. section 12.9
