polars.concat_list#

polars.concat_list(exprs: Union[Sequence[str | Expr | Series], Expr]) Expr[source]#

Concat the arrays in a Series dtype List in linear time.

Parameters:
exprs

Columns to concat into a List Series

Examples

Create lagged columns and collect them into a list. This mimics a rolling window.

>>> df = pl.DataFrame(
...     {
...         "A": [1.0, 2.0, 9.0, 2.0, 13.0],
...     }
... )
>>> (
...     df.with_columns(
...         [pl.col("A").shift(i).alias(f"A_lag_{i}") for i in range(3)]
...     ).select(
...         [
...             pl.concat_list([f"A_lag_{i}" for i in range(3)][::-1]).alias(
...                 "A_rolling"
...             )
...         ]
...     )
... )
shape: (5, 1)
┌───────────────────┐
│ A_rolling         │
│ ---               │
│ list[f64]         │
╞═══════════════════╡
│ [null, null, 1.0] │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [null, 1.0, 2.0]  │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [1.0, 2.0, 9.0]   │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [2.0, 9.0, 2.0]   │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [9.0, 2.0, 13.0]  │
└───────────────────┘