In Polars, the interpolate() method is used to fill missing values (nulls) in numeric columns by estimating intermediate points through linear interpolation. This approach assumes a straight-line relationship between adjacent known values to calculate the missing data. Similar to Pandas, Polars provides an interpolate() method for both Series and DataFrames. Specifically, the DataFrame version fills gaps in numeric columns by linearly estimating values based on surrounding existing data.
In this article, I will explain the Polars DataFrame interpolate() method, covering its syntax, parameters, and usage. This method produces a new DataFrame where missing values (nulls) in each numeric column are filled independently through interpolation.
Key Points –
- The
interpolate()method fills missing/null values using linear interpolation. - It operates independently on each numeric column in the DataFrame.
- Only numeric columns (floats and integers) are interpolated; non-numeric columns are unaffected.
- The method estimates missing values by assuming a linear change between neighboring non-null values.
- The method can be applied to the entire DataFrame or on specific columns via
with_columns(). - Returns a new DataFrame with interpolated values; original DataFrame remains unchanged.
- Does not fill nulls at the start or end of a column if no surrounding values exist.
- Currently, Polars supports only linear interpolation; no other interpolation methods (like polynomial or spline) are provided.
<a id="h-polars-dataframe-interpolate-introduction"></a>
Polars DataFrame interpolate() Introduction
Let’s know the syntax of the DataFrame interpolate() function
# Syntax of DataFrame interpolate()
DataFrame.interpolate(method: str = "linear") -> DataFrame
<a id="h-parameters-of-the-dataframe-interpolate"></a>
Parameters of the DataFrame interpolate()
Following are the parameters of the DataFrame interpolate() function
method(optional): The interpolation method to use. Currently, only"linear"is supported.
<a id="h-return-value"></a>
Return Value
This function returns a new DataFrame with nulls interpolated in all columns where applicable.
<a id="h-usage-of-polars-dataframe-interpolate-method"></a>
Usage of Polars DataFrame interpolate() Method
The interpolate() method in Polars replaces missing values (nulls) in numeric columns by calculating estimates that lie between existing data points using linear interpolation. It assumes a steady, constant rate of change between known values and fills the gaps by connecting them with straight lines.
First, let’s create a Polars DataFrame.
import polars as pl
# Sample DataFrame with missing values
df = pl.DataFrame({
"A": [1.0, None, None, 4.0, 5.0]
})
print("Original DataFrame:\n", df)
Yields below output.
Linear interpolation in a Polars DataFrame fills missing values (nulls) by calculating estimated points that lie evenly between two known data points. It works under the assumption that the values change steadily and at a constant rate between those points, effectively drawing a straight line to fill the gaps.
# Interpolate missing values linearly
df2 = df.interpolate()
print("After Linear Interpolation:\n", df2)
Here,
- The two null values between
1.0and4.0are filled with2.0and3.0. - The method assumes a straight-line change between known data points.
<a id="h-interpolation-across-multiple-numeric-columns"></a>
Interpolation Across Multiple Numeric Columns
When applied to a Polars DataFrame, the interpolate() method operates independently on each numeric column. It performs linear interpolation within each column, filling in null values based solely on the non-null values present in that specific column, without referencing or being influenced by the values in other columns.
import polars as pl
# DataFrame with missing values in multiple columns
df = pl.DataFrame({
"temperature": [20, None, 24, None, 28],
"humidity": [None, 60, None, 80, 90]
})
# Apply linear interpolation to all numeric columns
df2 = df.interpolate()
print("After Linear Interpolation:\n", df2)
# Output:
# After Linear Interpolation:
# shape: (5, 2)
┌─────────────┬──────────┐
│ temperature ┆ humidity │
│ --- ┆ --- │
│ f64 ┆ f64 │
╞═════════════╪══════════╡
│ 20.0 ┆ null │
│ 22.0 ┆ 60.0 │
│ 24.0 ┆ 70.0 │
│ 26.0 ┆ 80.0 │
│ 28.0 ┆ 90.0 │
└─────────────┴──────────┘
Here,
- Both
"temperature"and"humidity"columns have missing values (None). - Polars
interpolate()fills those gaps independently in each numeric column using linear interpolation. - Non-numeric columns (if any) would be ignored.
<a id="h-interpolate-only-one-column-using-with-columns"></a>
Interpolate Only one Column Using with_columns()
To interpolate a single column in a Polars DataFrame without modifying the others, you can use the with_columns() method. This allows you to apply interpolation selectively to just one column while keeping the rest of the DataFrame unchanged.
# Interpolate only the 'temperature' column
df2 = df.with_columns([
df["temperature"].interpolate().alias("temperature")
])
print("After Interpolating Only Column 'temperature':\n", df2)
# Output:
# After Interpolating Only Column 'temperature':
# shape: (5, 2)
┌─────────────┬──────────┐
│ temperature ┆ humidity │
│ --- ┆ --- │
│ f64 ┆ i64 │
╞═════════════╪══════════╡
│ 20.0 ┆ null │
│ 22.0 ┆ 60 │
│ 24.0 ┆ null │
│ 26.0 ┆ 80 │
│ 28.0 ┆ 90 │
└─────────────┴──────────┘
Here,
- Only the
"temperature"column is interpolated. "humidity"remains unchanged, including itsnullvalues.
<a id="h-interpolation-with-a-sorted-index"></a>
Interpolation with a Sorted Index
Sorting the data before interpolation ensures accurate linear estimation, especially in cases where the value progression depends on order, such as in time series data.
import polars as pl
# Unsorted DataFrame
df = pl.DataFrame({
"Day": [3, 1, 2, 5, 4],
"Value": [30.0, 10.0, None, 50.0, None]
})
# Interpolate after sorting by "Day"
df2 = (
df.sort("Day")
.with_columns(
pl.col("Value").interpolate().alias("Value")
)
)
print("Sorted and Interpolated:\n", df2)
# Output:
# Sorted and Interpolated:
# shape: (5, 2)
┌─────┬───────┐
│ Day ┆ Value │
│ --- ┆ --- │
│ i64 ┆ f64 │
╞═════╪═══════╡
│ 1 ┆ 10.0 │
│ 2 ┆ 20.0 │
│ 3 ┆ 30.0 │
│ 4 ┆ 40.0 │
│ 5 ┆ 50.0 │
└─────┴───────┘
Here,
- Always sort by the relevant index/column before interpolating.
- This is especially useful in time-based or ordinal datasets.
<a id="h-conclusion"></a>
Conclusion
In conclusion, Polar’s interpolate() method is a powerful and flexible tool for filling missing values through linear estimation. By applying interpolation column-wise, optionally within groups, or on sorted data, you can effectively handle gaps in your datasets, especially in time series or numerical analyses, while maintaining control over which columns get interpolated.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars DataFrame rows() Method with Examples
- Polars DataFrame Drop Nans
- Polars Adding Days to a Date
- Replace Value by Null in Polars
- Add Row of Column Totals in Polars
- Polars DataFrame Columns Selection
- Polars Delete a Column from DataFrame
- Polars String Manipulation of Cell Contents
- Polars String Concatenation of Two Existing Columns
- How to Get the Name of an Aliased Expression in Polars?
- Polars DataFrame with_columns() Function with Examples
- How to Change the First Letter of the Column Name to Uppercase in Polars?
<a id="h-reference"></a>
