Given a Polars df like below, how can I call explode()
on both columns while expanding the null entry to the correct length to match up with its row?
shape: (3, 2)
┌───────────┬─────────────────────┐
│ x ┆ y │
│ --- ┆ --- │
│ list[i64] ┆ list[bool] │
╞═══════════╪═════════════════════╡
│ [1] ┆ [true] │
│ [1, 2] ┆ null │
│ [1, 2, 3] ┆ [true, false, true] │
└───────────┴─────────────────────┘
Currently calling df.explode([“x”, “y”]) will result in this error:
polars.exceptions.ShapeError: exploded columns must have matching element counts
I’m assuming there’s not a built in way. But I can’t find/think of a way to convert that null into a list with the correct length so the explode will work (assuming I don’t statically know the required length beforehand).
I looked into passing list.lengths into repeat_by(), but repeat_by() doesn’t support null.
Brian Braddock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.