Time-Series in Polars: group_by_dynamic Explained

3D visualization of a sliding window frame aggregating data cubes along a timeline, representing Polars group_by_dynamic.

One of the most common tasks in data analysis is “resampling” time data. For example, turning a list of daily sales into “Total Monthly Sales.” A powerful feature for this in Polars is group_by_dynamic, which allows flexible time-based grouping.

In Pandas, you use .resample(). In Polars, you use the much faster <a href="https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.group_by_dynamic.html" type="link" id="https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.group_by_dynamic.html">group_by_dynamic()</a>.

Step 1: Create the Data

You must have a datetime column.

import polars as pl
from datetime import datetime

df = pl.DataFrame({
    "time": [
        datetime(2025, 1, 1),
        datetime(2025, 1, 15),
        datetime(2025, 2, 1),
        datetime(2025, 2, 20),
    ],
    "sales": [100, 200, 300, 400],
})

Step 2: group_by_dynamic()

This method has a crucial requirement: The DataFrame must be sorted by the time column.

# 1. Sort the DataFrame
df_sorted = df.sort("time")

# 2. Group by month
# 'every=' is the window size
# 'on=' is the column to group
monthly_sales = (
    df_sorted.group_by_dynamic("time", every="1mo")
             .agg(pl.col("sales").sum().alias("total_sales"))
)
print(monthly_sales)

Output:

shape: (2, 2)
┌─────────────────────┬─────────────┐
│ time                ┆ total_sales │
│ ---                 ┆ ---         │
│ datetime[μs]        ┆ i64         │
╞═════════════════════╪═════════════╡
│ 2025-01-01 00:00:00 ┆ 300         │
│ 2025-02-01 00:00:00 ┆ 700         │
└─────────────────────┴─────────────┘

It correctly grouped January’s sales (100+200) and February’s sales (300+400). This is a core function for any financial or sensor data analysis.

Similar Posts

Leave a Reply