Updated answer:
For a range of dates with monthly frequency on a specific day of month given in the start date (or the last feasible day of the month, accounting for different numbers of days of months and leap years), this function should work, at least for monthly frequencies:
import pandas as pd
def month_range_day(start=None, periods=None):
start_date = pd.Timestamp(start).date()
month_range = pd.date_range(start=start_date, periods=periods, freq='M')
month_day = month_range.day.values
month_day[start_date.day < month_day] = start_date.day
return pd.to_datetime(month_range.year*10000+month_range.month*100+month_day, format='%Y%m%d')
Example 1:
start_date = '2020-01-31'
month_range_day(start=start_date, periods=12)
Output:
DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30',
'2020-05-31', '2020-06-30', '2020-07-31', '2020-08-31',
'2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31'],
dtype='datetime64[ns]', freq=None)
Example 2:
start_date = '2019-01-29'
month_range_day(start=start_date, periods=12)
Output:
DatetimeIndex(['2019-01-29', '2019-02-28', '2019-03-29', '2019-04-29',
'2019-05-29', '2019-06-29', '2019-07-29', '2019-08-29',
'2019-09-29', '2019-10-29', '2019-11-29', '2019-12-29'],
dtype='datetime64[ns]', freq=None)
Previous Answer:
Assuming that you simply want the end-of-month frequencies, there is no need to use pd.DateOffset
:
import pandas as pd
start_date = '2018-09-01'
pd.date_range(start=start_date, periods=12, freq='M').strftime('%d-%m-%Y')
Output:
Index(['30-09-2018', '31-10-2018', '30-11-2018', '31-12-2018', '31-01-2019',
'28-02-2019', '31-03-2019', '30-04-2019', '31-05-2019', '30-06-2019',
'31-07-2019', '31-08-2019'],
dtype='object')
For more details, have a look at the offset aliases in pandas
. Changing the data format and type, if necessary, should be straight forward from here.