Reputation: 23
I have a SQLite database with the time for different runs. 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.
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. 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:
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. 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.
Upvotes: 1
Views: 183
Reputation: 93161
Use to_timedelta
. However, it expects the format to be HH:MM:SS.fff
:
df['time'] = pd.to_timedelta('00:' + df['time'])
avg_time = df['time'].mean().total_seconds()
Upvotes: 1