I would like to create vectors of NumPy datetime64 objects from 1-D vectors of years, months, and days, and also go the reverse direction, that is extracting vectors of year
I don't know of a way to do it without some sort of looping, but I inlined it a bit with a list comprehension:
years = [1990, 1992, 1995, 1994]
months = [1, 6, 3, 7]
days = [3, 20, 14, 27]
np.array(['{0[0]}-{0[1]}-{0[2]}'.format(x) for x in zip(years, months, days)], dtype='datetime64')
Going back the other way, you have to convert each item to a regular datetime
. You can do this by calling astype(object)
, which works for the whole array or for individual objects. Which one you do probably depends on how your using the data.