Skip to content

pandas/row-window-on-uneven-times

Rule row-window-on-uneven-times
Pack pandas
Severity warning
Fix none, edit by hand
Library versions every version

A rolling window given as a number counts rows, even with on= naming a time column, so on days with gaps it reaches further back than you meant. Pass the span as an offset, as in rolling("7D", on="day").

weekly = df.rolling(7, on="day")["sold"].sum()
weekly = df.rolling("7D", on="day")["sold"].sum()

The rule reports rolling with an integer window and on=. A window given as a number counts rows, also when on= names a time column. rolling(3) without on= gave the same numbers as with it. The table shows the sum for 2024-01-10 on six days with gaps.

Call Sum for 2024-01-10 Rows in the window
rolling(3, on="day").sum() 14.0 three rows that reach back 8 days
rolling("3D", on="day").sum() 8.0 the one row of the last 3 days

The rule misses a row window on a frame with the times in its index, because xpd can't see what the index holds.

xpd has no automatic fix, so make the change by hand.

A time window starts with min_periods=1, where the row window gives missing values until it has 7 rows.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

on=$C names the time column, but a window given as a number counts rows. On days with gaps, rolling(3, on="day") summed three rows that reached back 8 days and gave 14.0, where rolling("3D", on="day") gave 8.0 for the last 3 days. Pass the span as an offset, as in rolling("7D", on=$C).

Browse the other rules of the pandas pack.