Skip to content

Fill floating point null value with a fill value

Source code

Description

Fill floating point null value with a fill value

Usage

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

Arguments

value Value used to fill null values. Can be NULL if strategy is specified. Accepts expression input, strings are parsed as column names.
strategy Strategy used to fill null values. If value is NULL, must be one of “forward”, “backward”, “min”, “max”, “mean”, “zero”, “one”.
limit Number of consecutive null values to fill when using the “forward” or “backward” strategy.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = c(1, NA, 2, NaN))
df$with_columns(
  filled_null_zero = pl$col("a")$fill_null(strategy = "zero"),
  filled_null_99 = pl$col("a")$fill_null(99),
  filled_null_forward = pl$col("a")$fill_null(strategy = "forward"),
  filled_null_expr = pl$col("a")$fill_null(pl$col("a")$median())
)
#> shape: (4, 5)
#> ┌──────┬──────────────────┬────────────────┬─────────────────────┬──────────────────┐
#> │ a    ┆ filled_null_zero ┆ filled_null_99 ┆ filled_null_forward ┆ filled_null_expr │
#> │ ---  ┆ ---              ┆ ---            ┆ ---                 ┆ ---              │
#> │ f64  ┆ f64              ┆ f64            ┆ f64                 ┆ f64              │
#> ╞══════╪══════════════════╪════════════════╪═════════════════════╪══════════════════╡
#> │ 1.0  ┆ 1.0              ┆ 1.0            ┆ 1.0                 ┆ 1.0              │
#> │ null ┆ 0.0              ┆ 99.0           ┆ 1.0                 ┆ 2.0              │
#> │ 2.0  ┆ 2.0              ┆ 2.0            ┆ 2.0                 ┆ 2.0              │
#> │ NaN  ┆ NaN              ┆ NaN            ┆ NaN                 ┆ NaN              │
#> └──────┴──────────────────┴────────────────┴─────────────────────┴──────────────────┘