Group by operations are powerful in Polars, but when working with time series data, you can make them even more effective by using group_by_dynamic instead of the standard group_by. Below is a dataframe showing power usage recorded every half hour over five days.
shape: (193, 2)
Time
Power_Usage
datetime[ms]
i64
2025-02-01 00:00:00
2
2025-02-01 00:30:00
1
2025-02-01 01:00:00
5
2025-02-01 01:30:00
4
2025-02-01 02:00:00
4
…
…
2025-02-04 22:00:00
2
2025-02-04 22:30:00
4
2025-02-04 23:00:00
4
2025-02-04 23:30:00
2
2025-02-05 00:00:00
6
Aggregate on time series
Unlike the standard group_by, which limits you to predefined datetime components such as month or year, group_by_dynamic allows you to aggregate data over custom durations. In this example, the data is aggregated every five and a half hours.