Reputation: 9169
I was thinking this would be very easy but the below is not working for what I want. Just trying to compute a new date column by adding days to a pre-existing datetime column using values from another column. My 'offset' column below just has 1 digit numbers.
df['new_date'] = df['orig_date'].apply(lambda x: x + pd.DateOffset(days=df['offset']))
Error: unsupported type for timedelta days component: Series
thank you,
Upvotes: 9
Views: 9293
Reputation: 2212
A bit late, but it may be useful for others with the same doubt. There is a way of doing this in a vectorised way, so it is much faster.
df['new_date'] = df['orig_date'] + df['offset'].astype('timedelta64[D]'))
Upvotes: 17
Reputation: 128968
In [12]: df['C'] = df['A'] + df['B'].apply(pd.offsets.Day)
In [13]: df
Out[13]:
A B C
0 2013-01-01 0 2013-01-01
1 2013-01-02 1 2013-01-03
2 2013-01-03 2 2013-01-05
3 2013-01-04 3 2013-01-07
4 2013-01-05 4 2013-01-09
Upvotes: 12