How to add a flexible number of months via a for loop

Viewed 53

I am a new python user and have the below code. The goal I am trying to accomplish is to write a for loop that dynamically changes the number in the month's input so that each new iteration of date ranges will go out until 12/1/2033. Currently, dates in the latter half of the loop will extend far past that. I was thinking of doing some type of loop in which pd.DateOffset(months=n), but I have not been able to get it to work.

import pandas as pd
date_range1 = pd.date_range('01/01/2020','01/01/2022',freq='MS')

for dates in date_range1:
    StartDate = dates
    endDate = StartDate + pd.DateOffset(months=180)

    date_range = pd.date_range(StartDate,endDate,freq='MS')
0 Answers
Related