避免在Pandas滚动中应用“滚动cummax”

2024-09-28 03:18:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用GroupByRolling来计算timeseries的尾部cummax,如

import time
import pandas as pd


df_example = pd.DataFrame({"value": np.random.normal(1500), "group": 1500 * ["A"]})
t = time.process_time()
df_example["value"].rolling(100).apply(lambda x: x.cummax().max(), raw=False)
elapsed_time = time.process_time() - t
print(elapsed_time)

这正是我所期望的,即计算滚动cummax,但我没有找到一个避免groupby和apply组合的实现方法,这两种方法都很慢,在本例中,在我的计算机上需要0.75秒

谢谢你的想法


Tags: 方法importpandasdftimevalueexampleprocess

热门问题