Code
import polars as pl
import pytimetk as tkPolars shines on wide datasets and large groups thanks to its columnar memory model and eager/lazy execution. pytimetk supports Polars both through the .tk accessor and via engine="polars" on many heavy-hitting helpers, so you can keep your existing workflows while getting a speed boost.
We’ll use the m4_daily dataset (multiple daily series). Start in pandas, then convert to Polars.
| id | date | value |
|---|---|---|
| str | datetime[ns] | f64 |
| "D10" | 2014-07-03 00:00:00 | 2076.2 |
| "D10" | 2014-07-04 00:00:00 | 2073.4 |
| "D10" | 2014-07-05 00:00:00 | 2048.7 |
| "D10" | 2014-07-06 00:00:00 | 2048.9 |
| "D10" | 2014-07-07 00:00:00 | 2006.4 |
| … | … | … |
| "D500" | 2012-09-19 00:00:00 | 9418.8 |
| "D500" | 2012-09-20 00:00:00 | 9365.7 |
| "D500" | 2012-09-21 00:00:00 | 9445.9 |
| "D500" | 2012-09-22 00:00:00 | 9497.9 |
| "D500" | 2012-09-23 00:00:00 | 9545.3 |
Every Polars DataFrame gains a .tk accessor once pytimetk is imported. This means you can send Polars data straight into the visual helpers without bouncing back to pandas.
When you pass engine="polars" the heavy lifting happens in Polars, but the result returns as a pandas frame (so it works with the rest of the ecosystem). This is handy for weekly/monthly summaries across many groups.
| id | date | value |
|---|---|---|
| str | datetime[ns] | f64 |
| "D10" | 2014-07-06 00:00:00 | 2061.8 |
| "D10" | 2014-07-13 00:00:00 | 2005.828571 |
| "D10" | 2014-07-20 00:00:00 | 1981.085714 |
| "D10" | 2014-07-27 00:00:00 | 1895.185714 |
| "D10" | 2014-08-03 00:00:00 | 1924.457143 |
The same pattern applies to rolling window computations. Here we build trailing 7-day mean and standard deviation per series, computed entirely with the Polars backend.
| id | date | value | value_rolling_mean_win_7 | value_rolling_std_win_7 |
|---|---|---|---|---|
| str | datetime[ns] | f64 | f64 | f64 |
| "D10" | 2014-07-03 00:00:00 | 2076.2 | null | null |
| "D10" | 2014-07-04 00:00:00 | 2073.4 | null | null |
| "D10" | 2014-07-05 00:00:00 | 2048.7 | null | null |
| "D10" | 2014-07-06 00:00:00 | 2048.9 | null | null |
| "D10" | 2014-07-07 00:00:00 | 2006.4 | null | null |
If you need a pandas DataFrame afterwards, just convert:
You can stay in Polars from end-to-end:
pl.DataFrame operations..tk helpers that support Polars inputs.This keeps the data in a columnar format for as long as possible, unlocking better cache usage and multithreading—without rewriting the entire pytimetk API.
engine support matrix.