Skip to content

Fill missing values with the next non-null value

Description

[Superseded] This is an alias of $fill_null(strategy = "backward").

Usage

<Expr>$backward_fill(limit = NULL)

Arguments

limit The number of consecutive null values to backward fill.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  a = c(1, 2, NA),
  b = c(4, NA, 6),
  c = c(NA, NA, 2)
)
df$select(pl$all()$backward_fill())
#> shape: (3, 3)
#> ┌──────┬─────┬─────┐
#> │ a    ┆ b   ┆ c   │
#> │ ---  ┆ --- ┆ --- │
#> │ f64  ┆ f64 ┆ f64 │
#> ╞══════╪═════╪═════╡
#> │ 1.0  ┆ 4.0 ┆ 2.0 │
#> │ 2.0  ┆ 6.0 ┆ 2.0 │
#> │ null ┆ 6.0 ┆ 2.0 │
#> └──────┴─────┴─────┘
df$select(pl$all()$backward_fill(limit = 1))
#> shape: (3, 3)
#> ┌──────┬─────┬──────┐
#> │ a    ┆ b   ┆ c    │
#> │ ---  ┆ --- ┆ ---  │
#> │ f64  ┆ f64 ┆ f64  │
#> ╞══════╪═════╪══════╡
#> │ 1.0  ┆ 4.0 ┆ null │
#> │ 2.0  ┆ 6.0 ┆ 2.0  │
#> │ null ┆ 6.0 ┆ 2.0  │
#> └──────┴─────┴──────┘