1

Input file "clockTimes2020-40.xlsx" is CSV. Times are in columns. I want to calculate the time difference, "dur", as number of hours in float float, but I get the error message indicated.

import pandas
import datetime
print('pandas.__version__:',pandas.__version__)
df = pandas.read_excel('clockTimes2020-40.xlsx', sheet_name='clockTimes2020-40')
line0 = df.loc[0]
print('line0: ',line0)
print('line0 type is ',type(line0))
beg = line0[4]
end = line0[5]
print('beg = ',beg, 'type: ',type(beg))
print('end = ',end, 'type: ',type(end))
dur = end - beg
print('dur = ',dur,'type:',type(dur))

RESULTS runfile('C:/Users/Muir/ABCorp/untitled0.py', wdir='C:/Users/Muir/ABCorp') pandas.version: 1.1.3 line0: CID 1 Lname Adams Fname Francis J Mon 2020-09-28 00:00:00 Moin 08:00:10 Moout 17:16:24 Tue 2020-09-29 00:00:00 Tuin 07:58:41 Tuout 16:55:55 Wed 2020-09-30 00:00:00 Wein 07:53:00 Weout 17:45:45 Thu 2020-10-01 00:00:00 Thin 07:53:00 Thout 17:45:45 Fri 2020-10-02 00:00:00 Frin 07:53:00 Frout 16:59:45 Sat 2020-10-03 00:00:00 Sain 00:00:00 Saout 00:00:00 Sun 2020-10-04 00:00:00 Suin 00:00:00 Suout 00:00:00 Name: 0, dtype: object line0 type is <class 'pandas.core.series.Series'> beg = 08:00:10 type: <class 'datetime.time'> end = 17:16:24 type: <class 'datetime.time'> Traceback (most recent call last): File "C:\Users\Muir\ABCorp\untitled0.py", line 21, in dur = end - beg TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

user3338092
  • 45
  • 1
  • 5

2 Answers2

0

Python - Calculate the difference between two datetime.time objects

To calculate the difference, you have to convert the datetime.time object to a datetime.datetime object. Then when you subtract, you get a timedelta object. In order to find out how many hours the timedelta object is, you have to find the total seconds and divide it by 3600.

# Create datetime objects for each time (a and b)
dateTimeA = datetime.datetime.combine(datetime.date.today(), a)
dateTimeB = datetime.datetime.combine(datetime.date.today(), b)
# Get the difference between datetimes (as timedelta)
dateTimeDifference = dateTimeA - dateTimeB
# Divide difference in seconds by number of seconds in hour (3600)  
dateTimeDifferenceInHours = dateTimeDifference.total_seconds() / 3600
Paul Brennan
  • 2,638
  • 4
  • 19
  • 26
  • 1
    dur=((end.hour*3600 + end.minute *60 + end.second ) - ( beg.hour*3600 + beg.minute *60 + beg.second )) /3600. did the job. THANKS ALL – user3338092 Nov 24 '20 at 00:07
0

#EDIT [Solved]:

dur=((end.hour*3600  + end.minute *60 + end.second ) -    ( beg.hour*3600 + beg.minute *60 + beg.second )) /3600.

Else, you can Convert "TIME" as string to datetime type.

Then you can get "time" in float hours by using total_seconds()/3600.

Look at this Example:

from datetime import datetime
beg = '08:00:10'
end = '17:16:24' 

dur = datetime.strptime(end, '%H:%M:%S') - datetime.strptime(beg, '%H:%M:%S')

result = dur.total_seconds()/3600

print(result)
AziMez
  • 2,014
  • 1
  • 6
  • 16