We have a time series with a values column apart from the date column and for every day we have 24 rows (hours of the day). The goal is to create an additional column which contains the mean of the values for a specific hour of the day on a rolling window. For example for a rolling window of 365*24 the new column would convey the information: "The values column at this hour of the day for the last year had a mean value of X".
The input and output should look something like this for a rolling window of 24:
index date(as datetime64) values new_column reasoning
0 2021-02-21 00:00:00+00:00 100 -
1 2021-02-21 01:00:00+00:00 200 -
2 2021-02-21 02:00:00+00:00 300 -
3 2021-02-21 03:00:00+00:00 400 -
4 2021-02-21 04:00:00+00:00 500 -
5 2021-02-21 05:00:00+00:00 600 -
6 2021-02-21 06:00:00+00:00 700 -
7 2021-02-21 07:00:00+00:00 800 -
8 2021-02-21 08:00:00+00:00 900 -
9 2021-02-21 09:00:00+00:00 1000 -
10 2021-02-21 10:00:00+00:00 1100 -
11 2021-02-21 11:00:00+00:00 1200 -
12 2021-02-21 12:00:00+00:00 1300 -
13 2021-02-21 13:00:00+00:00 1400 -
14 2021-02-21 14:00:00+00:00 1500 -
15 2021-02-21 15:00:00+00:00 1600 -
16 2021-02-21 16:00:00+00:00 1700 -
17 2021-02-21 17:00:00+00:00 1800 -
18 2021-02-21 18:00:00+00:00 1900 -
19 2021-02-21 19:00:00+00:00 2000 -
20 2021-02-21 20:00:00+00:00 2100 -
21 2021-02-21 21:00:00+00:00 2200 -
22 2021-02-21 22:00:00+00:00 2300 -
23 2021-02-21 23:00:00+00:00 2400 -
24 2021-02-22 00:00:00+00:00 200 150 (i.e the mean of 100 and 200, the values at 00:00 for the 2 days)
25 2021-02-22 01:00:00+00:00 400 300 (i.e. mean of 200 and 400)
26 2021-02-22 02:00:00+00:00 600 450 (i.e. mean of 300 and 600)
27 2021-02-22 03:00:00+00:00 800 etc. etc.
28 2021-02-22 04:00:00+00:00 1000
29 2021-02-22 05:00:00+00:00 1200
30 2021-02-22 06:00:00+00:00 1400
31 2021-02-22 07:00:00+00:00 1600
32 2021-02-22 08:00:00+00:00 1800
33 2021-02-22 09:00:00+00:00 2000
34 2021-02-22 10:00:00+00:00 2200
35 2021-02-22 11:00:00+00:00 2400
36 2021-02-22 12:00:00+00:00 2600
37 2021-02-22 13:00:00+00:00 2800
38 2021-02-22 14:00:00+00:00 3000
39 2021-02-22 15:00:00+00:00 3200
40 2021-02-22 16:00:00+00:00 3400
41 2021-02-22 17:00:00+00:00 3600
42 2021-02-22 18:00:00+00:00 3800
43 2021-02-22 19:00:00+00:00 4000
44 2021-02-22 20:00:00+00:00 4200
45 2021-02-22 21:00:00+00:00 4400
46 2021-02-22 22:00:00+00:00 4600
47 2021-02-22 23:00:00+00:00 4800
Before even going to making a new column, I thought of trying a groupby:
means = df.groupby(df.date.dt.hour).values.mean()
However, that has the issue of returning the mean-per-hour for the entire dataframe and not a rolling window of it while a groupby(df.date.dt.hour).values.rolling(X).mean() only considers a window of the final grouped results and not a window of the initial data to be considered before grouping, and I don't know how to assign the values to a new column to the initial dataframe since the groupby() results do not have the same index.