Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum up Time difference
#8
Did you read the sum documentation? I provided a link.

Quote:sum(iterable, /, start=0)
Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.

I try a test to see if it works. I'm going to make a list of timedelta objects and sum them up.
from datetime import timedelta

deltas = [timedelta(hours=1, minutes=59, seconds=59) for _ in range(10)]

print(sum(deltas))
Error:
Traceback (most recent call last): File "...test.py", line 5, in <module> print(sum(deltas)) TypeError: unsupported operand type(s) for +: 'int' and 'datetime.timedelta'
Does this mean I cannot use sum() with timedelta objects? A closer look at the error message reveals the problem.
Quote:Unsupported types for +: int and timedelta.
I see the timedelta objects, but where is the int?

The int is here:
Quote:sum(iterable, /, start=0)
The result of sum is "start" and items from the iterable. I did not provide a "start" argument, so it uses the default value: 0. 0 is an int, and we cannot add an int and a timedelta object.

To correct we need to provide a "start" argument that is a timedelta object. We don't want the start to change the sum, so we want the timedelta equivalent of 0.
from datetime import timedelta

deltas = [timedelta(hours=1, minutes=35, seconds=42) for _ in range(10)]

print(sum(deltas, timedelta(seconds=0)))
Output:
19:59:50
If we are from different worlds, they are the world of "Reading the documentation" and "Not reading the documentation". You need to hop on a rocket and travel to the world of "Reading the documentation". It can be difficult at first, but I think it results in a deeper understanding than looking for matching examples only. You'll like it. It is very nice.
tester_V likes this post
Reply


Messages In This Thread
Sum up Time difference - by tester_V - Apr-05-2023, 05:15 AM
RE: Sum up Time difference - by Gribouillis - Apr-05-2023, 06:19 AM
RE: Sum up Time difference - by tester_V - Apr-05-2023, 06:04 PM
RE: Sum up Time difference - by deanhystad - Apr-05-2023, 07:31 PM
RE: Sum up Time difference - by deanhystad - Apr-05-2023, 06:34 PM
RE: Sum up Time difference - by tester_V - Apr-05-2023, 08:33 PM
RE: Sum up Time difference - by snippsat - Apr-05-2023, 06:55 PM
RE: Sum up Time difference - by deanhystad - Apr-05-2023, 08:52 PM
RE: Sum up Time difference - by tester_V - Apr-05-2023, 09:43 PM
RE: Sum up Time difference - by deanhystad - Apr-06-2023, 03:06 AM
RE: Sum up Time difference - by Gribouillis - Apr-06-2023, 06:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  time difference bettwenn logs enkliy 14 1,296 Nov-21-2023, 04:51 PM
Last Post: rob101
  Hard time trying to figure out the difference between two strings carecavoador 2 776 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  How to get indices of minimum time difference Mekala 1 2,241 Nov-10-2020, 11:09 PM
Last Post: deanhystad
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,690 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Correlation of Incidents using time difference Rajhesh 1 1,910 Jun-27-2019, 03:44 PM
Last Post: Larz60+
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 11,811 Feb-27-2018, 09:08 AM
Last Post: firesh
  Calculating time difference between times and splitting the HH and MM Bass 7 10,837 Jul-03-2017, 01:26 PM
Last Post: Bass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020