简体   繁体   English

如何计算 Python 中多次运行的平均时间

[英]How to calculate average time for several runs in Python

I have a SQLite database with the time for different runs.我有一个 SQLite 数据库,其中包含不同运行的时间。 The time is stored as text and has the following format: MM:SS.fff for example 04:20.458 for 4 minutes, 20 seconds and 458 milliseconds.时间以文本形式存储,格式如下: MM:SS.fff 例如 04:20.458 表示 4 分钟、20 秒和 458 毫秒。

What I would like to achieve is to get the average time of all runs.我想要实现的是获得所有运行的平均时间。 What I have done so far is to run a SQL query and write the result into a pandas dataframe.到目前为止,我所做的是运行 SQL 查询并将结果写入 pandas dataframe。 Data looks like this:数据如下所示:

          time
0    04:58.244
1    05:01.509
2    04:56.664
3    04:48.271
4    04:48.853
..         ...

As a next step I have converted the text values into a datetime format like this:作为下一步,我将文本值转换为日期时间格式,如下所示:

df['time'] = pd.to_datetime(df['time'], format='%M:%S.%f')

Output is now as following: Output 现在如下:

                       time
0   1900-01-01 00:04:58.244
1   1900-01-01 00:05:01.509
2   1900-01-01 00:04:56.664
3   1900-01-01 00:04:48.271
4   1900-01-01 00:04:48.853
..                      ...

And here I am stuck.在这里我被困住了。 Using mean() returns nothing.使用 mean() 什么也不返回。 All I found are instructions on how to calculate the difference between times, but as written before, I am interested in an overall average of the times.我发现的只是关于如何计算时间差的说明,但如前所述,我对时间的总体平均值感兴趣。 Unfortunately, I found nothing that helped me to make it work.不幸的是,我发现没有任何东西可以帮助我让它工作。

Some tips or help would be very appreciated.一些提示或帮助将不胜感激。

Use to_timedelta .使用to_timedelta However, it expects the format to be HH:MM:SS.fff :但是,它希望格式为HH:MM:SS.fff

df['time'] = pd.to_timedelta('00:' + df['time'])
avg_time = df['time'].mean().total_seconds()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM