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],
[2017,1,15,2],
[2017,1,15,3],
[2017,1,15,4],
[2017,1,15,5],
[2017,1,15,6],
[2017,1,15,7]],
columns=['year','month','day','month_offset'])
df['date']=df.apply(lambda g: pd.datetime(g.year,g.month,g.day),axis=1)
df['offset']=df.apply(lambda g: pd.offsets.MonthEnd(g.month_offset),axis=1)
df['date_offset']=df.date+df.offset
This is the warning returned for last statement in the code snippet:
C:Python3.5.2.3WinPython-64bit-3.5.2.3python-3.5.2.amd64libsite-packagespandascoreops.py:533: PerformanceWarning: Adding/subtracting array of DateOffsets to Series not vectorized
"Series not vectorized", PerformanceWarning)
I would like to this to work as a vectorized operation because of the performance benefits.
Thanks.
EDIT
To end, comparison of methods following on from @john-zwinck:
import time
import pandas as pd
import numpy as np
df=pd.DataFrame([[2017,1,1,1],
[2017,1,1,2],
[2017,1,1,3],
[2017,1,1,4],
[2017,1,1,5],
[2017,1,1,6],
[2017,1,1,7]],
columns=['year','month','day','month_offset'])
df['mydate']=df.apply(lambda g:
pd.datetime(g.year,g.month,g.day),axis=1)
start_time=time.time()
df['pandas_offset']=df.apply(lambda g: g.mydate +
pd.offsets.MonthEnd(g.month_offset),axis=1)
end_time=time.time()
print('Method1 {} seconds'.format(end_time-start_time))
start_time=time.time()
df['numpy_offset']=(df.mydate.values.astype('M8[M]')+
df.month_offset.values * np.timedelta64(1, 'M')).astype('M8[D]') -
np.timedelta64(1, 'D')
end_time=time.time()
print('Method3 with numpy vectorization {} seconds'.format(end_time-
start_time))
The result:
index year month day month_offset mydate offset1 final
0 2017 1 1 1 2017-01-01 2017-01-31 2017-01-31
1 2017 1 1 2 2017-01-01 2017-02-28 2017-02-28
2 2017 1 1 3 2017-01-01 2017-03-31 2017-03-31
3 2017 1 1 4 2017-01-01 2017-04-30 2017-04-30
4 2017 1 1 5 2017-01-01 2017-05-31 2017-05-31
5 2017 1 1 6 2017-01-01 2017-06-30 2017-06-30
6 2017 1 1 7 2017-01-01 2017-07-31 2017-07-31
runfile('C:/bitbucket/test/vector_dates.py', wdir='C:/bitbucket/test')
Method 1 0.003999948501586914 seconds
Method 2 with numpy vectorization 0.0009999275207519531 seconds
Clearly numpy much faster
See Question&Answers more detail:
os