0
from datetime import datetime, timedelta

frames_load = 0
times_start_dict = [{'timestamp': datetime(2013, 12, 21, 4, 36, 23)}, {'timestamp': datetime(2013, 12, 21, 4, 36, 23)}]
times_end_dict = [{'timestamp': datetime(2013, 12, 21, 4, 36, 53)}, {'timestamp': datetime(2013, 12, 21, 4, 36, 53)}]

for start in times_start_dict:
    for end in times_end_dict:         
        if check_startend(): #dummy function to check other value in dict to see if start/end times match; please comment this out and indent code block to reproduce error
            frames_load += end['timestamp'] - start['timestamp']
            print frames_load
            continue

Output:

Traceback (line 11): "frames_load += end['timestamp'] - start['timestamp']"

TypeError: unsupported operand type(s) for +=: 'int' and 'datetime.timedelta'

Some context:

I'm iterating over two lists consisting of dicts which contain datetime objects for start and end times, respectively. Once a start and end timedelta have been correctly matched (not shown), I find the time elapsed which becomes a timedelta object. I'm assigning it to frames_load so that in the next iteration, I can add the time elapsed (which should always be positive), figuring it's just like incrementing x += 1. However, I'm getting a TypeError which says to me that after the first iteration/calculation, frames_load is now an int and no longer a timedelta object.

How can I add timedeltas to frames_load so that it can display the aggregate/sum timedelta?

5
  • 2
    Your error message does not match your code block, see the int(...). Commented Dec 30, 2013 at 2:44
  • @Nabla Thanks for the edit, I'm new to posting/commenting on stack. What do you mean my error does not match the code block? I wrote this to represent my code and error, so perhaps you're pointing to a discrepancy I don't see. Can you elaborate? Commented Dec 30, 2013 at 5:36
  • @nabla do you mean that the error int cannot be added to a timedelta object does not match with line 4? Commented Dec 30, 2013 at 5:37
  • 1
    The line you posted in the code is frames_load += end['timestamp'] - start['timestamp']. The same line in the error traceback reads frames_load += int(end['timestamp'] - start['timestamp']). So the code is not the same code that produced the error. This makes it hard to give a precise answer. Also you should show minimal code initializing frames_load and best times_start_dict and times_end_dict as well, so everyone can reproduce the error. Commented Dec 30, 2013 at 6:22
  • You already have the answer from @alko: instead of frames_load = 0 use frames_load = timedelta(). Commented Dec 30, 2013 at 18:55

1 Answer 1

2

First, this code could not output 00:00:35, as frames_load=0 initially.

Second, error says it all, you can't add timedelta, i.e. end['timestamp'] - start['timestamp'], to integer, that is to 0.

If you want sum up cumulative timedelta, you should init frames_load=timedelta(). Or following code will do the trick:

times_start_dict = [{'timestamp': datetime(2013, 12, 21, 4, 36, 23)}, 
                    {'timestamp': datetime(2013, 12, 21, 4, 36, 23)}]
times_end_dict = [{'timestamp': datetime(2013, 12, 21, 4, 36, 25)}, 
                  {'timestamp': datetime(2013, 12, 21, 4, 36, 26)}]
print sum((end['timestamp'] - start['timestamp'] 
             for start, end in zip(times_start_dict, times_end_dict)),
          timedelta())

# 0:00:05
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome thank you @alko, nabla, and others who contributed. I had tried a similar approach to initialize frames_load to datetime.timedelta() but that errored. I guess it's the datetime module is implicitly invoked here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.