Generate list of months between interval in python

I found a very succinct way to do this with Pandas, sharing in case it helps anybody:


UPDATE: I’ve got it down to a one-liner with the help of this post 🙂

pd.date_range('2014-10-10','2016-01-07', 
              freq='MS').strftime("%Y-%b").tolist()

OLD ANSWER:

daterange = pd.date_range('2014-10-10','2016-01-07' , freq='1M') 
daterange = daterange.union([daterange[-1] + 1])  
daterange = [d.strftime('%y-%b') for d in daterange]

The second line prevents the last date from getting clipped off the list.

Leave a Comment