I have a numpy array which has a long list of datetimes. I was wondering is there a way to add a year to all values of the array at once without using a for loop? eg. Using some numpy or datetime module?
>>> import datetime
>>> import numpy as np
>>> dts.shape
(64580,)
>>> dts[:5]
array([datetime.date(2000, 1, 15), datetime.date(2000, 1, 15),
datetime.date(2000, 1, 15), datetime.date(2000, 1, 15),
datetime.date(2000, 1, 15)], dtype=object)
>>> new_dts = somemodule.somefunctionforaddingyearorsomething(dts, year=1)
>>> new_dts
array([datetime.date(2001, 1, 15), datetime.date(2001, 1, 15),
datetime.date(2001, 1, 15), datetime.date(2001, 1, 15),
datetime.date(2001, 1, 15)], dtype=object)
Note: Day of each date is always set to day 15 as the dates represent monthly mean data.
I have implemented it using a for loop however this can be computationally slow..
The code for that is here:
def add_year_to_Datelist(dl):
dts = dl.dates.copy()
for idx, date in enumerate(dts):
dts[idx] = date.replace(year=date.year + 1)
dl.set_dates(dts)
return dl
Cheers