Skip to content

Fill missing values with the last non-null value

Description

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

Usage

<Expr>$forward_fill(limit = NULL)

Arguments

limit The number of consecutive null values to forward fill.

Value

A polars expression

Examples

library("polars")

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