Sub topic 8
Frequency aliases and date offsets
This chapter assumes you can build a DatetimeIndex and call pd.date_range with basic arguments. The code uses pandas 2.2 or later.
Aliases are short names for offsets
The freq argument of pd.date_range, resample, and to_period takes a string called a frequency alias. Each alias names a date offset, which is an object that knows how to step from one timestamp to the next. A string like "2h" is converted into an offset object, and pd.tseries.frequencies.to_offset shows that conversion directly.
import pandas as pd
print(pd.date_range("2024-01-31", periods=4, freq="ME"))
print(pd.date_range("2024-01-01", periods=4, freq="2h"))
print(pd.tseries.frequencies.to_offset("15min"))An alias can start with an integer. "2h" means every two hours and "15min" means every fifteen minutes.
Common aliases
| Alias | Meaning | Notes |
|---|---|---|
| `D` | Calendar day | Always 24 hours of clock time |
| `B` | Business day | Monday to Friday |
| `W` | Weekly | Anchored to Sunday, same as `W-SUN` |
| `ME` | Month end | Last calendar day of each month |
| `MS` | Month start | First day of each month |
| `QE` | Quarter end | Quarters ending in March, June, September, December |
| `YE` | Year end | December 31 |
| `h`, `min`, `s` | Hour, minute, second | Fixed-length steps |
Anchored frequencies
Some aliases are anchored to a point in a cycle. W-MON is weekly on Mondays. QE-JAN is quarterly with a year ending in January. Anchoring matters when your data follows a fiscal calendar or a weekly reporting day.
print(pd.date_range("2024-03-01", periods=3, freq="W-MON"))
print(pd.date_range("2024-01-01", periods=3, freq="QE-JAN"))If your start date is not on the anchor, pandas moves forward to the first date that is. This is why freq="MS" starting on January 31 yields February 1 as its first value.
Offsets as objects
You can use offsets directly, and you can add them to a Timestamp. This is useful for calendar arithmetic that a plain Timedelta cannot express, such as "the next business day" or "the end of this month".
start = pd.Timestamp("2024-03-08") # a Friday
print(start + pd.offsets.Day(1)) # 2024-03-09
print(start + pd.offsets.BDay(1)) # 2024-03-11, skips the weekend
print(start + pd.offsets.MonthEnd(1)) # 2024-03-31
print(start + pd.offsets.Week(weekday=0)) # 2024-03-11, the next MondayMonthEnd(1) rolls forward to the next month end. A timestamp already on a month end moves to the following month end. MonthEnd(0) leaves a timestamp that is already on a month end unchanged.
Custom business days
CustomBusinessDay accepts holidays and a custom weekly mask. You can pass it as freq to date_range.
cbd = pd.offsets.CustomBusinessDay(holidays=["2024-03-11"])
start = pd.Timestamp("2024-03-08")
print(start + cbd) # 2024-03-12, because Monday the 11th is a holiday
print(pd.date_range("2024-03-08", periods=3, freq=cbd))Common errors
- Using
Dfor "a day" across daylight saving changes.Dis calendar-aware on time-zone-aware indexes and keeps the wall-clock time.24halways adds exactly 24 hours, so the two can differ on the day clocks change. - Expecting
date_rangeto start on your date. Anchored aliases such asMS,ME, andWsnap to their anchor first. - Mixing up
MEandMS. Both produce monthly data, but the labels differ. A monthly sum labeled2024-01-31and one labeled2024-01-01look similar and cause off-by-one joins. - Passing an invalid alias. A typo such as
"ms"is accepted but means milliseconds, not month start. Check the index'sfreqattribute after you build it.
Exercise
What are the first value and the freq of pd.date_range("2024-01-31", periods=3, freq="MS"), and why?
Show answer
The first value is 2024-02-01, and the freq is MS. January 31 is not a month start, so pandas rolls forward to the first date that matches the anchor. The three values are 2024-02-01, 2024-03-01, and 2024-04-01.
Exercise
Starting from the Friday 2024-03-08, which offset gives 2024-03-11: pd.offsets.Day(1) or pd.offsets.BDay(1)?
Show answer
pd.offsets.BDay(1). It skips Saturday and Sunday and lands on Monday. Day(1) gives Saturday, 2024-03-09.