Ciaren

Rolling aggregate

Rolling aggregate — rollingAggregate

Compute a moving aggregate (mean, sum, min, max, std, median) over a window of N rows, within an optional partition and order. Ideal for smoothing time series.

Use cases

  • A 7-day moving average of sales.
  • A rolling sum of usage per customer (partitioned by customer).
  • Rolling volatility (std) of a price series.

What it does

Rows are ordered by the order by columns, then a window of window rows is aggregated with the chosen function. With partition by, the window restarts within each group. The original row order is preserved in the output.

Configuration

Config keyTypeRequiredDescription
targetstringYesNumeric column to aggregate
functionstringYesmean, sum, min, max, std, median
windowintYesNumber of rows per window (≥ 1)
min_periodsintNoMin rows required; empty = full window
order_bystring[]NoOrder rows within the window (e.g. a date)
partition_bystring[]NoRestart the window within each group
descendingbooleanNoOrder descending
new_columnstringYesName of the result column

Generated Python code

df_2 = df_1.assign(sales_ma=lambda _d: _d.sort_values('date', kind='stable')['sales'].rolling(7).mean())

Tips & common mistakes

  • Set min periods to 1 to get partial windows at the start; otherwise the first window − 1 rows are null.
  • Always set order by for time series — otherwise the window follows input order, which may not be chronological.

See also