I want to fill NaN using a mean of values from the same hour-minutes of the previous days. To simplify, this is an example of my df.
timstamp data
22/04/2016 09:00 1
22/04/2016 09:05 2
...
23/04/2016 09:00 3
23/04/2016 09:05 4
...
24/04/2016 09:00 5
24/04/2016 09:05 6
...
25/04/2016 09:00 7
25/04/2016 09:05 8
...
25/04/2016 10:00 NaN
25/04/2016 10:05 NaN
The real data contains many days with continuous 5-minute intervals.
df = df.groupby(df.index.minute).fillna(df.data.rolling(3).mean())
attempted to do rolling mean from the previous hour-minute on the past days but it did not work.
An alternative method of df = df.groupby(df.index.minute).ffill()
take values from the previous two lines (i.e. 7 and 8) which comes from the same minutes of the previous hour on the same day.
However, I want the following result:
timstamp data
22/04/2016 09:00 1
22/04/2016 09:05 2
...
23/04/2016 09:00 3
23/04/2016 09:05 4
...
24/04/2016 09:00 5
24/04/2016 09:05 6
...
25/04/2016 09:00 7
25/04/2016 09:05 8
25/04/2016 10:00 3
25/04/2016 10:05 4
where the value 3 (second last line) is the mean of the values from the same hour-minute of the previous days (mean of 1, 3, and 5), and 4 (last line) is the mean of 2, 4, and 6. Given the size of my df, I want to take a mean from tens of previous days.
Edit
I am getting closer. Using the following code, the mean of the data is calculated by similar hours and minutes as I wanted:
df.set_index('timstamp', inplace=True)
df=df.groupby([df.index.hour, df.index.minute]).mean()
df.index.names = ["hour", "minute"]
But, it uses the entire data to get the hour-minute mean. What I wanted is to use the same hour-minutes from the previous days only, where I can set the number of past days in the calculation. Then, the resulted mean value shall be used to fill the NaN.