Skip to content Skip to sidebar Skip to footer

Python Pandas Dateoffset Using Value From Another Column

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

Solution 1:

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

Solution 2:

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]'))

Post a Comment for "Python Pandas Dateoffset Using Value From Another Column"