Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

python - Average time for datetime list

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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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_timedelta part, coming very soon)

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).

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...