i have read 2 columns of data excel file, calculate difference between each pair , write answer in new column in excel. there no problem in reading , writing data not subtract 2 datetime.time objects each other.
my data in format "16:56:20.09" , when try find difference between 2 of them following error:
typeerror: unsupported operand type(s) -: 'datetime.time' , 'datetime.time' could please me solve problem?
you can't subtract time instances, because there's no definitive ordering them; what's difference between 12.00 , 13.00? hour? if tell mean 12.00 yesterday , 13.00 tomorrow? hence error:
>>> datetime import date, time, datetime >>> t1 = time(12, 00) >>> t2 = time(13, 00) >>> t1 - t2 traceback (most recent call last): file "<pyshell#3>", line 1, in <module> t1 - t2 typeerror: unsupported operand type(s) -: 'datetime.time' , 'datetime.time' you need know what day times occur on calculate difference. need convert them datetime objects combining them appropriate date:
>>> dt1 = datetime.combine(date(2015, 7, 14), t1) >>> dt2 = datetime.combine(date(2015, 7, 16), t2) now can subtract them:
>>> dt2 - dt1 datetime.timedelta(2, 3600) # 2 days, 3,600 seconds if they're on same day (or you're assuming are), generally* doesn't matter which day, can e.g.
>>> datetime.combine(date.today(), t2) - datetime.combine(date.today(), t1) datetime.timedelta(0, 3600) # 1 hour * (i.e. ignoring changing clocks, leap seconds, ...)
Comments
Post a Comment