Reputation: 2773
My dataset df
looks like this. It is a minute
based dataset.
time, Open, High
2017-01-01 00:00:00, 1.2432, 1.1234
2017-01-01 00:01:00, 1.2432, 1.1234
2017-01-01 00:02:00, 1.2332, 1.1234
2017-01-01 00:03:00, 1.2132, 1.1234
...., ...., ....
2017-12-31 23:59:00, 1.2132, 1.1234
I want to find the hourly rolling mean
for Open
column but should be flexible so that I can also find hourly rolling mean
for other columns.
What did I do?
I am able to find the daily rolling average
like given below, but how do I find for the hour basis so that I do not find mean
for the entire day
# Pandas code to find the rolling mean for a single day
df
.assign(1davg=df.rolling(window=1*24*60)['Open'].mean())
.groupby(df['time'].dt.date)
.last()
Please note that changing this line of code does not work because I already tried it: window=1*24*60
to window=60
Upvotes: 0
Views: 427
Reputation: 210912
IIUC:
mask = (df["time"].dt.hour >= 22) | (df["time"].dt.hour <= 2)
res = df.loc[mask].rolling("1H", on="time")["Open"].mean()
Upvotes: 1