Skip to content

Fill null values with a value or strategy

Source code

Description

Fill null values with a value or strategy

Usage

<Expr>$fill_null(value = NULL, strategy = NULL, limit = NULL)

Arguments

value Expr or something coercible in an Expr
strategy Possible choice are NULL (default, requires a non-null value), “forward”, “backward”, “min”, “max”, “mean”, “zero”, “one”.
limit Number of consecutive null values to fill when using the “forward” or “backward” strategy.

Value

Expr

Examples

library("polars")

pl$DataFrame(a = c(NA, 1, NA, 2, NA))$
  with_columns(
  value = pl$col("a")$fill_null(999),
  backward = pl$col("a")$fill_null(strategy = "backward"),
  mean = pl$col("a")$fill_null(strategy = "mean")
)
#> shape: (5, 4)
#> ┌──────┬───────┬──────────┬──────┐
#> │ a    ┆ value ┆ backward ┆ mean │
#> │ ---  ┆ ---   ┆ ---      ┆ ---  │
#> │ f64  ┆ f64   ┆ f64      ┆ f64  │
#> ╞══════╪═══════╪══════════╪══════╡
#> │ null ┆ 999.0 ┆ 1.0      ┆ 1.5  │
#> │ 1.0  ┆ 1.0   ┆ 1.0      ┆ 1.0  │
#> │ null ┆ 999.0 ┆ 2.0      ┆ 1.5  │
#> │ 2.0  ┆ 2.0   ┆ 2.0      ┆ 2.0  │
#> │ null ┆ 999.0 ┆ null     ┆ 1.5  │
#> └──────┴───────┴──────────┴──────┘