Python 在熊猫数据帧上滚动720分钟

Python 在熊猫数据帧上滚动720分钟,python,python-3.x,pandas,time-series,pandas-groupby,Python,Python 3.x,Pandas,Time Series,Pandas Groupby,我有一个如下所示的数据集。我们的想法是查看之前的每720分钟,而不是我们在grouper函数中使用的频率。我想看看前720分钟的积极变化。它实际上不计算720分钟内的正和 row Timestamp Direction Positive Neg Nut 1 1/20/19 12:15 2 1/20/19 12:17 Nut 3 1/20/19 12:17 Neg 4 1/20/19 12:18

我有一个如下所示的数据集。我们的想法是查看之前的每720分钟,而不是我们在grouper函数中使用的频率。我想看看前720分钟的积极变化。它实际上不计算720分钟内的正和

row      Timestamp     Direction     Positive      Neg        Nut

 1    1/20/19 12:15    
 2    1/20/19 12:17    Nut
 3    1/20/19 12:17    Neg
 4    1/20/19 12:18    Neg
 5    1/20/19 12:19    Pos
 6    1/20/19 12:20    Neg
 7    1/20/19 12:21    Neg
 8    1/20/19 12:22    Pos
 9    1/20/19 12:23    Neg
 10   1/20/19 12:24    Pos
 11   1/20/19 12:25    Neg
 12   1/20/19 12:26    Neg
 13   1/20/19 12:27    Neg
 14   1/20/19 12:29    Neg
 15   1/20/19 12:29    Nut
 720   1/20/19 12:30   Pos           230(o2:o720)    284       205
 721   1/20/19 12:31   Nut           230(o3:o721)    284       206
因此,我在excel中执行
=COUNTIF(Direction2:Direction721,“Pos”)
来计算正列。我尝试了60分钟和15分钟的代码,但是当我使用720分钟,也就是12小时时,我没有得到我想要的正的,负的计数。它给我一个0,1等的计数,这是完全错误的

已尝试的代码:

cols = df['ChangeDirection'].dropna().unique()
for c in cols:
    df[c] = df['ChangeDirection'].eq(c).rolling('720min').sum()
df.loc[:df.index[0] + pd.Timedelta(720*60, unit="s"), cols] = np.nan

好的,我刚刚意识到我有一个时间间隔,虽然效果很好。

好的,我刚刚意识到我有一个时间间隔,虽然效果很好