liveBook · Manning

Rolling window

As explained in Outlier Detection in Python

A rolling window is a selection of the most recent fixed-size or fixed-duration observations used for analysis.

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

How Outlier Detection in Python covers it

How deep it goes
Explains three-to-five-year and ten-record windows, initial missing values, sorting, and grouped calculations.
Examples
Uses financial audits, industrial-process histories, and purchase unit costs with daily cumulative spending.
What it assumes
Assumes ordered observations and a meaningful comparison history; teaches why initial rows lack full-window values.
What you can do after
Apply it through dataframe operations to calculate rolling means and add contextual features for multivariate outlier detection.
How it connects
Connects rolling windows with outlier detection, reference datasets, feature engineering, multivariate analysis, and cumulative totals.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.