Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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,

like image 371
horatio1701d Avatar asked May 08 '26 07:05

horatio1701d


1 Answers

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]'))
like image 133
ivallesp Avatar answered May 09 '26 21:05

ivallesp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!