Skip to content

Commit e4c0a3c

Browse files
authored
docs: improve explode method documentation with null/empty list examples (#5164)
## Changes Made Improved the documentation for the `DataFrame.explode()` method to better demonstrate its behavior: - Added example showing DataFrame structure before exploding for clarity - Added comprehensive example demonstrating how null values and empty lists are handled during explosion The documentation previously claimed to show null/empty list behavior "(see example below)" but the example only showed regular list explosion. This PR fulfills that promise with proper examples. ## Checklist - [x] Documented in API Docs (if applicable) - [x] Documentation builds and is formatted properly
1 parent 33884be commit e4c0a3c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

daft/dataframe/dataframe.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,6 +2944,18 @@ def explode(self, *columns: ColumnInputType) -> "DataFrame":
29442944
... ],
29452945
... }
29462946
... )
2947+
>>> df.collect()
2948+
╭─────────────┬────────────┬───────────────╮
2949+
│ x ┆ y ┆ z │
2950+
│ --- ┆ --- ┆ --- │
2951+
│ List[Int64] ┆ List[Utf8] ┆ List[Float64] │
2952+
╞═════════════╪════════════╪═══════════════╡
2953+
│ [1] ┆ [a] ┆ [1] │
2954+
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
2955+
│ [2, 3] ┆ [b, c] ┆ [2, 2] │
2956+
╰─────────────┴────────────┴───────────────╯
2957+
<BLANKLINE>
2958+
(Showing first 2 of 2 rows)
29472959
>>> df.explode(df["x"], df["y"]).collect()
29482960
╭───────┬──────┬───────────────╮
29492961
│ x ┆ y ┆ z │
@@ -2959,6 +2971,46 @@ def explode(self, *columns: ColumnInputType) -> "DataFrame":
29592971
<BLANKLINE>
29602972
(Showing first 3 of 3 rows)
29612973
2974+
Example with Null values and empty lists:
2975+
2976+
>>> df2 = daft.from_pydict(
2977+
... {"id": [1, 2, 3, 4], "values": [[1, 2], [], None, [3]], "labels": [["a", "b"], [], None, ["c"]]}
2978+
... )
2979+
>>> df2.collect()
2980+
╭───────┬─────────────┬────────────╮
2981+
│ id ┆ values ┆ labels │
2982+
│ --- ┆ --- ┆ --- │
2983+
│ Int64 ┆ List[Int64] ┆ List[Utf8] │
2984+
╞═══════╪═════════════╪════════════╡
2985+
│ 1 ┆ [1, 2] ┆ [a, b] │
2986+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
2987+
│ 2 ┆ [] ┆ [] │
2988+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
2989+
│ 3 ┆ None ┆ None │
2990+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
2991+
│ 4 ┆ [3] ┆ [c] │
2992+
╰───────┴─────────────┴────────────╯
2993+
<BLANKLINE>
2994+
(Showing first 4 of 4 rows)
2995+
>>> df2.explode(df2["values"], df2["labels"]).collect()
2996+
╭───────┬────────┬────────╮
2997+
│ id ┆ values ┆ labels │
2998+
│ --- ┆ --- ┆ --- │
2999+
│ Int64 ┆ Int64 ┆ Utf8 │
3000+
╞═══════╪════════╪════════╡
3001+
│ 1 ┆ 1 ┆ a │
3002+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
3003+
│ 1 ┆ 2 ┆ b │
3004+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
3005+
│ 2 ┆ None ┆ None │
3006+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
3007+
│ 3 ┆ None ┆ None │
3008+
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
3009+
│ 4 ┆ 3 ┆ c │
3010+
╰───────┴────────┴────────╯
3011+
<BLANKLINE>
3012+
(Showing first 5 of 5 rows)
3013+
29623014
"""
29633015
parsed_exprs = self.__column_input_to_expression(columns)
29643016
builder = self._builder.explode(parsed_exprs)

0 commit comments

Comments
 (0)