Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I subtract 3 hours of a datetime in python?

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?

like image 401
Mariana Avatar asked Oct 30 '18 13:10

Mariana


People also ask

How do you subtract hours from a date?

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.

How do you subtract datetime in Python?

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.

How do you subtract timestamp from hours?

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 = .

How do you subtract with time?

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).


2 Answers

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)
like image 139
Franco Piccolo Avatar answered Sep 21 '22 23:09

Franco Piccolo


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
like image 22
jezrael Avatar answered Sep 21 '22 23:09

jezrael