Implementation:Pola rs Polars Upsample and Interpolate
| Knowledge Sources | |
|---|---|
| Domains | Data Engineering, Time Series |
| Last Updated | 2026-02-09 10:00 GMT |
Overview
Concrete APIs for upsampling a Polars DataFrame to a higher temporal frequency and filling the resulting null values using forward fill, interpolation, or other strategies.
Description
DataFrame.upsample(time_column, every) inserts new rows at the specified frequency interval, producing a DataFrame with evenly-spaced timestamps. The non-index columns in newly inserted rows contain null values. These nulls are then filled using DataFrame.fill_null(strategy) for strategy-based filling or DataFrame.interpolate() for linear interpolation between adjacent known values.
The upsample method requires the temporal column to be sorted. An optional group_by parameter supports upsampling panel data where each entity is upsampled independently.
Usage
Use these APIs whenever you need to:
- Increase the temporal frequency of a time series (e.g., 30-minute to 15-minute intervals).
- Fill temporal gaps with forward-filled or interpolated values.
- Align multiple time series to a common temporal grid.
Code Reference
Source Location
- Repository: Polars
- File:
docs/source/src/python/user-guide/transformations/time-series/resampling.py(lines 1-36)
Signature
DataFrame.upsample(
time_column: str,
*,
every: str | timedelta,
group_by: str | Sequence[str] | None = None,
maintain_order: bool = False,
) -> DataFrame
DataFrame.fill_null(
value: Any | Expr | None = None,
strategy: str | None = None,
limit: int | None = None,
matches_supertype: bool = True,
) -> DataFrame
DataFrame.interpolate() -> DataFrame
Import
import polars as pl
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| time_column | str |
Yes | Name of the sorted temporal column to upsample |
| every | timedelta | Yes | Target frequency as a duration string (e.g., "15m", "1h", "1d")
|
| group_by | Sequence[str] | None | No | Grouping columns for panel data upsampling |
| strategy | None | No (for fill_null) |
Fill strategy: "forward", "backward", "mean", "zero", "one", "max", "min"
|
| limit | None | No (for fill_null) |
Maximum number of consecutive nulls to fill |
Outputs
| Name | Type | Description |
|---|---|---|
| result (upsample) | pl.DataFrame |
DataFrame with new rows inserted at the target frequency; non-index columns contain nulls at new time points |
| result (fill_null) | pl.DataFrame |
DataFrame with nulls replaced according to the specified strategy |
| result (interpolate) | pl.DataFrame |
DataFrame with nulls filled via linear interpolation between adjacent known values |
Usage Examples
Upsample with Forward Fill
import polars as pl
# Upsample from 30-min to 15-min intervals
df_upsampled = df.upsample(time_column="time", every="15m")
# Fill nulls with forward fill
df_filled = df_upsampled.fill_null(strategy="forward")
print(df_filled)
Upsample with Interpolation
import polars as pl
# Upsample from 30-min to 15-min intervals
df_upsampled = df.upsample(time_column="time", every="15m")
# Fill nulls using linear interpolation
df_interpolated = df_upsampled.interpolate()
print(df_interpolated)
Upsample Panel Data
import polars as pl
# Upsample each sensor independently
df_upsampled = df.upsample(
time_column="timestamp",
every="1h",
group_by="sensor_id",
)
df_filled = df_upsampled.fill_null(strategy="forward")