Generate list of months between interval in python

后端 未结 10 1173
庸人自扰
庸人自扰 2020-12-08 02:14

I want to generate a python list containing all months occurring between two dates, with the input and output formatted as follows:

date1 = \"2014-10-10\"  #         


        
10条回答
  •  萌比男神i
    2020-12-08 03:00

    With pandas, you can have a one liner like this:

    import pandas as pd
    
    date1 = "2014-10-10"  # input start date
    date2 = "2016-01-07"  # input end date
    
    month_list = [i.strftime("%b-%y") for i in pd.date_range(start=date1, end=date2, freq='MS')]
    

提交回复
热议问题