It is worth noting that pandas.date_range()
only includes dates within the defined interval, which may not be expected :
start = "2020-03-08"
end = "2021-03-08"
pd.date_range(start, end, freq='MS')
results in
DatetimeIndex(['2020-04-01', '2020-05-01', '2020-06-01', '2020-07-01',
'2020-08-01', '2020-09-01', '2020-10-01', '2020-11-01',
'2020-12-01', '2021-01-01', '2021-02-01', '2021-03-01'],
dtype='datetime64[ns]', freq='MS')
For MS
, a workaround to include the first day of the opening month is to work only with the year and month of the start date :
pd.date_range(start[:7], end, freq='MS')
will then give
DatetimeIndex(['2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01',
'2020-07-01', '2020-08-01', '2020-09-01', '2020-10-01',
'2020-11-01', '2020-12-01', '2021-01-01', '2021-02-01',
'2021-03-01'],
dtype='datetime64[ns]', freq='MS')
If you wish to keep the same starting day for each month, you can then add the offset with pd.DateOffset()
:
pd.date_range(start[:7], end, freq='MS') + pd.DateOffset(days=7)
will give
DatetimeIndex(['2020-03-08', '2020-04-08', '2020-05-08', '2020-06-08',
'2020-07-08', '2020-08-08', '2020-09-08', '2020-10-08',
'2020-11-08', '2020-12-08', '2021-01-08', '2021-02-08',
'2021-03-08'],
dtype='datetime64[ns]', freq=None)
As mentioned in comments, note that trouble may come with this workaround for offsets higher or equals to 28.