I need to convert R to Python code and find the drive_time in hours
R code :
</>’ “drive_time” = difftime(TripSummaryDrivingDuration, “1899-12-31 00:00:00”, tz = ‘UTC”, units = </>“hours” ’
Input data
TripSummaryDrivingDuration
11:04:12.114000
20:17:29.337000
22:53:52.591000
1900-01-00 17:15:34
1900-01-00 07:50:37
1900-01-00 18:13:52
14:22:45.308000
09:12:56.611000
19:07:25.093000
05:03:29.737000
1900-01-00 05:43:43
16:41:18.672000
04:40:10.234000
1900-01-04 02:14:56
03:28:29.053000
09:15:48.271000
12:37:26.042000
08:55:49.740000
11:44:25.621000
18:19:13.531000
02:05:42.622000
05:23:35.622000
18:43:16.352000
Length: 6472
I used python code and found the output
‘’’
</>from datetime import datetime
</>import pandas as pd
</>trip_1[‘TripSummaryDrivingDuration’] = datetime.now( )
</>start_time = datetime(1899,12,31,0,0,0)
</>def calculate_drive_time(row):
</>return (row – start_time).total_seconds( )/3600
</>trip_1[“drive_time”] = trip_1[‘TripSummaryDrivingDuration’].apply(calculate_drive_time)
</>trip_1[“drive_time”] = pd.to_datetime(trip_1[“drive_time”])
</>trip_1[“drive_time”]
‘’’
output
0 1970-01-01 00:00:00.001079368
1 1970-01-01 00:00:00.001079368
2 1970-01-01 00:00:00.001079368
3 1970-01-01 00:00:00.001079368
4 1970-01-01 00:00:00.001079368
…………
6467 1970-01-01 00:00:00.001079368
6468 1970-01-01 00:00:00.001079368
6469 1970-01-01 00:00:00.001079368
6470 1970-01-01 00:00:00.001079368
6471 1970-01-01 00:00:00.001079368
Name: drive_time, length: 6472, dtype: datetime64[ns]
I need output in hrs. Any help and suggestion will be highly appreciated.
Thanks,
Bijay