2

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
2018-09-03 20:30:00
2017-05-18 21:00:00
2014-11-26 21:00:00

I would expect a result of

20:45:00

I've tried simply taking the time component of the datatime objects and averaging them, a la

df['tm'].dt.time.mean()

But it gives me the following error:

TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'
Tim
  • 2,756
  • 1
  • 15
  • 31

1 Answers1

3

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'
jpp
  • 159,742
  • 34
  • 281
  • 339