In Polars, the interpolate() function is used to fill missing (null) values in a Series by estimating intermediate values based on neighboring non-null entries, typically using linear interpolation. It works by calculating intermediate values between existing data points, effectively smoothing and completing the dataset for better continuity.
In this article, I will explain the Polars Series interpolate() method, including its syntax, parameters, and functionality. This method returns a new Polars Series where missing values are filled with interpolated estimates.
Key Points –
interpolate()is a method in Polars Series used to fill missing (null) values through interpolation.- The default and currently supported interpolation method is linear interpolation.
- It works by estimating missing values based on known values before and after the nulls.
- The method returns a new Series; the original Series remains unchanged.
- Nulls at the beginning or end of the Series remain
null(no extrapolation is performed). - Nulls between known values are replaced by estimated values that follow a straight line between the surrounding points.
- Interpolation can be combined with other methods (like forward-fill or backward-fill) to handle edge
nullvalues when needed. - The Series is automatically promoted to a floating-point type if necessary (e.g. if it originally contained integers).
Syntax of Polars Series interpolate() Function
Let’s know the syntax of the series interpolate() function.
# Syntax of Series interpolate()
Series.interpolate(method: InterpolationMethod = 'linear') → Series
Parameters of the Polars series interpolate()
It allows only one parameter.
method(optional) – The interpolation method to use. Currently, Polars supports only'linear'interpolation.
Return Value
This function returns a new Series with missing values filled via the specified interpolation method.
Usage of Polars Series interpolate() Function
The interpolate() method in Polars fills missing (null) values in a Series by applying linear interpolation. It calculates the missing values by estimating them along a straight line between the surrounding known values.
Now, let’s create a Polars Series.
import polars as pl
ser = pl.Series("values", [10, 20, None, None, 50])
print("Original Series:\n", ser)
Yields below output.
If you want to fill missing values using basic linear interpolation on a Polars Series, here’s a simple example with the Series.interpolate() method.
# Apply linear interpolation
ser2 = ser.interpolate()
print("Interpolated Series:\n", ser2)
Here,
- You had values 10 and 30.
- The missing value (
None) between them is interpolated as 20, which lies exactly halfway between 10 and 30.
Float Series Interpolation
Interpolation on a Polars Series containing float values with missing data allows you to estimate and fill those gaps by calculating intermediate values between the existing known points.
import polars as pl
# Create a float Series with missing values
ser = pl.Series("temperatures", [15.5, None, None, 30.0, 35.5])
# Interpolate missing values (default linear method)
ser2 = ser.interpolate()
print("Interpolated Series:\n", ser2)
# Output:
# Interpolated Series:
# shape: (5,)
# Series: 'temperatures' [f64]
# [
# 15.5
# 20.333333
# 25.166667
# 30.0
# 35.5
# ]
Here,
- The two
nullvalues between 15.5 and 30.0 are interpolated linearly. - The values fill the gap evenly, giving approximately 20.33 and 25.17.
- Linear interpolation works smoothly on floats the same way as integers.
Interpolating with Nulls at the Beginning
Interpolation behaves differently when null values appear at the beginning of a Polars Series. The Series.interpolate() method has a specific way of handling these leading nulls. Here’s an example illustrating what happens when you apply interpolate() to a Series with null values at the start.
import polars as pl
ser = pl.Series("values", [None, None, 20, 30, 40])
# Interpolating with nulls at the beginning
ser2 = ser.interpolate()
print("\nInterpolated Series:\n", ser2)
# Output:
# Interpolated Series:
# shape: (5,)
# Series: 'values' [f64]
# [
# null
# null
# 20.0
# 30.0
# 40.0
# ]
Here,
- The leading nulls (at indices 0 and 1) cannot be interpolated because there is no previous known value before them.
- So those nulls remain null after interpolation.
- Interpolation fills only the nulls between or after known values.
Interpolating with Nulls at the End
When null values appear at the end of a Polars Series, interpolation behaves similarly to how it does at the beginning, but from the opposite side. Polars manages these trailing nulls when using the interpolate() method.
import polars as pl
ser = pl.Series("data", [10, 20, 30, None, None])
# Interpolating with nulls at the end
ser2 = ser.interpolate()
print("Interpolated Series:\n", ser2)
# Output:
# Interpolated Series:
#shape: (5,)
# Series: 'data' [f64]
# [
# 10.0
# 20.0
# 30.0
# null
# null
# ]
Here,
- Nulls at the end remain null after interpolation.
- Polars’
interpolate()does not extrapolate beyond the last known value. - Only gaps between valid values get filled.
Single Null in the Middle
Interpolating a single null value in the middle of a Polars Series is simple. The interpolate() method estimates the missing value by drawing a linear path between the known values surrounding the null.
import polars as pl
ser = pl.Series("data", [5, 10, None, 20, 25])
# Single null in the middle
ser2 = ser.interpolate()
print("Interpolated Series:\n", ser2)
# Output:
# Interpolated Series:
# shape: (5,)
# Series: 'data' [f64]
# [
# 5.0
# 10.0
# 15.0
# 20.0
# 25.0
# ]
Here,
- The single
nullbetween 10 and 20 is replaced with the average (linear interpolation) value 15.0. - The interpolation smoothly fills gaps between known values.
Conclusion
In conclusion, the interpolate() function in Polars Series provides an easy and effective way to fill missing (null) values through linear interpolation. Whether your gaps appear at the start, middle, or end of the Series, this method helps you generate smooth and continuous data without introducing arbitrary values.
Happy Learning!!
Related Articles
- 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 sample() Function with Examples
- Polars Series explode() – Explained by Examples
- Polars Series rolling_mean() – Usage & Examples
- Polars Series struct.schema() – Explained by Examples