Mean Of Time Component Only Of Pandas Datetime Column
I have a pandas dataframe, with a column of datetimes. I need to find the average time of this column, regardless of the date. For example, if I had dte ---- 2018-02-20 20:30:00
Solution 1:
One way is to deduct the normalized datetime
and then calculate mean of timedelta
series.
df = pd.DataFrame({'datetime': ['2018-02-20 20:30:00', '2018-09-03 20:30:00',
'2017-05-18 21:00:00', '2014-11-26 21:00:00']})
# convert to datetime
df['datetime'] = pd.to_datetime(df['datetime'])
# take difference to normalized datetime
df['time'] = df['datetime'] - df['datetime'].dt.normalize()
# calculate mean and format
res = str(df['time'].mean())[-8:]
print(res)
'20:45:00'
Post a Comment for "Mean Of Time Component Only Of Pandas Datetime Column"