I am trying to do the following but is seems that vectorized operations in this mode are not supported.
import pandas as pd
df=pd.DataFrame([[2017,1,15,1],
Consider the following approach:
In [94]: df['date'] = pd.to_datetime(df[['year','month','day']])
In [95]: df['date_offset'] = df.apply(lambda x: x['date'] + pd.offsets.MonthEnd(x['month_offset']), axis=1)
In [96]: df
Out[96]:
year month day month_offset date date_offset
0 2017 1 15 1 2017-01-15 2017-01-31
1 2017 1 15 2 2017-01-15 2017-02-28
2 2017 1 15 3 2017-01-15 2017-03-31
3 2017 1 15 4 2017-01-15 2017-04-30
4 2017 1 15 5 2017-01-15 2017-05-31
5 2017 1 15 6 2017-01-15 2017-06-30
6 2017 1 15 7 2017-01-15 2017-07-31
A truly vectorized way to do this is to construct an array of numpy.timedelta64
from month_offset
, add this to the array of dates, then subtract numpy.timedelta64(1, 'D')
to go back to the last day of the previous month.
Solutions using apply(lambda)
are likely to be much slower. And as the warning said, some Pandas date offset operations are not vectorized. If your data are large, it's better to avoid them. The NumPy facilities like busday_offset()
and timedelta64
are fully performant.