Here's the setup:
I have two (integer-indexed) columns, start
and month_delta
. start
has timestamps (its internal type is np.datetime64[ns]
) and month_delta
is integers.
I want to quickly produce the column that consists of the each datetime in start
, offset by the corresponding number of months in month_delta
. How do I do this?
Things I've tried that don't work:
apply
is too slow.- You can't add a series of
DateOffset
objects to a series ofdatetime64[ns]
dtype (or aDatetimeIndex
). - You can't use a Series of
timedelta64
objects either; Pandas silently converts month-based timedeltas to nanosecond-based timedeltas that are ~30 days long. (Yikes! What happened to not failing silently?)
Currently I'm iterating over all different values of month_delta
and doing a tshift
by that amount on the relevant part of a DatetimeIndex
I created, but this is a horrible kludge:
new_dates = pd.Series(pd.Timestamp.now(), index=start.index)
date_index = pd.DatetimeIndex(start)
for i in xrange(month_delta.max()):
mask = (month_delta == i)
cur_dates = pd.Series(index=date_index[mask]).tshift(i, freq='M').index
new_dates[mask] = cur_dates
Yuck! Any suggestions?