Sub topic 15
Resampling with PeriodIndex
This chapter assumes you can resample a Series with a DatetimeIndex and know the frequency aliases such as "M" and "Q". It shows what changes when the index holds periods instead of timestamps.
Timestamps and periods
A Timestamp is a single instant. A Period is a span of time, such as the whole of March 2024. A PeriodIndex is an index of such spans.
Periods suit data that describes a span, such as monthly revenue or quarterly GDP. A monthly figure is not a measurement taken at midnight on the first of the month, and a period says so directly.
You can build a PeriodIndex directly with pd.period_range(), or convert from timestamps with to_period():
import pandas as pd
ts = pd.Series(
[1, 2, 3],
index=pd.to_datetime(["2024-01-15", "2024-02-10", "2024-02-20"]),
)
print(ts.to_period("M"))Each timestamp is replaced by the month that contains it. Both February values now share the label 2024-02.
Downsampling with periods
resample() works on a PeriodIndex with the same call you already know. The result is also indexed by periods:
import pandas as pd
idx = pd.period_range("2024-01", periods=6, freq="M")
s = pd.Series([10, 20, 30, 40, 50, 60], index=idx)
print(s.resample("Q").sum())The output has two rows. 2024Q1 holds 10 + 20 + 30 = 60 and 2024Q2 holds 40 + 50 + 60 = 150.
Every bin is a whole calendar period, so the labels are the periods themselves. The origin and offset options from the bin-alignment chapter are for timestamp bins that start at an arbitrary time. They are not how you shift calendar periods.
Upsampling and convention
When you move from a coarse period to a finer one, pandas must decide where each value lands. A quarterly value could sit in the first month of the quarter or the last. The convention argument controls this. It accepts "start" (the default) and "end".
import pandas as pd
q = pd.Series(
[100, 200],
index=pd.period_range("2024Q1", periods=2, freq="Q"),
)
print(q.resample("M").ffill())
print(q.resample("M", convention="end").ffill())With the default "start", the 100 lands in January and the 200 in April. The ffill() call then repeats each value across the rest of its quarter. With convention="end", the values land in the last month of each quarter. Fill methods work as in the upsampling chapter, but the position of the known values depends on convention.
Moving between the two index types
You can switch representations when a task suits one better.
to_period()turns aDatetimeIndexinto aPeriodIndex.to_timestamp()turns aPeriodIndexback into timestamps. By default it uses the start of each period. Passhow="end"for the end.
Another common workflow is to resample timestamps and then convert the result:
import pandas as pd
idx = pd.date_range("2024-01-01", periods=90, freq="D")
s = pd.Series(range(90), index=idx)
monthly = s.resample("MS").sum().to_period("M")
print(monthly)This gives the same sums as resampling by month and relabels each row with a month period. That is often the clearest label for monthly reports.
Common errors
- Passing a period index to code that expects timestamps. Plotting and merging with timestamp data may fail or misalign. Convert with
to_timestamp()first. - Forgetting that period frequencies describe a span.
"M"on aPeriodIndexmeans a calendar month, and"Q"means a calendar quarter. - Expecting time zones. A
Periodhas no time zone, so daylight saving time does not apply. If you need zone-aware bins, resample timestamps and then callto_period().
Exercise
A Series has a monthly PeriodIndex for 2024 (12 values). You call resample("Q").sum(). How many rows does the result have, and what does convention control when you later resample the quarterly result back to months?
Show answer
The result has 4 rows, one per quarter (2024Q1 to 2024Q4). When upsampling quarters to months, convention chooses whether each quarterly value lands in the first month of its quarter ("start", the default) or the last month ("end").