第一次出现日期时间索引



我有一个名为Rolling_Max的DateTime索引数据帧。它给出了从11月1日到21年1月1日的每一天的过去2520天的最高价格。如何在其中添加一列,其中包含每个最大值首次出现的日期?

dfsp = pdr.get_data_yahoo('^GSPC', start='2011-01-01', end='2021-01-01')['Adj Close']
window = 2520
Roll_Max = dfsp.rolling(window, min_periods=1).max()

第二行应该有另一列,表示";2011-01-03";

您可以尝试一下,假设价格是pd系列

price_argmax = price.reset_index(drop=True).rolling(2520, min_periods=130).apply(lambda x: x.index[x.argmax()])
price_argmax = pd.Series(price.index[price_argmax.dropna().astype("int")], index = price.index[price_argmax.dropna().index]).reindex(price.index)

如果你需要快速,那么你可能想用numba/numpy写一个循环。

相关内容

最新更新