'How to calculate mean value at specific date and hour using DatetimeIndex

I have some missing data which has as index DatetimeIndex over some years. I would now like to replace this missing value with the mean value of all datapoints which have the same day, month and hour. If no data is available for this hour, I would like to calculate the the mean of the nearest interpolated hour. How can I achieve this?

My data is for example

                     Generation
2017-03-01 01:00:00         0.0
2018-03-01 01:00:00         987
2018-03-01 03:00:00         500

And my missing data:

                     Generation
2019-03-01 01:00:00         Na
2019-03-01 02:00:00         Na

And I would like to something like this

                     Generation
2019-03-01 01:00:00      493.5
2019-03-01 02:00:00      743.5

I would like to avoid any loop as I have a large dataset.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source