I don't have a data frame constructor so I can't test this code. But you would want to take your current data frame and stack it. That moves the time into the left. Then, within row groups, use df.resample
while passing a 1 hour time string.
This might look something like this:
# df.columns = pd.to_datetime(df.columns) # if needed
ndf = df.stack() \
.groupby(level=0).resample(level=1, rule='H') \
.mean()
This would convert a sample data frame like this:
>>> df = pd.DataFrame({
... '00:15': [1, 2],
... '00:30': [3, 4],
... '00:45': [5, 6],
... '01:00': [7, 8],
... })
>>> df
00:15 00:30 00:45 01:00
0 1 3 5 7
1 2 4 6 8
Into:
>>> df.stack().groupby(level=0).resample(level=1, rule='H').mean()
0 2023-08-23 00:00:00 3.0
2023-08-23 01:00:00 7.0
1 2023-08-23 00:00:00 4.0
2023-08-23 01:00:00 8.0
dtype: float64
I would not store my data in this format where the time is the column. It isn't Tidy.
This solution keeps the rows as individual units of observations with the groupby
. You might want also to clean up the time. Pandas will store the time as date-time only. I think that issue is outside scope.