A support team logged 10 tickets on January 1, 14 on January 2, and 22 on January 4. The log has no January 3 entry. On January 4, the average of the last three recorded counts is about 15.33 tickets, and the average over the three-day window is 18.

Use rolling(3) in pandas to count rows or rolling("3D") to set a time window. The dates in each window explain why the two averages differ.

The log shows 10 tickets on January 1, 14 on January 2, no row on January 3, and 22 on January 4. The three-row average is 15.33 and the three-day average is 18.
On January 4, the last three records span four calendar dates. The three-day window contains two recorded counts.Credit: MathBlog

Sort the ticket log by date

This fictional log has one row per recorded day. Parse the date strings, sort the rows, and use the dates as the index.

Ticket log in its original order
Date Tickets
2026-01-05 26
2026-01-01 10
2026-01-04 22
2026-01-02 14
import pandas as pd

log = pd.DataFrame({
    "date": ["2026-01-05", "2026-01-01", "2026-01-04", "2026-01-02"],
    "tickets": [26, 10, 22, 14],
})
log["date"] = pd.to_datetime(log["date"])
log = log.sort_values("date").set_index("date")
tickets = log["tickets"]

log["last_3_rows"] = tickets.rolling(3, min_periods=3).mean()
log["last_3_days"] = tickets.rolling(
    "3D", min_periods=1, closed="right"
).mean()

print(log.round(2).to_string())

The code rounds the printed table to two decimal places. The columns in log keep their full calculated precision.

            tickets  last_3_rows  last_3_days
date
2026-01-01       10          NaN         10.0
2026-01-02       14          NaN         12.0
2026-01-04       22        15.33         18.0
2026-01-05       26        20.67         24.0

See which dates go into each average

The three-row window on January 4 includes January 1, 2, and 4. Its average is \((10+14+22)/3\), or about 15.33. January 1 and 2 show NaN because min_periods=3 requires three nonmissing counts.

With closed="right", the three-day window on January 4 includes its end date and excludes the boundary three days earlier, January 1. Only January 2 and 4 have counts in that window, so the average is \((14+22)/2=18\). On January 5, the window excludes January 2 and averages 22 and 26 to get 24.

Decide how to handle missing days

Use a row window for the last three measurements, even when the dates are unevenly spaced. Use a time window when the reporting rule is based on elapsed time. Label the average so readers know it uses the available counts in that period.

Set the time window’s min_periods to 3 if the report requires three recorded counts before showing an average. Every three-day window in this example would then return NaN. If you need a row for every day, add the absent dates with reindex. Keep January 3 missing unless you can verify that the team received zero tickets that day.

The example was run with Python 3.11.3 and pandas 2.3.3.

Further reading