coalesce

pydiverse.transform.coalesce(
arg: ColExpr,
*args: ColExpr,
) ColExpr[source]

Returns the first non-null value among the given.

Parameters:
  • arg – The first value.

  • args – Further values. All must have the same type.

Examples

>>> t = pdt.Table(
...     {
...         "a": [5, None, 435, -1, 8, None],
...         "b": [-45, None, 6, 23, 1, 0],
...         "c": [10, 2, None, None, None, None],
...     }
... )
>>> (
...     t
...     >> mutate(
...         x=pdt.coalesce(t.a, t.b, t.c),
...         y=pdt.coalesce(t.c, t.b, t.a),
...     )
...     >> show()
... )
Table <unnamed>, backend: PolarsImpl
shape: (6, 5)
┌──────┬──────┬──────┬─────┬─────┐
│ a    ┆ b    ┆ c    ┆ x   ┆ y   │
│ ---  ┆ ---  ┆ ---  ┆ --- ┆ --- │
│ i64  ┆ i64  ┆ i64  ┆ i64 ┆ i64 │
╞══════╪══════╪══════╪═════╪═════╡
│ 5    ┆ -45  ┆ 10   ┆ 5   ┆ 10  │
│ null ┆ null ┆ 2    ┆ 2   ┆ 2   │
│ 435  ┆ 6    ┆ null ┆ 435 ┆ 6   │
│ -1   ┆ 23   ┆ null ┆ -1  ┆ 23  │
│ 8    ┆ 1    ┆ null ┆ 8   ┆ 1   │
│ null ┆ 0    ┆ null ┆ 0   ┆ 0   │
└──────┴──────┴──────┴─────┴─────┘