Код: Выделить всё
import polars as pl
df = pl.DataFrame({"x": [1.2, 1.3, 3.4, 3.5]})
df
# shape: (3, 1)
# ┌─────┐
# │ a │
# │ --- │
# │ f64 │
# ╞═════╡
# │ 1.2 │
# │ 1.3 │
# │ 3.4 │
# │ 3.5 │
# └─────┘
Код: Выделить всё
shape: (4, 2)
┌─────┬───────────────────┐
│ x ┆ y │
│ --- ┆ --- │
│ f64 ┆ list[f64] │
╞═════╪═══════════════════╡
│ 1.2 ┆ [1.2, 1.3] │
│ 1.3 ┆ [1.2, 1.3, 3.4] │
│ 3.4 ┆ [1.2, 1.3, … 3.5] │
│ 3.5 ┆ [1.3, 3.4, 3.5] │
└─────┴───────────────────┘
Код: Выделить всё
df.with_row_index("index").with_columns(
y = pl.col("x").rolling(index_column = "index", period = "4i", offset = "-3i")
).drop("index")
Подробнее здесь: https://stackoverflow.com/questions/794 ... -row-index