I am trying to use date_range
. I came across some values valid for freq
, like BME
and BMS
and I would like to be able to quickly look up the proper strings to get what I want.
What values are valid in Pandas 'Freq' tags?
You can find it called Offset Aliases:
A number of string aliases are given to useful common time series frequencies. We will refer to these aliases as offset aliases.
Alias Description
B business day frequency
C custom business day frequency
D calendar day frequency
W weekly frequency
M month end frequency
SM semi-month end frequency (15th and end of month)
BM business month end frequency
CBM custom business month end frequency
MS month start frequency
SMS semi-month start frequency (1st and 15th)
BMS business month start frequency
CBMS custom business month start frequency
Q quarter end frequency
BQ business quarter end frequency
QS quarter start frequency
BQS business quarter start frequency
A, Y year end frequency
BA, BY business year end frequency
AS, YS year start frequency
BAS, BYS business year start frequency
BH business hour frequency
H hourly frequency
T, min minutely frequency
S secondly frequency
L, ms milliseconds
U, us microseconds
N nanoseconds
You can find the entire list of valid frequencies by
pd.offsets.__all__
which returns
['BusinessDay', 'MonthBegin', 'Hour', 'Minute', 'Second', ...]
It turns out each of these is a class and all of them have a _prefix
attribute which is the alias. So, e.g., the alias of Minute
can be found by
pd.offsets.Minute._prefix # 'T'
# some other aliases
pd.offsets.MonthEnd._prefix # 'M'
pd.offsets.MonthBegin._prefix # 'MS'
In fact, if we look at the source code, the alias dict is constructed by accessing _prefix
of each offset class.
In addition to the offsets in jezrael's post, you can also specify an anchoring suffix for some frequencies.
Weekly frequencies can have anchors on any day of the week: W-SUN
, W-MON
, W-TUE
, W-WED
, W-THU
, W-FRI
and W-SAT
.
For example, to anchor weeks to end on Mondays, you can use W-MON
(note that it is case-insensitive).
pd.date_range('2022-01-01', '2022-02-01', freq='w-mon')
# DatetimeIndex(['2022-01-03', '2022-01-10', '2022-01-17', '2022-01-24', '2022-01-31'],
# dtype='datetime64[ns]', freq='W-MON')
Also quarterly and annual frequencies can have anchors on any month of the year; i.e. the following frequencies can have month as a suffix so that each frequency can be anchored to end on that month:
Q
QS
BQ
BQS
A
AS
BA
BAS
and the valid suffixes are JAN
, FEB
, MAR
, APR
, MAY
, JUN
, JUL
, AUG
, SEP
, OCT
, NOV
and DEC
.
For example, to anchor years to end at the beginning of May, you can use A-MAY
pd.date_range('2020-01-01', '2023-02-01', freq='AS-May')
# DatetimeIndex(['2020-05-01', '2021-05-01', '2022-05-01'], dtype='datetime64[ns]', freq='AS-MAY')