How to include end date in pandas date_range method?

前端 未结 7 1679
[愿得一人]
[愿得一人] 2021-01-02 02:42

From pd.date_range(\'2016-01\', \'2016-05\', freq=\'M\', ).strftime(\'%Y-%m\'), the last month is 2016-04, but I was expecting it to be 2016

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-02 03:24

    A way to do it without messing with figuring out month ends yourself.

    pd.date_range(*(pd.to_datetime(['2016-01', '2016-05']) + pd.offsets.MonthEnd()), freq='M')
    
    DatetimeIndex(['2016-01-31', '2016-02-29', '2016-03-31', '2016-04-30',
               '2016-05-31'],
              dtype='datetime64[ns]', freq='M')
    

提交回复
热议问题