DataFrame.rolling produces incorrect result if window boundaries are not monotone
Checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of Polars.
Reproducible example
from datetime import datetime
import polars as pl
df = pl.DataFrame({
"t": [
datetime(2025, 1, 30, 12),
datetime(2025, 1, 31, 1),
datetime(2025, 2, 28, 6),
],
"v": [0, 1, 2],
}).set_sorted("t")
print(df)
result = (
df.rolling("t", offset="1mo", period="1d", closed="right")
.agg(pl.col("v"))
.with_columns(lower=pl.col("t").dt.offset_by("1mo"), upper=pl.col("t").dt.offset_by("1mo1d"))
).select("t", "lower", "upper", "v")
print(result)shape: (3, 2)
┌─────────────────────┬─────┐
│ t ┆ v │
│ --- ┆ --- │
│ datetime[μs] ┆ i64 │
╞═════════════════════╪═════╡
│ 2025-01-30 12:00:00 ┆ 0 │
│ 2025-01-31 01:00:00 ┆ 1 │
│ 2025-02-28 06:00:00 ┆ 2 │
└─────────────────────┴─────┘
shape: (3, 4)
┌─────────────────────┬─────────────────────┬─────────────────────┬───────────┐
│ t ┆ lower ┆ upper ┆ v │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ datetime[μs] ┆ datetime[μs] ┆ list[i64] │
╞═════════════════════╪═════════════════════╪═════════════════════╪═══════════╡
│ 2025-01-30 12:00:00 ┆ 2025-02-28 12:00:00 ┆ 2025-03-01 12:00:00 ┆ [] │
│ 2025-01-31 01:00:00 ┆ 2025-02-28 01:00:00 ┆ 2025-03-01 01:00:00 ┆ [] │
│ 2025-02-28 06:00:00 ┆ 2025-03-28 06:00:00 ┆ 2025-03-29 06:00:00 ┆ [] │
└─────────────────────┴─────────────────────┴─────────────────────┴───────────┘Log output
Issue description
If using calendrical offsets in rolling, the window boundaries are not guaranteed to be monotone: that is, a row may re-enter a window having previously left it. In this case, the implementation must "refill" from old data.
Monotone windows often admit an algorithmically faster implementation, so we would like to not always refill. The specialised rolling kernels do this.
The implementation of group_by_dynamic handles this case by avoiding dispatch to rolling kernels for potentially non-monotone windows:
# polars-time/src/group_by/dynamic.rs:~304
// The upper bound of a window is not a monotonic function of its lower
// bound (month clamping, DST), so the group ends may move backwards.
let monotonic = slice_groups_are_monotonic(&groups);
PolarsResult::Ok(GroupsType::new_slice(groups, overlapping, monotonic))In contrast the rolling implementation unilaterally sets the monotone flag to true
# polars-time/src/group_by/dynamic.rs:~411
PolarsResult::Ok(GroupsType::new_slice(groups, true, true))So we end up dispatching to specialised kernels when it is invalid.
Expected behavior
I would expect the answer:
┌─────────────────────┬─────────────────────┬─────────────────────┬───────────┐
│ t ┆ lower ┆ upper ┆ v │
│ --- ┆ --- ┆ --- ┆ --- │
│ datetime[μs] ┆ datetime[μs] ┆ datetime[μs] ┆ list[i64] │
╞═════════════════════╪═════════════════════╪═════════════════════╪═══════════╡
│ 2025-01-30 12:00:00 ┆ 2025-02-28 12:00:00 ┆ 2025-03-01 12:00:00 ┆ [] │
│ 2025-01-31 01:00:00 ┆ 2025-02-28 01:00:00 ┆ 2025-03-01 01:00:00 ┆ [2] │
│ 2025-02-28 06:00:00 ┆ 2025-03-28 06:00:00 ┆ 2025-03-29 06:00:00 ┆ [] │
└─────────────────────┴─────────────────────┴─────────────────────┴───────────┘Since the second row's window contains the third row's t value.
A cursory glance at the streaming rolling aggregation suggests its implementation also suffers this same problem (but group_by_dynamic again does not).
Installed versions
--------Version info---------
Polars: 2.0.0-rc.2
Build: da47b7405f0e2d188e71cce7c2d588c695f4098d
Index type: UInt32
Platform: Linux-6.8.0-139-generic-x86_64-with-glibc2.39
Python: 3.13.15 | packaged by conda-forge | (main, Sep 2 2026, 22:01:58) [GCC 15.3.0]
Runtime: rt32
----Optional dependencies----
Azure CLI <not installed>
adbc_driver_manager <not installed>
altair <not installed>
azure.identity <not installed>
boto3 1.40.70
cloudpickle 3.1.2
connectorx <not installed>
deltalake <not installed>
fastexcel <not installed>
fsspec 2026.7.0
gevent <not installed>
google.auth 2.58.0
great_tables <not installed>
matplotlib 3.10.9
numpy 2.4.6
openpyxl 3.1.5
pandas 3.0.6
polars_cloud <not installed>
pyarrow 25.0.0
pydantic 2.13.5
pyiceberg <not installed>
sqlalchemy 2.0.54
torch 2.13.0
xlsx2csv <not installed>
xlsxwriter 3.2.9
Source: pola-rs/polars