I want to calculate the maximum of a column over a sliding window. This solution provides a remedy that works when the index is numeric but I have DatetimeIndex object as my index. I do not want to create a new index column and solve my problem. Instead, I want to solve it directly using rolling
because my data is big. Also, I do not want to use for. Here is a replicable example:
np.random.seed(0)
dtidx = pd.date_range(start='6/1/2022', end='6/2/2022', freq='min')
df = pd.DataFrame(np.cumsum(np.random.randn(len(dtidx))), index=dtidx)
ax = df.plot(figsize=(20, 4))
window = 60
df.rolling(window).max().plot(ax=ax)
plt.show()
df.rolling(window).apply(lambda x: x.idxmax())
My question
How can I catch only maximums and denote them on the graph. Currently, the maximums are denoted by 60 minutes delay.