Average time for a date and time list

Looking for a quick solution to the problem of averaging over time.

I have a list of datetime objects. It is necessary to find the 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") 
+8
source share
4 answers

Here is the best way to approach this problem.

Generate datetimes fetch

 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 20 m 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 is like timedelta (note that this requires numpy 1.7 and pandas 0.13 for the to_timedelta part, which will be 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 
+5
source

I searched the same, but then I discovered it. A very simple way to get an average list of datetime objects.

  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 it all in one simple line:

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

Greetings

+1
source

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

 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) 
0
source

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

 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) 
0
source

Source: https://habr.com/ru/post/957031/


All Articles