Reputation: 5914
I am trying to figure out the best way to average a series of datetime.time
values with about 40 records in the format of 23:19:30
or HH:MM:SS
, however, when I attempt to use the to_timedelta
method and apply .mean()
I run into an error:
ValueError: Invalid type for timedelta scalar: <class 'datetime.time'>
What would be the proper format for the time to be in to use to_timedelta().mean()
or is there a better approach?
Code:
bed_time_mean = pd.to_timedelta(bed_rise_df['start_time']).mean()
Upvotes: 0
Views: 163
Reputation: 25544
pd.to_timedelta
accepts strings in H:M:S format as input, so you could convert your column with datetime.time objects to string first. Ex:
from datetime import time
import pandas as pd
df = pd.DataFrame({'t':[time(1,2,3), time(2,3,4), time(3,4,5)]})
pd.to_timedelta(df['t'].astype(str)).mean()
# Timedelta('0 days 02:03:04')
Upvotes: 1