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?
To subtract hours from a date: Use the getHours() method to get the hours of the specific date. Use the setHours() method to set the hours for the date. The setHours method takes the hours as a parameter and sets the value for the date.
For adding or subtracting date, we use something called timedelta() function which can be found under datetime class. It is used to manipulate date, and we can perform an arithmetic operations on date like adding or subtract. timedelta is very easy and useful to implement.
If you wanted to subtract 5 hours and 15 minutes from the date-timestamp you would subtract 5.25 / 24 = . 2187. If you needed to subtract 4 hours and 8 minutes, you would subtract 8 / 60 = . 133 and therefore 4.133 / 24 = .
To subtract time, subtract the minutes then subtract the hours. Since we can't have negative minutes, add 60 to the minutes and subtract 1 from the hours (60 minutes = 1 hour).
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)
I believe you need convert columns to_datetime
and subtract 3
hours timedelta:
cols = ['startdate','enddate']
td = pd.Timedelta(3, unit='h')
df[cols] = df[cols].apply(lambda x: pd.to_datetime(x, format='%d/%m/%Y %H:%M:%S') - td
If want aplly solution for each column separately:
td = pd.Timedelta(3, unit='h')
df['startdate'] = pd.to_datetime(df['startdate'], format='%d/%m/%Y %H:%M:%S') - td
df['enddate'] = pd.to_datetime(df['enddate'], format='%d/%m/%Y %H:%M:%S') - td
print (df)
cpf day startdate enddate
0 1234 1 2018-01-08 09:50:00 2018-01-08 12:30:00
1 1234 1 2018-01-08 11:30:00 2018-01-08 12:40:00
2 1234 1 2018-01-08 11:50:00 2018-01-08 12:50:00
3 1234 2 2018-02-08 17:20:00 2018-03-07 21:50:00
4 1234 3 2018-03-07 22:00:00 2018-03-08 00:50:00
5 1235 1 2018-01-08 08:50:00 2018-01-08 12:20:00
6 5212 1 2018-01-08 11:50:00 2018-01-08 12:20:00
Last if need convert datetimes to custom format:
df['startdate'] = df['startdate'].dt.strftime('%d/%m/%Y %H:%M:%S')
df['enddate'] = df['enddate'].dt.strftime('%d/%m/%Y %H:%M:%S')
print (df)
cpf day startdate enddate
0 1234 1 08/01/2018 09:50:00 08/01/2018 12:30:00
1 1234 1 08/01/2018 11:30:00 08/01/2018 12:40:00
2 1234 1 08/01/2018 11:50:00 08/01/2018 12:50:00
3 1234 2 08/02/2018 17:20:00 07/03/2018 21:50:00
4 1234 3 07/03/2018 22:00:00 08/03/2018 00:50:00
5 1235 1 08/01/2018 08:50:00 08/01/2018 12:20:00
6 5212 1 08/01/2018 11:50:00 08/01/2018 12:20:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With