Pandas: add timedelta column to datetime column (vectorized)

Full code solution:

import pandas as pd
from datetime import timedelta

df = pd.DataFrame([['2016-01-10',28],['2016-05-11',28],['2016-02-23',15],['2015-12-08',30]], 
                      columns = ['ship_string','days_supply'])

df['ship_date'] = pd.to_datetime(df['ship_string'])

df['time_added'] = pd.to_timedelta(df['days_supply'],'d')
df['supply_ended'] = df['ship_date'] + df['time_added']

print df

  ship_string  days_supply  ship_date  time_added supply_ended
0  2016-01-10           28 2016-01-10     28 days   2016-02-07
1  2016-05-11           28 2016-05-11     28 days   2016-06-08
2  2016-02-23           15 2016-02-23     15 days   2016-03-09
3  2015-12-08           30 2015-12-08     30 days   2016-01-07

Please let me know in the comments below if this isn’t a good vectorized solution and i’ll edit.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)