【问题标题】:How to create a rolling time window with offset in Pandas如何在 Pandas 中创建带有偏移量的滚动时间窗口
【发布时间】:2019-11-08 19:54:12
【问题描述】:

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

                             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 秒。例如,这些窗口:

start                     end
2016-09-30 22:00:00.000   2016-09-30 22:10:00.000
2016-09-30 22:05:00.000   2016-09-30 22:15:00.000
2016-09-30 22:10:00.000   2016-09-30 22:20:00.000

我已经查看了 Pandas rolling 来执行此操作。但似乎它没有添加我上面描述的偏移量的选项。我是否忽略了某些东西,还是应该为此创建一个自定义函数?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    DataFrame.resampleDataFrame.shift 结合起来应该可以实现您想要实现的目标。

    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)
    

    这将为您提供原始时间序列(示例取自 api docs DataFrame.resample)。

    2000-01-01 00:00:00  0                                                                                                                                                                        
    2000-01-01 00:01:00  1                                                                                                                                                                        
    2000-01-01 00:02:00  2                                                                                                                                                                        
    2000-01-01 00:03:00  3                                                                                                                                                                        
    2000-01-01 00:04:00  4                                                                                                                                                                        
    2000-01-01 00:05:00  5                                                                                                                                                                        
    2000-01-01 00:06:00  6                                                                                                                                                                        
    2000-01-01 00:07:00  7                                                                                                                                                                        
    2000-01-01 00:08:00  8
    

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

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

    这将为您提供 step 大小的非重叠窗口。

    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 
    

    可能有更优雅的解决方案,但我希望这会有所帮助。

    【讨论】:

    • 谢谢!我想就是这样
    猜你喜欢
    • 2023-02-17
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2020-07-13
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多