In Polars, the fill_null() function is used to handle missing data by replacing null values in a Series or DataFrame column. You can fill these nulls with a fixed value (such as a number or string), or apply a strategy like forward fill or backward fill. The function returns a new Series with the nulls replaced, helping you ensure that your data is complete and ready for analysis.
In this article, I will explain the fill_null() method of the Polars Series, including its syntax, parameters, and functionality. This method returns a new Series with null values replaced based on the provided value or chosen strategy.
Key Points –
- The
fill_null()function replaces null (missing) values in a Polars Series. - You can fill
nullvalues with a fixed scalar value (number, string, etc.). - You can fill
nullvalues using strategies like"forward"(propagate last non-null value forward). - You can also use
"backward"strategy to fillnullwith the next non-null value - The replacement value or strategy must match the Series data type (e.g., don't use a string to fill numeric Series).
fill_null()can be combined with other Series methods to create flexible data-cleaning pipelines.- You can compute an aggregate (like mean) first, then use it in
fill_null(). - You can use a
strategyto compute replacement values (such as'mean','min','max','forward','backward','zero','one').
<a id="h-polars-series-fill-null-introduction"></a>
Polars Series fill_null() Introduction
Let’s know the syntax of the series fill_null() method.
# Syntax of Series fill_null()
Series.fill_null(
value: Any | Expr | None = None,
strategy: FillNullStrategy | None = None,
limit: int | None = None,
) → Series
<a id="h-parameters-of-the-polars-series-fill-null"></a>
Parameters of the Polars series fill_null()
Following are the parameters of the fill_null() function.
value(optional) - Scalar value or Polars expression to replacenullvalues.strategy(optional) - Strategy to fillnullvalues:"forward"→ forward fill"backward"→ backward fill"min"→ fill with minimum value"max"→ fill with maximum value"mean"→ fill with mean of the series"zero"→ fill with 0"one"→ fill with 1
limit- Maximum number of consecutive nulls to fill. IfNone, fill all.
<a id="h-return-value"></a>
Return Value
This function returns a new Polars Series with null values filled.
<a id="h-usage-of-polars-series-fill-null-function"></a>
Usage of Polars Series fill_null() Function
The fill_null() function in Polars is used to replace null (missing) values in a Series, either with a specified value or by applying a strategy (such as forward fill, mean, etc.).
First, let’s create a Polars Series.
import polars as pl
# Sample Series with nulls
ser = pl.Series("values", [10, None, 30, None, 50])
print("Original Series:\n", ser)
Yields below output.
To fill nulls with a fixed value (e.g., 0) in a Polars Series, you can use the fill_null() function.
# Fill nulls with a fixed value (0)
ser2 = ser.fill_null(0)
print("Series after fill_null(0):\n", ser2)
Here,
- You used
ser.fill_null(0)to replace allnullvalues with0. - The Series is now clean and has no missing values.
<a id="h-fill-nulls-with-the-mean-of-the-series"></a>
Fill Nulls with the Mean of the Series
You can also fill nulls in a Polars Series with the mean of the Series, a common technique in data cleaning. Since Series.fill_null() accepts a fixed value, you first compute the mean and then pass it to fill_null().
# Compute mean of the Series (ignores nulls)
mean_value = ser.mean()
print(f"\nMean of the Series (ignoring nulls): {mean_value}")
# Fill nulls with the mean value
ser2 = ser.fill_null(mean_value)
print("\nSeries after fill_null(mean):\n", ser2)
# Output:
# Series after fill_null(mean):
# shape: (5,)
# Series: 'values' [f64]
# [
# 10.0
# 30.0
# 30.0
# 30.0
# 50.0
# ]
Here,
ser.mean()calculates the mean of non-null values, here:(10 + 30 + 50) / 3 = 30.0fill_null(mean_value)replaces nulls with30.0.
<a id="h-forward-fill-use-last-non-null-value"></a>
Forward Fill (Use Last Non-Null Value)
You can use Polars Series.fill_null() to perform a forward fill. This technique replaces each null value with the most recent non-null value that appears earlier in the Series.
# Forward fill nulls
ser2 = ser.fill_null(strategy="forward")
print("\nSeries after forward fill:\n", ser2)
# Output:
# Series after forward fill:
# shape: (5,)
# Series: 'values' [i64]
# [
# 10
# 10
# 30
# 30
# 50
# ]
Here,
- The first
nullbecame10, last non-null before it. - The second
nullbecame30, last non-null before it. - Non-null values stay unchanged.
<a id="h-backward-fill-use-next-non-null-value"></a>
Backward Fill (Use Next Non-Null Value)
In backward fill, each null is replaced with the next non-null value that appears after it in the Series. This technique fills nulls with the nearest non-null value below them.
# Backward fill nulls
ser2 = ser.fill_null(strategy="backward")
print("Series after backward fill:\n", ser2)
# Output:
# Series after backward fill:
# shape: (5,)
# Series: 'values' [i64]
# [
# 10
# 30
# 30
# 50
# 50
# ]
Here.
- The first
nullbecame30, next non-null below it - The second
nullbecame50, next non-null below it - Non-null values stay unchanged.
<a id="h-fill-nulls-with-a-fixed-string-for-string-series"></a>
Fill Nulls with a Fixed String (For String Series)
You can also use fill_null() to replace nulls in a string Series with a fixed string. The function works the same way for string data, you can fill null values with strings like "unknown" or "missing".
import polars as pl
# Sample string Series with nulls
ser = pl.Series("Courses", ["Spark", None, "Python", None, "Hyperion"])
# Fill nulls with a fixed string
ser2 = ser.fill_null("Unknown")
print("Series after fill_null('Unknown'):\n", ser2)
# Output:
# Series after fill_null('Unknown'):
# shape: (5,)
# Series: 'Courses' [str]
# [
# "Spark"
# "Unknown"
# "Python"
# "Unknown"
# "Hyperion"
# ]
Here,
fill_null()works for any data type, numeric, string, etc.- Here it replaced
nullwith"Unknown".
<a id="h-conclusion"></a>
Conclusion
In summary, fill_null() is a versatile function in the Polars Series that allows you to handle missing data effectively. Whether you want to replace nulls with a fixed value, use forward or backward fill, or even fill string data with placeholders, fill_null() provides a simple and consistent way to clean your Series for further analysis.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series limit() – Explained by Examples
- Polars Series interpolate() Function with Examples
- Polars Series quantile() – Usage & Examples
- Polars Series extend() Usage & Examples
- Polars Series abs() – Explained by Examples
- Polars Series Shape Usage & Examples
- Polars Series sum() Method with Examples
- Polars Series describe() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars Series clear() – Explained by Examples
- Polars Series rolling_mean() – Usage & Examples
- Polars Series struct.schema() – Explained by Examples
<a id="h-references"></a>
