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!
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With