TypeError: can't compare datetime.timedelta to float

安稳与你 提交于 2019-12-10 17:53:13

问题


I'm working on my python script to work out the duration times between start date and end date format like 20140520160000 and 20140520170000 so I can get the hour.

I'm having a trouble with this code:

if epgDuration >= 0.10 and epgDuration <= 0.30:
   epgwidth = "250"

I get an error when I'm trying to compare the range of the times between 0.10 mins and 0.30 mins.

The error I get is: TypeError: can't compare datetime.timedelta to float.

The error are jumping on this line:

if epgDuration >= 0.10 and epgDuration <= 0.30:

Here is the results:

14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 2:30:00
14:44:55 T:1580  NOTICE: 3:00:00
14:44:55 T:1580  NOTICE: 1:00:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 1:00:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00
14:44:55 T:1580  NOTICE: 0:30:00

Here is the code when I use to duration the times:

for row in programs:
    program_startdate = str(row[2])
    program_endDate = str(row[3])

    try:
       start_date = datetime.datetime.strptime(program_startdate, "%Y%m%d%H%M%S")
       end_date = datetime.datetime.strptime(program_endDate, "%Y%m%d%H%M%S")
    except TypeError:
       start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_startdate, "%Y%m%d%H%M%S")))
       end_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(program_endDate, "%Y%m%d%H%M%S")))

    #workout the duration times to get the program time
    epgDuration = end_date - start_date

    if epgDuration >= 0.10 and epgDuration <= 0.30:
       epgwidth = "250"

    elif epgDuration >= 1.00 and epgDuration <= 1.29:
         epgwidth = "500"
    print epgwidth

回答1:


Indeed, you cannot compare a timedelta to a float value.

You can convert the object to seconds:

if 600 <= epgDuration.total_seconds() <= 1800:

where 10 minutes is 600 seconds, and 30 minutes is 1800.

Or create new timedelta() objects to compare against:

epgwidth = "0"

if timedelta(minutes=10) <= epgDuration <= timedelta(minutes=30):
    epgwidth = "250"

elif timedelta(hours=1) <= epgDuration <= timedelta(hours=1.5):
    epgwidth = "500"

I've given epgwidth a default value before the if statements for the case where the time difference is not falling in the 10-30 minutes or 1-1.5 hour ranges.




回答2:


To get number-of-minutes from a timedetla object, you can use total_seconds() and divide by 60:

epgDurationMin = epgDuration.total_seconds()/60.
if 0.10 <= epgDurationMin <= 0.30:
   ...

Also note you can use python's cool comparison-chaining (e.g. a <= b <= c)



来源:https://stackoverflow.com/questions/25714670/typeerror-cant-compare-datetime-timedelta-to-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!