2

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?

Awitz
  • 296
  • 1
  • 11
  • 1
    Does this answer help you? https://stackoverflow.com/a/39962299/10741023 – dylanvanw Aug 11 '20 at 18:34
  • Does this answer your question? [Pandas: add timedelta column to datetime column (vectorized)](https://stackoverflow.com/questions/38355816/pandas-add-timedelta-column-to-datetime-column-vectorized) – Trenton McKinney Aug 11 '20 at 18:35

1 Answers1

2

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']
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74