Average difference between dates in Python

I have a series of datetime objects and would like to calculate the average delta between them.

For example, if the input was (2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00), then the average delta would be exactly 00:10:00, or 10 minutes.

Any suggestions on how to calculate this using Python?

Asked by: Oliver314 | Posted: 24-09-2021

Answer 1

As far as algorithms go, that's an easy one. Just find the max and min datetimes, take the difference, and divide by the number of datetimes you looked at.

If you have an array a of datetimes, you can do:

mx = max(a)
mn = min(a)
avg = (mx-mn)/(len(a)-1)

to get back the average difference.

EDIT: fixed the off-by-one error

Answered by: Andrew939 | Posted: 25-10-2021

Answer 2

Say a is your list

sumdeltas = timedelta(seconds=0)
i = 1
while i < len(a):
    sumdeltas += a[i-1] - a[i]
    i = i + 1

avg_delta = sumdeltas / (len(a) - 1)

This will indeed average your deltas together.

Answered by: Max349 | Posted: 25-10-2021

Answer 3

Since you seem to be throwing out the 20 minute delta between times 1 and 3 in your example, I'd say you should just sort the list of datetimes, add up the deltas between adjacent times, then divide by n-1.

Do you have any code you can share with us, so we can help you debug it?

Answered by: Brooke929 | Posted: 25-10-2021

Answer 4

You can subtract each successive date from the one prior (resulting in a timedelta object which represents the difference in days, seconds). You can then average the timedelta objects to find your answer.

Answered by: Roland159 | Posted: 25-10-2021

Answer 5

small clarification

from datetime import timedelta

def avg(a):
    numdeltas = len(a) - 1
    sumdeltas = timedelta(seconds=0)

    i = 1
    while i < len(a):
        delta = abs(a[i] - a[i-1])
            sumdeltas += delta
        i += 1
    avg = sumdeltas / numdeltas
    return avg

Answered by: Ted335 | Posted: 25-10-2021

Similar questions

c# - Text difference algorithm

I need an algorithm that can compare two text files and highlight their difference and ( even better!) can compute their difference in a meaningful way (like two similar files should have a similarity score higher than two dissimilar files, with the word "similar" defined in the normal terms). It sounds easy to implement, but it's not. The implementation can be in c# or python. Thanks.

python - Algorithm to calculate percent difference between two blobs of text

Closed. This question does not meet Stack Overflow guid...

Algorithm for getting time difference in Python

I want to ask you about getting the time difference in Python. Suppose my starting time is 11:30am and ending at 1:00pm. The difference would be 1:00pm-11:30am = 90 minutes. I am new to python. Are there existing algorithms to solve these kinds of problems? I need then for my scheduling algorithm problem. Your response would be greatly appreciated. :)

python - Algorithm to find the least difference between lists

I have been trying to understand the algorithm used here to compare two lists, implemented in this commit. The intention, as I understood, is to find the least amount of changes to create

algorithm - Minimum subarray difference in python

Consider I have a non-empty array of integers: A0..An. And consider a parameter P where 0 &lt; P &lt;=n. I need to find a minimum absolute difference between left and right subarray splited by P. For example: A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3 P = 1, difference = |3 − 10| = 7 P = 2, difference = |4 − 9| = 5 P = 3, difference = |6 − 7| = 1 P = 4, difference =...

python - Algorithm to find indices of two numbers in Series with difference within a specified range

I have a list of numbers (sorted in increasing order, no repeats) and I'd like to write an algorithm that will quickly return the indices of two elements in the list whose difference is within a specified range. This could be multiple pairs of indices, but my intention is to adjust make the range small enough to capture only one or two, then do some decisioning based off of the indices. I'm currently usin...

algorithm - python Code running time difference

I was practicing on Leetcode, I want to ask 3 questions about the codes running time. I noticed that on Leetcode, even the same code will have quite different running time if submitted multiple times. And the difference is huge, is that normal? I have seen the difference as first times beats 26%, but second times beats 51%. That really is confusing me, while I am trying to figure out where I am and how goo...

