pandas add column of different days to datetime column

Viewed 268

I have a DataFrame that looks like this.

                          time  daysToExp
0   2020-08-11 13:53:57.083388          0
1   2020-08-11 13:53:57.083388          1
2   2020-08-11 13:53:57.083388          3
3   2020-08-11 13:53:57.083388          4
4   2020-08-11 13:53:57.083388          8

I would like to add the daysToExp column to the time column and end with a result to something like this.

                          time  daysToExp                         Date
0   2020-08-11 13:53:57.083388          0   2020-08-11 13:53:57.083388
1   2020-08-11 13:53:57.083388          1   2020-08-12 13:53:57.083388
2   2020-08-11 13:53:57.083388          3   2020-08-14 13:53:57.083388
3   2020-08-11 13:53:57.083388          4   2020-08-15 13:53:57.083388
4   2020-08-11 13:53:57.083388          8   2020-08-19 13:53:57.083388

The closest command I found is this, df['Date'] = df['time'] + pd.DateOffset(days=3)but it cannot do an entire column in a DataFrame. What should I do?

1 Answers

You can use pd.to_timedelta:

df['time'] + pd.to_timedelta(df['daysToExp'], unit='D')

or equivalently:

df['time'] + pd.to_timedelta('1D') * df['daysToExp']
Related