Skip to content Skip to sidebar Skip to footer

Pandas - Add Seconds From A Column To Datetime In Other Column

I have a dataFrame with two columns, ['StartDate' ,'duration'] the elements in the StartDate column are datetime type, and the duration are ints. Something like: StartDate Dura

Solution 1:

consider this df

    StartDate   duration
0   01/01/2017  135
1   01/02/2017  235

You can get the datetime column like this

df['EndDate'] = pd.to_datetime(df['StartDate']) + pd.to_timedelta(df['duration'], unit='s')
df.drop('StartDate,'duration', axis = 1, inplace = True)

You get

EndDate02017-01-01 00:02:1512017-01-02 00:03:55

EDIT: with the sample dataframe that you posted

df['EndDate'] = pd.to_timedelta(df['StartDate']) + pd.to_timedelta(df['Duration'], unit='s')

Solution 2:

df.StartDate = df.apply(lambda x: pd.to_datetime(x.StartDate)+pd.Timedelta(Second(df.duration)) ,axis = 1)

Post a Comment for "Pandas - Add Seconds From A Column To Datetime In Other Column"