Issue
I have two columns with datetime in gmt and I need subtract three hours from this datetime. For example in line 4 I need subtract startdate in 3 hours, the result it was: 08/02/2018 17:20:0
. And in the same line 4 I need to subtract enddate in 3 hours, the result it was: 08/02/2018 21:50:0
.
Initial table:
cpf day startdate enddate
1234 1 08/01/2018 12:50:0 08/01/2018 15:30:0
1234 1 08/01/2018 14:30:0 08/01/2018 15:40:0
1234 1 08/01/2018 14:50:0 08/01/2018 15:50:0
1234 2 08/02/2018 20:20:0 08/03/2018 00:50:0
1234 3 08/03/2018 01:00:0 08/03/2018 03:50:0
1235 1 08/01/2018 11:50:0 08/01/2018 15:20:0
5212 1 08/01/2018 14:50:0 08/01/2018 15:20:0
Result table:
cpf day startdate enddate
1234 1 08/01/2018 09:50:0 08/01/2018 10:30:0
1234 1 08/01/2018 11:30:0 08/01/2018 10:40:0
1234 1 08/01/2018 11:50:0 08/01/2018 10:50:0
1234 2 08/02/2018 17:20:0 08/02/2018 21:50:0
1234 3 08/02/2018 22:00:0 08/03/2018 00:50:0
1235 1 08/01/2018 08:50:0 08/01/2018 10:20:0
5212 1 08/01/2018 11:50:0 08/01/2018 10:20:0
How can I do that in Python?
Solution
You can use timedelta
from datetime import timedelta
df['startdate'] = pd.to_datetime(df['startdate']) - timedelta(hours=3)
df['enddate'] = pd.to_datetime(df['enddate']) - timedelta(hours=3)
Answered By - Franco Piccolo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.