如何在Pandas中创建具有偏移量的滚动时间窗口

2024-03-29 11:36:25 发布

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

我想对带有偏移量的时间窗口内的记录应用一些统计信息。我的数据如下:

                             lon        lat  stat  ...   speed  course  head
ts                                                 ...                      
2016-09-30 22:00:33.272  5.41463  53.173161    15  ...     0.0     0.0   511
2016-09-30 22:01:42.879  5.41459  53.173180    15  ...     0.0     0.0   511
2016-09-30 22:02:42.879  5.41461  53.173161    15  ...     0.0     0.0   511
2016-09-30 22:03:44.051  5.41464  53.173168    15  ...     0.0     0.0   511
2016-09-30 22:04:53.013  5.41462  53.173141    15  ...     0.0     0.0   511

[5 rows x 7 columns]

我需要600秒时间窗口内的记录,300秒的步长。例如,这些窗口:

^{pr2}$

我观察过熊猫来做这个。但它似乎没有添加我上面描述的偏移量的选项。我是忽略了什么,还是应该为此创建一个自定义函数?在


Tags: columns数据信息记录时间headstat偏移量
1条回答
网友
1楼 · 发布于 2024-03-29 11:36:25

您想实现的目标应该是将DataFrame.resample与{}结合起来。在

import pandas as pd

index = pd.date_range('1/1/2000', periods=9, freq='T')
series = pd.Series(range(9), index=index)
df = pd.DataFrame(series)

这将为您提供一个原始的timeseries(示例取自api docsDataFrame.resample)。在

^{pr2}$

现在按您的步骤大小重新采样(请参见DataFrame.shift)。在

sampled = df.resample('90s').sum()

这将为您提供步长大小的非重叠窗口。在

2000-01-01 00:00:00   1                                                                                                                                                                       
2000-01-01 00:01:30   2                                                                                                                                                                       
2000-01-01 00:03:00   7                                                                                                                                                                       
2000-01-01 00:04:30   5                                                                                                                                                                       
2000-01-01 00:06:00  13                                                                                                                                                                       
2000-01-01 00:07:30   8

最后,将采样的df移动一步,并与先前创建的df求和。窗口大小是步长的两倍会有帮助。在

sampled.shift(1, fill_value=0) + sampled

这将产生:

2000-01-01 00:00:00   1                                                                                                                                                                       
2000-01-01 00:01:30   3                                                                                                                                                                       
2000-01-01 00:03:00   9                                                                                                                                                                       
2000-01-01 00:04:30  12                                                                                                                                                                       
2000-01-01 00:06:00  18                                                                                                                                                                       
2000-01-01 00:07:30  21 

也许还有更优雅的解决方案,但我希望这能有所帮助。在

相关问题 更多 >