may
may

Reputation: 1195

Grouping by datetime by hour in dataframe

I try to use this:

casa_consumo_h = casa_consumo['date'].groupby(lambda x: x.hour).mean()

I get the following error:

AttributeError: 'int' object has no attribute 'hour'

My date is of this type:

date             55535 non-null datetime64[ns]

How can I group by hour my dataframe?

date                         Ac      f      I      Pf     R       V
0   2017-08-01 00:00:00     37      59.97   1.49    0.4     10  22
1   2017-08-01 00:00:01     302     59.97   1.51    0.8     10  22
2   2017-08-01 00:00:03     3077    59.97   1.49    0.8     10  22
3   2017-08-01 00:00:05     300     59.97   1.50    0.8     15  22

Upvotes: 1

Views: 96

Answers (1)

Scott Boston
Scott Boston

Reputation: 153550

Use:

df.groupby(df['date'].dt.hour).mean()

Output:

          Ac      f       I   Pf      R     V
date                                         
0     301.78  59.97  1.4975  0.7  11.25  22.0

Upvotes: 2

Related Questions