Python 日期时间列表的平均时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19681703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:21:34  来源:igfitidea点击:

Average time for datetime list

pythondatetimepandasaverage

提问by user2915556

Looking for fastest solution of time averaging problem.

寻找时间平均问题的最快解决方案。

I've got a list of datetime objects. Need to find average value of time (excluding year, month, day). Here is what I got so far:

我有一个日期时间对象列表。需要找到时间的平均值(不包括年、月、日)。这是我到目前为止所得到的:

import datetime as dtm
def avg_time(times):
    avg = 0
    for elem in times:
        avg += elem.second + 60*elem.minute + 3600*elem.hour
    avg /= len(times)
    rez = str(avg/3600) + ' ' + str((avg%3600)/60) + ' ' + str(avg%60)
    return dtm.datetime.strptime(rez, "%H %M %S")

采纳答案by Jeff

Here's a better way to approach this problem

这是解决此问题的更好方法

Generate a sample of datetimes

生成日期时间样本

In [28]: i = date_range('20130101',periods=20000000,freq='s')

In [29]: i
Out[29]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-01 00:00:00, ..., 2013-08-20 11:33:19]
Length: 20000000, Freq: S, Timezone: None

avg 20m times

平均 2000 万次

In [30]: %timeit pd.to_timedelta(int((i.hour*3600+i.minute*60+i.second).mean()),unit='s')
1 loops, best of 3: 2.87 s per loop

The result as a timedelta (note that this requires numpy 1.7 and pandas 0.13 for the to_timedeltapart, coming very soon)

结果作为时间增量(请注意,这需要 numpy 1.7 和 Pandas 0.13to_timedelta部分,很快就会推出)

In [31]: pd.to_timedelta(int((i.hour*3600+i.minute*60+i.second).mean()),unit='s')
Out[31]: 
0   11:59:12
dtype: timedelta64[ns]

In seconds (this will work for pandas 0.12, numpy >= 1.6).

在几秒钟内(这将适用于 pandas 0.12,numpy >= 1.6)。

In [32]: int((i.hour*3600+i.minute*60+i.second).mean())
Out[32]: 43152

回答by Martijn Pieters

You would at least use sum()with a generator expression to create the total number of seconds:

您至少会使用sum()生成器表达式来创建总秒数:

from datetime import datetime, date, time

def avg_time(datetimes):
    total = sum(dt.hour * 3600 + dt.minute * 60 + dt.second for dt in datetimes)
    avg = total / len(datetimes)
    minutes, seconds = divmod(int(avg), 60)
    hours, minutes = divmod(minutes, 60)
    return datetime.combine(date(1900, 1, 1), time(hours, minutes, seconds))

Demo:

演示:

>>> from datetime import datetime, date, time, timedelta
>>> def avg_time(datetimes):
...     total = sum(dt.hour * 3600 + dt.minute * 60 + dt.second for dt in datetimes)
...     avg = total / len(datetimes)
...     minutes, seconds = divmod(int(avg), 60)
...     hours, minutes = divmod(minutes, 60)
...     return datetime.combine(date(1900, 1, 1), time(hours, minutes, seconds))
... 
>>> avg_time([datetime.now(), datetime.now() - timedelta(hours=12)])
datetime.datetime(1900, 1, 1, 7, 13)

回答by Rishikesh Jha

I was looking for the same, but then i discovered this. A very simple way to get average of datetime object's list.

我一直在寻找相同的,但后来我发现了这一点。获取日期时间对象列表平均值的一种非常简单的方法。

    import datetime
    #from datetime.datetime import timestamp,fromtimestamp,strftime ----> You can use this as well to remove unnecessary datetime.datetime prefix :)  
    def easyAverage(datetimeList): ----> Func Declaration
        sumOfTime=sum(map(datetime.datetime.timestamp,datetimeList))
        '''
         timestamp function changes the datetime object to a unix timestamp sort of a format.
         So I have used here a map to just change all the datetime object into a unix time stamp form , added them using sum and store them into sum variable.
        '''
        length=len(datetimeList) #----> Self Explanatory

        averageTimeInTimeStampFormat=datetime.datetime.fromtimestamp(sumOfTime/length)
        '''
        fromtimestamp function returns a datetime object from a unix timestamp.
        '''

        timeInHumanReadableForm=datetime.datetime.strftime(averageTimeInTimeStampFormat,"%H:%M:%S") #----> strftime to change the datetime object to string.
        return timeInHumanReadableForm

Or you can do all this in one simple line:

或者您可以在一条简单的行中完成所有这些:

    avgTime=datetime.datetime.strftime(datetime.datetime.fromtimestamp(sum(map(datetime.datetime.timestamp,datetimeList))/len(datetimeList)),"%H:%M:%S")

Cheers,

干杯,

回答by wiesiu_p

Here's a short and sweet solution (perhaps not the fastest though). It takes the difference between each date in the date list and some arbitrary reference date (returning a datetime.timedelta), and then sums these differences and averages them. Then it adds back in the original reference date.

这是一个简短而甜蜜的解决方案(虽然可能不是最快的)。它获取日期列表中的每个日期与某个任意参考日期之间的差异(返回 datetime.timedelta),然后对这些差异求和并求平均值。然后它添加回原始参考日期。

import datetime
def avg(dates):
  any_reference_date = datetime.datetime(1900, 1, 1)
  return any_reference_date + sum([date - any_reference_date for date in dates], datetime.timedelta()) / len(dates)