Skip to content

Commit

Permalink
docs(python): Add doc examples to concat_list (#17127)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Jun 22, 2024
1 parent 21602ba commit 9391c03
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions py-polars/polars/functions/as_datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,36 @@ def concat_list(exprs: IntoExpr | Iterable[IntoExpr], *more_exprs: IntoExpr) ->
Examples
--------
Concatenate two existing list columns. Null values are propagated.
>>> df = pl.DataFrame({"a": [[1, 2], [3], [4, 5]], "b": [[4], [], None]})
>>> df.with_columns(concat_list=pl.concat_list("a", "b"))
shape: (3, 3)
┌───────────┬───────────┬─────────────┐
│ a ┆ b ┆ concat_list │
│ --- ┆ --- ┆ --- │
│ list[i64] ┆ list[i64] ┆ list[i64] │
╞═══════════╪═══════════╪═════════════╡
│ [1, 2] ┆ [4] ┆ [1, 2, 4] │
│ [3] ┆ [] ┆ [3] │
│ [4, 5] ┆ null ┆ null │
└───────────┴───────────┴─────────────┘
Non-list columns are cast to a list before concatenation. The output data type
is the supertype of the concatenated columns.
>>> df.select("a", concat_list=pl.concat_list("a", pl.lit("x")))
shape: (3, 2)
┌───────────┬─────────────────┐
│ a ┆ concat_list │
│ --- ┆ --- │
│ list[i64] ┆ list[str] │
╞═══════════╪═════════════════╡
│ [1, 2] ┆ ["1", "2", "x"] │
│ [3] ┆ ["3", "x"] │
│ [4, 5] ┆ ["4", "5", "x"] │
└───────────┴─────────────────┘
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]})
Expand Down

0 comments on commit 9391c03

Please sign in to comment.