Skip to content

Commit 64db266

Browse files
committed
updated docstring example in v2 (test passed)
1 parent 5539438 commit 64db266

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

narwhals/stable/v2/__init__.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,14 +917,40 @@ def concat_str(
917917

918918

919919
def struct(exprs: IntoExpr | Iterable[IntoExpr], *more_exprs: IntoExpr) -> Expr:
920-
"""Horizontally combine multiple columns into a single struct column.
920+
r"""Horizontally combine multiple columns into a single struct column.
921921
922922
Arguments:
923923
exprs: One or more expressions to combine into a struct. Strings are treated as column names.
924924
*more_exprs: Additional columns or expressions, passed as positional arguments.
925925
926926
Returns:
927927
An expression that produces a single struct column containing the given fields.
928+
929+
Example:
930+
>>> import pandas as pd
931+
>>> import narwhals as nw
932+
>>>
933+
>>> data = {
934+
... "a": [1, 2, 3],
935+
... "b": ["dogs", "cats", None],
936+
... "c": ["play", "swim", "walk"],
937+
... }
938+
>>> df_native = pd.DataFrame(data)
939+
>>> (
940+
... nw.from_native(df_native).select(
941+
... nw.struct([nw.col("a") * 2, nw.col("b"), nw.col("c")]).alias(
942+
... "my_struct"
943+
... )
944+
... )
945+
... )
946+
┌─────────────────────────────────────┐
947+
| Narwhals DataFrame |
948+
|-------------------------------------|
949+
| my_struct|
950+
|0 {'a': 2, 'b': 'dogs', 'c': 'play'}|
951+
|1 {'a': 4, 'b': 'cats', 'c': 'swim'}|
952+
|2 {'a': 6, 'b': None, 'c': 'walk'}|
953+
└─────────────────────────────────────┘
928954
"""
929955
return _stableify(nw.struct(exprs, *more_exprs))
930956

0 commit comments

Comments
 (0)