python - Create an algorithm to collect points who difference is less than a value with a given condition

Hi I have list of tuples as given below: [(2031, 0.11078125), (2032, 0.11131274999999999), (1298, 0.11819950000000001), (2033, 0.12396399999999999), (2030, 0.13113425), (1238, 0.13305375), (2886, 0.1332045), (2704, 0.13512125000000003), (1299, 0.13639975), (1308, 0.136436), (957, 0.13736575), (2698, 0.1374555), (2768, 0.137864), (1297, 0.13829075000000002), (1959, 0.13873375), (1958, 0.138735), (2767, 0.138...

python - What is the algorithm for two intervals difference?

I'm making a program in Python where I have 2 intervals as an input and then it has to return 3 operations: - union (A | B) - intersection (A &amp; B) - difference I've tried making sets for intervals A and B and then subtract them with set method (A-B) but it has no sense as it has to return an interval, not a set and I distinguish between open / closed / open_closed etc intervals. input: [3, 10) (5, 16]...

Powerset algorithm in Python: difference between + and append on lists

I’m working through the powerset problem in Python. The powerset P(S) of a set S is the set of all subsets of S. For example, if S = {a, b, c} then P(s) = {{}, {a}, {b}, {c}, {a,b}, {a, c}, {b, c}, {a, b, c}}. This solution works just fine: def powerset(array): powerset = [[]] for num in array: for i in range(len(...

datetime - What is the simplest way to find the difference between 2 times in python?

I have 2 time values which have the type datetime.time. I want to find their difference. The obvious thing to do is t1 - t2, but this doesn't work. It works for objects of type datetime.datetime but not for datetime.time. So what is the best way to do this?

c# - Text difference algorithm

I need an algorithm that can compare two text files and highlight their difference and ( even better!) can compute their difference in a meaningful way (like two similar files should have a similarity score higher than two dissimilar files, with the word "similar" defined in the normal terms). It sounds easy to implement, but it's not. The implementation can be in c# or python. Thanks.

Is there any difference between "string" and 'string' in Python?

This question already has answers here:

What is the difference between Ruby and Python versions of"self"?

I've done some Python but have just now starting to use Ruby I could use a good explanation of the difference between "self" in these two languages. Obvious on first glance: Self is not a keyword in Python, but there is a "self-like" value no matter what you call it. Python methods receive self as an explicit argument, whereas Ruby does not. Ruby sometimes has methods explicitly d...

python - What's the difference between all of the os.popen() methods?

I was looking at the Python documentation and saw that there are 4-5 different versions of popen(), e.g. os.popen(), os.popen2(), etc. Apart from the fact that some include stderr while others don't, what are the differences between them and when would you use each one? The documentation didn't really explain it very wel...

datetime - Python's timedelta: can't I just get in whatever time unit I want the value of the entire difference?

I am trying to have some clever dates since a post has been made on my site ("seconds since, hours since, weeks since, etc..") and I'm using datetime.timedelta difference between utcnow and utc dated stored in the database for a post. Looks like, according to the docs, I have to use the days attribute AND the seconds attribute, to get the fancy date strings I want. Can't I just get in whatever time unit I ...

Difference between the use of double quote and quotes in python

This question already has answers here:

python - How to tell the difference between an iterator and an iterable?

In Python the interface of an iterable is a subset of the iterator interface. This has the advantage that in many cases they can be treated in the same way. However, there is an important semantic difference between the two, since for an iterable __iter__ returns a new iterator object and not just self. How ...

Difference between class (Python) and struct (C)

I'm new to python. I've studied C and I noticed that that the C structure (struct) seemed to have the same task as "class" in python. So what is, conceptually, the difference?

syntax - What's the difference between "2*2" and "2**2" in Python?

What is the difference between the following statements? Statement 1: var=2**2*3 Statement 2: var2=2*2*3 I see no difference. This raises the following question. Why is Statement 1 used if we can use Statement 2?

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python