Sub topic 2
Frequency strings: describing the target frequency
This chapter assumes you can create a DatetimeIndex and know basic Series and DataFrame indexing. Every resample() call needs a rule that says how wide each bin is. Most often that rule is a frequency string such as "W" or "MS". This chapter explains how those strings are built.
Anatomy of a frequency string
A frequency string has up to three parts:
- An optional integer multiplier, such as
3or15. - An alias that names the unit, such as
D,W, ormin. - An optional anchor after a hyphen, such as
-FRIor-JAN.
So "3D" means every three days, "15min" means every 15 minutes, and "W-FRI" means weeks that end on Friday. Without a number, the multiplier is 1.
Common aliases
| Alias | Meaning | Notes |
|---|---|---|
| `D` | Calendar day | Always 24 hours of clock time |
| `B` | Business day | Monday to Friday |
| `W` | Week | Same as `W-SUN`, bins end on Sunday |
| `MS` | Month start | Bins begin on the 1st |
| `ME` | Month end | Bins end on the last day |
| `QS` | Quarter start | |
| `YS` | Year start | |
| `h` | Hour | |
| `min` | Minute | |
| `s` | Second |
Start aliases (MS, QS, YS) and end aliases (ME, QE, YE) describe the same size of bin. They differ in which date appears as the label.
How pandas reads the string
Internally, pandas converts the string to an offset object with to_offset. You can call it yourself to check what a string means.
import pandas as pd
from pandas.tseries.frequencies import to_offset
for rule in ["D", "3D", "W", "W-FRI", "MS", "ME", "15min"]:
print(rule, "->", repr(to_offset(rule)))The output shows objects such as a day offset, a three-day offset, and a week offset anchored to a weekday. If a string is invalid, to_offset raises a ValueError, which is faster than finding out inside a long pipeline.
Using the string in resample()
The multiplier and the anchor change the bins directly. This example resamples ten daily values into bins that end on Friday.
import pandas as pd
idx = pd.date_range("2024-01-01", periods=10, freq="D")
s = pd.Series(range(10), index=idx)
print(s.resample("3D").sum())
print(s.resample("W-FRI").sum())The "3D" bins start at the first timestamp and hold 3, 12, 21, and 9. The first W-FRI bin is labeled 2024-01-05 and holds 0+1+2+3+4 = 10. The second is labeled 2024-01-12 and holds 5+6+7+8+9 = 35.
You can pass an offset object instead of a string. pd.offsets.Week(weekday=4) is equivalent to "W-FRI". Strings are shorter, and offset objects are easier to build from variables.
Common mistakes
- Using
"M"and expecting month start. It meant month end, and in current versions you writeME. UseMSfor month start. - Writing
"W"and assuming bins end on Saturday or begin on Monday."W"ends on Sunday. UseW-SATor another anchor for a different week. - Using
"D"across a daylight saving change. The offset is a fixed 24 hours, so bins in a time zone can shift. A later chapter covers this. - Mixing up
"m"and"min". Minutes aremin, and a baremis not a valid alias.
Exercise
You have a daily series and want the sum for each calendar quarter, labeled with the first day of the quarter (for example 2024-01-01). Which frequency string do you pass to resample(), and why not "QE"?
Show answer
Pass "QS". It is the quarter-start alias, so each bin is labeled with the first day of the quarter. "QE" makes the same size bins but labels them with the last day of the quarter, such as 2024-03-31.