Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Date Range Monthly on Specific Day of Month

In Pandas, I know you can use anchor offsets to specify more complicated reucrrences: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#anchored-offset

I want to specify a date_range such that it is monthly on the nth day of each month. What is the best syntax to do that with? I'm imaginging something similar to this which specifies a recurrence every 2 weeks on Friday:

schedule = pd.date_range(start=START_STR, periods=26, freq="2W-FRI")
like image 557
themaestro Avatar asked Oct 13 '16 22:10

themaestro


1 Answers

No need to re-invent the wheel. Use DateOffset from pandas:

import pandas as pd
from pandas.tseries.offsets import DateOffset
from datetime import date

date1 = date(2019,1,29)
pd.date_range(date1, periods=12, freq=DateOffset(months=1))

Output:

DatetimeIndex(['2019-01-29', '2019-02-28', '2019-03-28', '2019-04-28',
               '2019-05-28', '2019-06-28', '2019-07-28', '2019-08-28',
               '2019-09-28', '2019-10-28', '2019-11-28', '2019-12-28'],
              dtype='datetime64[ns]', freq='<DateOffset: months=1>')
like image 191
cpage Avatar answered Oct 16 '22 11:10

cpage