Aggregate over a RollingGroupBy
Description
Aggregate a DataFrame over a rolling window created with
$rolling()
.
Usage
<RollingGroupBy>$agg(...)
Arguments
…
|
Exprs to aggregate over. Those can also be passed wrapped in a list, e.g
$agg(list(e1,e2,e3)) .
|
Value
An aggregated DataFrame
Examples
library("polars")
df = pl$DataFrame(
dt = c("2020-01-01", "2020-01-01", "2020-01-01", "2020-01-02", "2020-01-03", "2020-01-08"),
a = c(3, 7, 5, 9, 2, 1)
)$with_columns(
pl$col("dt")$str$strptime(pl$Date, format = NULL)$set_sorted()
)
df$rolling(index_column = "dt", period = "2d")$agg(
pl$col("a"),
pl$sum("a")$alias("sum_a"),
pl$min("a")$alias("min_a"),
pl$max("a")$alias("max_a")
)
#> shape: (6, 5)
#> ┌────────────┬───────────────────┬───────┬───────┬───────┐
#> │ dt ┆ a ┆ sum_a ┆ min_a ┆ max_a │
#> │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
#> │ date ┆ list[f64] ┆ f64 ┆ f64 ┆ f64 │
#> ╞════════════╪═══════════════════╪═══════╪═══════╪═══════╡
#> │ 2020-01-01 ┆ [3.0, 7.0, 5.0] ┆ 15.0 ┆ 3.0 ┆ 7.0 │
#> │ 2020-01-01 ┆ [3.0, 7.0, 5.0] ┆ 15.0 ┆ 3.0 ┆ 7.0 │
#> │ 2020-01-01 ┆ [3.0, 7.0, 5.0] ┆ 15.0 ┆ 3.0 ┆ 7.0 │
#> │ 2020-01-02 ┆ [3.0, 7.0, … 9.0] ┆ 24.0 ┆ 3.0 ┆ 9.0 │
#> │ 2020-01-03 ┆ [9.0, 2.0] ┆ 11.0 ┆ 2.0 ┆ 9.0 │
#> │ 2020-01-08 ┆ [1.0] ┆ 1.0 ┆ 1.0 ┆ 1.0 │
#> └────────────┴───────────────────┴───────┴───────┴───────┘