Sub topic 3
How resample() groups rows into bins
This chapter assumes you can build a pandas Series or DataFrame, select rows by label, and read frequency strings such as "D" and "W". It explains what resample() does internally. Later chapters cover downsampling, upsampling, and bin edges in detail.
Resampling is a groupby on time
resample() works like groupby(). It splits rows into groups, here called bins, and you then apply a function to each group. The bins are consecutive, equal-length time intervals. They are built from the frequency string you pass and from the range of the DatetimeIndex.
Calling resample() alone does no computation. It returns a Resampler object that remembers the frequency and the bin settings. Nothing happens until you call an aggregation such as sum(), mean(), or size() on it.
A worked example
import pandas as pd
idx = pd.date_range("2024-01-01", periods=6, freq="D")
s = pd.Series([1, 2, 3, 4, 5, 6], index=idx)
r = s.resample("3D")
print(r.sum())
print(r.size())The first bin starts at the first day in the data, 2024-01-01. Each bin covers three days, so the second bin starts on 2024-01-04. The sums are 6 (1+2+3) and 15 (4+5+6). The size() call returns the number of rows in each bin, which is 3 for both.
Reusing r for two calls shows that the Resampler holds the bin rules and the aggregation is a separate step.
What the result looks like
- The result has one row per bin, not one row per original row.
- The new index holds the bin labels, which by default are the left edge of each bin.
- The index has a fixed frequency, shown as
Freq: 3Dwhen you print a Series. - The original index must be a DatetimeIndex, a PeriodIndex, or a TimedeltaIndex. Otherwise pandas raises a
TypeError.
Bins are built across the whole range
Bins cover the full span from the first row to the last row, with no gaps between them. If no row falls inside a bin, the bin still appears in the result. Its value depends on the aggregation, as a later chapter explains.
Rows do not need to be evenly spaced or sorted by time before you resample. Each row goes into the bin that contains its timestamp. Sorting the index first is still a good habit, because it makes the result easier to check by eye.
Common errors
- TypeError about the index. The index is not time-based. Convert a date column with
pd.to_datetime()and useset_index(), or passon="date"toresample(). - Expecting output without an aggregation.
s.resample("W")on its own prints aResamplerobject, not data. Add.sum(),.mean(), or another method. - Confusing row counts. The result has one row per bin. A series of 10 daily rows resampled to
"5D"gives 2 rows, not 10.
Exercise
A Series has the values 0 to 9 and a daily DatetimeIndex starting on 2024-01-01. What are the index labels and values of s.resample("5D").sum()?
Show answer
There are two bins. The first is labeled 2024-01-01 and holds days 1 to 5, so its sum is 0+1+2+3+4 = 10. The second is labeled 2024-01-06 and holds days 6 to 10, so its sum is 5+6+7+8+9 = 35.