typestar

Resampling a time series in Python

resample is groupby for time: pick a frequency, pick an aggregate.

import pandas as pd

index = pd.date_range("2026-07-01", periods=10, freq="D")
runs = pd.Series(range(90, 100), index=index, name="tpm")

print(runs.resample("W").mean().round(1))
print(runs.resample("3D").agg(["min", "max"]))
print(runs.resample("ME").sum())

How it works

  1. ME is month end, W week, D day — the alias decides the bucket.
  2. Empty buckets appear, so decide what filling them means.
  3. asfreq reindexes without aggregating.

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
259
Tokens
106
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 58 seconds.

Type this snippet

Step 2 of 3 in Time series, step 24 of 26 in pandas.

← Previous Next →