3

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?

2

1 Answer 1

3

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.