Search code examples
pythondatetimetimedelta

How to get "x" datatime.timedeltas from one array into one output?


I have a script getting input from 'x' zipfiles. From each zipfile I get a datatime.timedelta object that I convert into minutes and seconds in the format: average time spent on session is 24min 0s in 'x' pdf's.

Example: from one pdf I get 24min 0s and another 28min 0s. I want to create a new pdf with the output: average time spent on session 26min 0s (because that is the average time spend between 24min and 28min).

I have script creating an array with [datetime.timedelta(seconds=1462, microseconds=936503), datetime.timedelta(seconds=1706, microseconds=983165)]. I use .append to push 0:24:22.936503 and 0:28:26.983165 into my array.

How can I merge this two datatime.timedelta elements in my array so I can find the average time spent? I would like to end up with the format x:xx:xx.xxxxxx that I later use the code ({x:xx:xx.xxxxxx.total_seconds()/60:.0f} min {x:xx:xx.xxxxxx.total_seconds()%60:.0f} s) to get the output on the pdf that I want.


Solution

  • You can find the average of a list of timedelta objects basically the same way you would with a list of numbers - sum them together and divide by the number of elements.

    For example:

    deltas = [timedelta(minutes=30), timedelta(minutes=20)]
    
    avg = sum(deltas, start=timedelta()) / len(deltas)
    
    print(f"{avg.total_seconds()/60:.0f} min {avg.total_seconds()%60:.0f} s")
    

    this gives:

    25 min 0 s
    

    Note that we have to pass start=timedelta() to the sum function (see docs here)

    Presumably this is because sum behaves like a special case of reduce function - i.e. it needs a starting value and its default of 0 is not compatible with timedeltas, so we give it the timedelta equivalent of zero as the start value.