Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregating time to a defined slot

Tags:

python

pandas

I have a dataframe df which has a few weeks of 2minute resolution data:

df.dtypes

time_stamp      datetime64[ns]
Day_name                object 
x                     int64
y                   int64

df.head

time_stamp             Day_name   x   y  
0 2017-05-17 14:28:35  Wednesday  100 200   
1 2017-05-17 14:30:32  Wednesday  300 400

I want to aggregate the metrics x and y and find their average for the average '15' minute period. I originally had an epoch metric, but I've converted this to the datetime shown above.

time_stamp             Day_name   x   y        15_min_slot
    0 2017-05-17 14:28:35  Wednesday  100 200  14:15
    1 2017-05-17 14:30:32  Wednesday  300 400  14:30

How do i do this?

I can find the hour via:

df['hour'] = df['time_stamp'].dt.hour

df['minute'] = df['time_stamp'].dt.minute

What I'll eventually do is then:

output = df.groupby(['15_min_slot'],as_index=False)['x'].mean()

like image 632
LearningSlowly Avatar asked Dec 06 '25 10:12

LearningSlowly


1 Answers

You can use Grouper, in combination with the freq argument, i.e:

df.groupby(pd.Grouper(key=df['time_stamp'], freq='15T')).mean()
like image 133
Mathias711 Avatar answered Dec 08 '25 01:12

Mathias711