I am parsing a huge ascii file with dates assigned to entries. So, I found myself using datetime package in parallel to numpy.datetime64 to add array capabilities. I know that the pandas package is probably most recommended to be used for date, however try to pull this off without pandas. I have been looking around for a neat way to add/subtract a certain datestep like one year, or 3 month from a datetime64 object.
Currently, I am converting dt64 object to dt object and use replace function to change the year for example and have to convert it back to dt64 afterward which is a bit messy to me. So, I would appreciate if anyone has a better solution using only numpy.datetime64 format.
Example: Converting a "YYYY-12-31" to "(YYYY-1)-12-31"
a = np.datetime64(2014,12,31) # a is dt64 object
b = a.astype(object) # b is dt object converted from a
c = np.datetime64( b.replace(b.year-1)) # c is dt64 object shifted back 1 year (a -1year)
You can use the numpy.timedelta64 object to perform time delta calculations on a numpy.datetime64 object, see Datetime and Timedelta Arithmetic.
Since a year can be either 365 or 366 days, it is not possible to substract a year, but you could substract 365 days instead:
results in:
numpy.datetime64('2013-12-31')
How about:
Is vector based pandas usage and I think it deals with leap years.