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.
import numpy as np
def compose_date(years, months=1, days=1, weeks=None, hours=None, minutes=None,
seconds=None, milliseconds=None, microseconds=None, nanoseconds=None):
years = np.asarray(years) - 1970
months = np.asarray(months) - 1
days = np.asarray(days) - 1
types = ('<M8[Y]', '<m8[M]', '<m8[D]', '<m8[W]', '<m8[h]',
'<m8[m]', '<m8[s]', '<m8[ms]', '<m8[us]', '<m8[ns]')
vals = (years, months, days, weeks, hours, minutes, seconds,
milliseconds, microseconds, nanoseconds)
return sum(np.asarray(v, dtype=t) for t, v in zip(types, vals)
if v is not None)
years = [1990, 1992, 1995, 1994]
months = [1, 6, 3, 7]
days = [3, 20, 14, 27]
print(compose_date(years, months, days))
yields
array(['1990-01-03', '1992-06-20', '1995-03-14', '1994-07-27'], dtype='datetime64[D]')
This can be done with pandas without explicit looping (the code is taken from the pandas documentation):
df = pd.DataFrame({'year': [2015, 2016],
....: 'month': [2, 3],
....: 'day': [4, 5],
....: 'hour': [2, 3]})
....:
In [32]: pd.to_datetime(df)
Out[32]:
0 2015-02-04 02:00:00
1 2016-03-05 03:00:00
dtype: datetime64[ns]
of course you can 'floor' the date times to 'day' and return a numpy array with .values