Code
today = pd.to_datetime('today').strftime("%d/%m/%Y")
last = (pd.to_datetime(today) - pd.DateOffset(years=1)).strftime("%d/%m/%Y")
listofmonths = pd.date_range(start=last, end=today, freq='MS')
listofmonths
Output:
DatetimeIndex(['2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01',
'2021-10-01', '2021-11-01', '2021-12-01', '2022-01-01'],
dtype='datetime64[ns]', freq='MS')
Note: Refer the output in which i am getting only 8 months and i want past 12 months first date from the current date
Expected Output:
**DatetimeIndex(['2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01',
'2021-10-01', '2021-11-01', '2021-12-01','2021-01-01', '2022-02-01',
'2022-03-01','2022-04-01', '2022-05-01', '2022-06-01',],
dtype='datetime64[ns]', freq='MS')**