Python中日期之间的平均差异

时间:2022-06-07 01:26:24

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.

例如,如果输入是(2008-10-01 12:15:00,2008-10-01 12:25:00,2008-10-01 12:35:00),那么平均增量将是00 :10:00或10分钟。

Any suggestions on how to calculate this using Python?

关于如何使用Python计算这个的任何建议?

5 个解决方案

#1


1  

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.

您可以从前一个中减去每个连续日期(得到一个timedelta对象,表示天,秒的差异)。然后,您可以对timedelta对象进行平均以找到答案。

#2


11  

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

编辑:修复了一个一个错误

#3


3  

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.

这确实会使你的增量平均在一起。

#4


2  

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.

由于你似乎在你的例子中抛出了时间1和3之间的20分钟增量,我会说你应该只对日期时间列表进行排序,将相邻时间之间的增量相加,然后除以n-1。

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

您有可以与我们分享的任何代码,我们可以帮您调试吗?

#5


0  

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])
        try:
            sumdeltas += delta
        except:
            raise
        i += 1
    avg = sumdeltas / numdeltas
    return avg

#1


1  

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.

您可以从前一个中减去每个连续日期(得到一个timedelta对象,表示天,秒的差异)。然后,您可以对timedelta对象进行平均以找到答案。

#2


11  

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

编辑:修复了一个一个错误

#3


3  

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.

这确实会使你的增量平均在一起。

#4


2  

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.

由于你似乎在你的例子中抛出了时间1和3之间的20分钟增量,我会说你应该只对日期时间列表进行排序,将相邻时间之间的增量相加,然后除以n-1。

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

您有可以与我们分享的任何代码,我们可以帮您调试吗?

#5


0  

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])
        try:
            sumdeltas += delta
        except:
            raise
        i += 1
    avg = sumdeltas / numdeltas
    return avg