Skip to content

Commit

Permalink
doctest-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
FBruzzesi committed Dec 21, 2024
1 parent 864e932 commit cc72f6b
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3152,17 +3152,20 @@ def explode(self: Self, columns: str | Sequence[str], *more_columns: str) -> Sel
>>> import polars as pl
>>> import pyarrow as pa
>>> data = {
... "a": ["x", "y", "z", "w"],
... "lst1": [[1, 2], None, [None], []],
... "lst2": [[3, None], None, [42], []],
... "a": ["x", "y", "z"],
... "lst1": [[1, 2], [None, 3], [None]],
... "lst2": [["foo", None], ["bar", None], ["baz"]],
... }
We define a library agnostic function:
>>> def agnostic_explode(df_native: IntoDataFrameT) -> IntoDataFrameT:
... return (
... nw.from_native(df_native)
... .with_columns(nw.col("lst1", "lst2").cast(nw.List(nw.Int32())))
... .with_columns(
... nw.col("lst1").cast(nw.List(nw.Int32())),
... nw.col("lst2").cast(nw.List(nw.String())),
... )
... .explode("lst1", "lst2")
... .to_native()
... )
Expand All @@ -3172,33 +3175,33 @@ def explode(self: Self, columns: str | Sequence[str], *more_columns: str) -> Sel
>>> agnostic_explode(pd.DataFrame(data))
a lst1 lst2
0 x 1 3
0 x 1 foo
0 x 2 <NA>
1 y <NA> <NA>
2 z <NA> 42
3 w <NA> <NA>
1 y <NA> bar
1 y 3 <NA>
2 z <NA> baz
>>> agnostic_explode(pl.DataFrame(data))
shape: (5, 3)
┌─────┬──────┬──────┐
│ a ┆ lst1 ┆ lst2 │
│ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ i32
│ str ┆ i32 ┆ str
╞═════╪══════╪══════╡
│ x ┆ 1 ┆ 3
│ x ┆ 1 ┆ foo
│ x ┆ 2 ┆ null │
│ y ┆ null ┆ null
znull ┆ 42
w ┆ null ┆ null
│ y ┆ null ┆ bar
y3 ┆ null
z ┆ null ┆ baz
└─────┴──────┴──────┘
>>> agnostic_explode(pa.table(data))
pyarrow.Table
a: string
lst1: int32
lst2: int32
lst2: string
----
a: [["x","x","y","z","w"]]
lst1: [[1,2,null,null,null]]
lst2: [[3,null,null,42,null]]
a: [["x","x","y","y","z"]]
lst1: [[1,2,null,3,null]]
lst2: [["foo",null,"bar",null,"baz"]]
"""
return super().explode(columns, *more_columns)

Expand Down

0 comments on commit cc72f6b

Please sign in to comment.