Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Module

.fill_null

Expr.fill_null replaces null values with a literal, expression, or supported strategy such as forward, backward, min, max, mean, zero, or one. A limit can bound consecutive replacements for forward and backward filling.

How it works

This expression replaces null entries either with a supplied literal or expression, or by applying a supported strategy such as forward, backward, minimum, maximum, mean, zero, or one. The optional limit restricts how many consecutive nulls forward or backward filling may replace.

Syntax

Expr.fill_null(value: Any | Expr | None = None, strategy: FillNullStrategy | None = None, limit: int | None = None) → Expr

Parameters

NameTypeDescription
valueAny | Expr | None = NoneValue used to fill null values.
strategyFillNullStrategy | None = NoneStrategy used to fill null values. Supported values are None, forward, backward, min, max, mean, zero, and one.
limitint | None = NoneNumber of consecutive null values to fill when using the forward or backward strategy.

Returns

Expr

Key points

  • The method returns an expression that replaces null entries using either a value or a strategy.
  • The value argument can provide the replacement literal or expression.
  • The strategy argument selects the documented null-filling policy.
  • limit bounds consecutive replacements for forward and backward strategies.
  • Supported strategy names include directional fills, aggregate-based fills, and constant zero or one fills.

Common mistakes

  • This method targets null values, not floating-point NaN values; use fill_nan for NaN replacement.
  • Use interpolate when the intended behavior is interpolation rather than one of the fill strategies.

Example: Fill nulls in an expression

import polars as pl

df = pl.DataFrame({"value": [1, None, 3, None]})
result = df.with_columns(
    pl.col("value").fill_null(strategy="forward", limit=1)
)
print(result)

Replaces each fillable null with the preceding non-null value, limited to one consecutive null.

When to use it

Forward-fill gaps

A column contains gaps that should inherit the most recent available value.

Apply computed replacements

Missing values should use a column-derived calculation rather than a fixed constant.

Limit directional filling

Only a bounded run of consecutive missing values should be propagated.

Interview questions

QWhat does Expr.fill_null do?

It returns an Expr that fills null values with either value or a supported strategy. The strategy can be forward, backward, min, max, mean, zero, or one.

Follow-up: How would you prevent an unbounded forward fill? Pass a limit, which controls the number of consecutive nulls filled by forward or backward strategies.

QWhat is the purpose of limit?

The limit is the number of consecutive null values filled when using forward or backward filling. It does not define a general row limit for every strategy.

Follow-up: Which argument would you use to replace nulls with a computed column-dependent result? Supply an expression through value.

QHow can nulls be replaced with a computed result?

value accepts a literal or expression used as the replacement. If a neighboring-value or aggregate-style policy is preferable, strategy selects the filling behavior instead.

Follow-up: What should be used for missing floating-point NaN values? Use fill_nan rather than fill_null.

  • Expr.interpolate
  • Expr.fill_nan
  • DataFrame.fill_null
  • Series.fill_null

Source: polars.Expr.fill_null — Polars documentation. Checked 2026-10-15.