Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list of first day of month for given period

Tags:

python

list

I am trying find an efficient way of creating a list of dates only including the first day of the month for a given period. Something like this but better:

import datetime
dates = [
  datetime.date (2014, 4, 1),
  datetime.date (2014, 5, 1),
  datetime.date (2014, 6, 1),
  datetime.date (2014, 7, 1),
  datetime.date (2014, 8, 1),
  datetime.date (2014, 9, 1),
  datetime.date (2014, 10, 1),
  datetime.date (2014, 11, 1),
  datetime.date (2014, 12, 1),
  datetime.date (2015, 1, 1),
  datetime.date (2015, 2, 1)]

Alternatively, some direction on what to Google for this. Cheers!

like image 630
Gregology Avatar asked Mar 27 '14 18:03

Gregology


1 Answers

With pandas :

   dates= pd.date_range('2018-01-01','2020-01-01' , freq='1M')-pd.offsets.MonthBegin(1)

result :

`DatetimeIndex(['2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01',
               '2018-05-01', '2018-06-01', '2018-07-01', '2018-08-01',
               '2018-09-01', '2018-10-01', '2018-11-01', '2018-12-01',
               '2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01',
               '2019-05-01', '2019-06-01', '2019-07-01', '2019-08-01',
               '2019-09-01', '2019-10-01', '2019-11-01', '2019-12-01'],
              dtype='datetime64[ns]', freq='MS')
like image 188
MEA Avatar answered Oct 05 '22 13:10

MEA