Complete worked example
python
import pandas as pd
def inspect_example(rows):
if any(type(r.get("events")) is not int or r["events"] < 0 for r in rows):
raise ValueError("nonnegative whole counts required")
frame = pd.DataFrame(rows, columns=["day", "events"])
dates = pd.to_datetime(frame["day"], format="%Y-%m-%d", utc=True, errors="raise")
if dates.duplicated().any():
raise ValueError("duplicate day")
counts = pd.Series(pd.array(frame["events"], dtype="Int64"), index=dates).sort_index()
if len(counts):
counts = counts.reindex(pd.date_range(counts.index.min(), counts.index.max(), freq="D"), fill_value=0)
mean = counts.rolling(window=3, min_periods=3).mean()
return pd.DataFrame({"day": counts.index.strftime("%Y-%m-%d"), "events": counts.to_numpy(), "mean": mean.to_numpy()}).astype({"day": "string", "events": "Int64", "mean": "Float64"})- Reasoning
- Parse strict YYYY-MM-DD keys as UTC midnight instants and reject duplicate days or negative/non-integer counts. Sort by date, construct date_range from the endpoints and reindex the event Series onto that complete grid. Only then apply rolling(window=3, min_periods=3). Return formatted UTC days, nullable integer event counts and nullable floating means. Trace counts three on November first and six on November third. The completed second day has zero events. The third day's mean is three, based on three, zero and six. A direct rolling calculation on the two original rows cannot express that daily population.