Pandas date_range to generate monthly data at beginning of the month

前端 未结 1 1001
小蘑菇
小蘑菇 2020-12-13 12:12

I\'m trying to generate a date range of monthly data where the day is always at the beginning of the month:

pd.date_range(start=\'1/1/1980\', end=\'11/1/1991         


        
1条回答
  •  时光说笑
    2020-12-13 12:36

    You can do this by changing the freq argument from 'M' to 'MS':

    d = pandas.date_range(start='1/1/1980', end='11/1/1990', freq='MS')    
    print(d)
    

    This should now print:

    DatetimeIndex(['1980-01-01', '1980-02-01', '1980-03-01', '1980-04-01',
                   '1980-05-01', '1980-06-01', '1980-07-01', '1980-08-01',
                   '1980-09-01', '1980-10-01', 
                   ...
                   '1990-02-01', '1990-03-01', '1990-04-01', '1990-05-01',
                   '1990-06-01', '1990-07-01', '1990-08-01', '1990-09-01',
                   '1990-10-01', '1990-11-01'],
                  dtype='datetime64[ns]', length=131, freq='MS', tz=None)
    

    Look into the offset aliases part of the documentation. There it states that 'M' is for the end of the month (month end frequency) while 'MS' for the beginning (month start frequency).

    0 讨论(0)
提交回复
热议问题