I'd like to create a DateTimeIndex
in Pandas from broadcasted arrays of years, months, days, hours, etc. This is relatively straightforward to do via a list comprehension; e.g.
import numpy as np
import pandas as pd
def build_DatetimeIndex(*args):
return pd.DatetimeIndex([pd.datetime(*tup)
for tup in np.broadcast(*args)])
For example:
>>> year = 2012
>>> months = [1, 2, 5, 6]
>>> days = [1, 15, 1, 15]
>>> build_DatetimeIndex(year, months, days)
DatetimeIndex(['2012-01-01', '2012-02-15', '2012-05-01', '2012-06-15'],
dtype='datetime64[ns]', freq=None)
But due to the list comprehension, this becomes rather slow as the size of the inputs grow. Is there a built-in way to do this in Pandas, or is there any way to define build_DatetimeIndex
in terms of a fast, vectorized operation?