One way would be to first resample
the dataframe to have the same frequency as your wanted step-size (in this case 30 minutes).
Then you can use rolling
with a window size of 16 (i.e., 8 hours).
df['scheduled_departure_utc'] = pd.to_datetime(df['scheduled_departure_utc'])
df.set_index('scheduled_departure_utc').resample('30T').count()['run_id'].rolling(window=16, min_periods=1).sum()
Resulting pandas series:
2021-01-11 13:00:00 2.0
2021-01-11 13:30:00 4.0
2021-01-11 14:00:00 4.0
2021-01-11 14:30:00 4.0
2021-01-11 15:00:00 4.0
2021-01-11 15:30:00 4.0
2021-01-11 16:00:00 4.0
2021-01-11 16:30:00 4.0
2021-01-11 17:00:00 4.0
2021-01-11 17:30:00 4.0
2021-01-11 18:00:00 9.0
2021-01-11 18:30:00 10.0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…