polars.Expr.fill_null#

Expr.fill_null(value: Any | None = None, strategy: FillNullStrategy | None = None, limit: int | None = None) Expr[source]#

Fill null values using the specified value or strategy.

To interpolate over null values see interpolate.

Parameters:
value

Value used to fill null values.

strategy{None, ‘forward’, ‘backward’, ‘min’, ‘max’, ‘mean’, ‘zero’, ‘one’}

Strategy used to fill null values.

limit

Number of consecutive null values to fill when using the ‘forward’ or ‘backward’ strategy.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 2, None],
...         "b": [4, None, 6],
...     }
... )
>>> df.fill_null(strategy="zero")
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 4   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 0   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 0   ┆ 6   │
└─────┴─────┘
>>> df.fill_null(99)
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 4   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 99  │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 99  ┆ 6   │
└─────┴─────┘
>>> df.fill_null(strategy="forward")
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 4   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 4   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 6   │
└─────┴─────┘