Abi
Abi

Reputation: 337

Pandas amend date by column value

I have a column of dates, and a column of how many hours that time is off by.

index   | Datetime            | Hour change
1         31-02-2016 10:55:36   1
2         17-03-2016 14:23:04   -3

How can I change my datetime by the number of hours in the Hour Change column? The result to the above should be:

31-02-2016 11:55:36
17-03-2016 11:23:04

Upvotes: 1

Views: 28

Answers (1)

jezrael
jezrael

Reputation: 862641

I think you can add to column Datetime column Hour change converted to numpy array by values and hour's timedelta:

print df
                 Datetime  Hour change
index                                 
1     2016-02-28 10:55:36            1
2     2016-03-17 14:23:04           -3

print df['Datetime'] + df['Hour change'].values.astype("timedelta64[h]")
index
1   2016-02-28 11:55:36
2   2016-03-17 11:23:04
Name: Datetime, dtype: datetime64[ns]

df['Datetime'] = df['Datetime'] + df['Hour change'].values.astype("timedelta64[h]")
print df
                 Datetime  Hour change
index                                 
1     2016-02-28 11:55:36            1
2     2016-03-17 11:23:04           -3

Upvotes: 1

Related Questions