Skip to content

Commit

Permalink
Struct examples (#2924)
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzwilksch committed Mar 17, 2022
1 parent b15b951 commit c4e5b5d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions py-polars/polars/internals/lazy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,27 @@ def struct(exprs: Union[Sequence["pli.Expr"], "pli.Expr"]) -> "pli.Expr":
│ {2,"b",null,[3]} │
└───────────────────────┘
Only collect specific columns as a struct:
>>> df = pl.DataFrame(
... {"a": [1, 2, 3, 4], "b": ["one", "two", "three", "four"], "c": [9, 8, 7, 6]}
... )
>>> df.with_column(pl.struct(pl.col(["a", "b"])).alias("a_and_b"))
shape: (4, 4)
┌─────┬───────┬─────┬───────────────────────────────┐
│ a ┆ b ┆ c ┆ a_and_b │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ struct[2]{'a': i64, 'b': str} │
╞═════╪═══════╪═════╪═══════════════════════════════╡
│ 1 ┆ one ┆ 9 ┆ {1,"one"} │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2 ┆ two ┆ 8 ┆ {2,"two"} │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 3 ┆ three ┆ 7 ┆ {3,"three"} │
├╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 4 ┆ four ┆ 6 ┆ {4,"four"} │
└─────┴───────┴─────┴───────────────────────────────┘
"""
exprs = pli.selection_to_pyexpr_list(exprs)
return pli.wrap_expr(_as_struct(exprs))

0 comments on commit c4e5b5d

Please sign in to comment.