【问题标题】:Create overlapping groups with pandas timegrouper使用 pandas timegrouper 创建重叠组
【发布时间】:2013-12-15 10:02:07
【问题描述】:

我正在使用 Pandas Timegrouper 在 python 中对 pandas 数据框中的数据点进行分组:

grouped = data.groupby(pd.TimeGrouper('30S'))

我想知道是否有办法实现窗口重叠,就像在这个问题中建议的那样:Window overlap in Pandas 同时保持熊猫数据框作为数据结构。

更新:测试了下面提出的三种解决方案的时间,滚动平均值似乎更快:

%timeit df.groupby(pd.TimeGrouper('30s',closed='right')).mean()
%timeit df.resample('30s',how='mean',closed='right')
%timeit pd.rolling_mean(df,window=30).iloc[29::30]

产量:

1000 loops, best of 3: 336 µs per loop
1000 loops, best of 3: 349 µs per loop
1000 loops, best of 3: 199 µs per loop

【问题讨论】:

    标签: python pandas grouping


    【解决方案1】:

    创建一些正好 3 x 30 秒长的数据

    In [51]: df = DataFrame(randn(90,2),columns=list('AB'),index=date_range('20130101 9:01:01',freq='s',periods=90))
    

    以这种方式使用 TimeGrouper 相当于重新采样(这就是重新采样的实际作用) 请注意,我使用了closed 来确保准确包含 30 个观察结果

    In [57]: df.groupby(pd.TimeGrouper('30s',closed='right')).mean()
    Out[57]: 
                                A         B
    2013-01-01 09:01:00 -0.214968 -0.162200
    2013-01-01 09:01:30 -0.090708 -0.021484
    2013-01-01 09:02:00 -0.160335 -0.135074
    
    In [52]: df.resample('30s',how='mean',closed='right')
    Out[52]: 
                                A         B
    2013-01-01 09:01:00 -0.214968 -0.162200
    2013-01-01 09:01:30 -0.090708 -0.021484
    2013-01-01 09:02:00 -0.160335 -0.135074
    

    如果你然后选择 30 秒的间隔,这也是等价的

    In [55]: pd.rolling_mean(df,window=30).iloc[28:40]
    Out[55]: 
                                A         B
    2013-01-01 09:01:29       NaN       NaN
    2013-01-01 09:01:30 -0.214968 -0.162200
    2013-01-01 09:01:31 -0.150401 -0.180492
    2013-01-01 09:01:32 -0.160755 -0.142534
    2013-01-01 09:01:33 -0.114918 -0.181424
    2013-01-01 09:01:34 -0.098945 -0.221110
    2013-01-01 09:01:35 -0.052450 -0.169884
    2013-01-01 09:01:36 -0.011172 -0.185132
    2013-01-01 09:01:37  0.100843 -0.178179
    2013-01-01 09:01:38  0.062554 -0.097637
    2013-01-01 09:01:39  0.048834 -0.065808
    2013-01-01 09:01:40  0.003585 -0.059181
    

    因此,根据您想要实现的目标,使用 rolling_mean 很容易进行重叠 然后选择你想要的任何“频率”。例如,这里是一个 5 秒的重采样,间隔为 30 秒。

    In [61]: pd.rolling_mean(df,window=30)[9::5]
    Out[61]: 
                                A         B
    2013-01-01 09:01:10       NaN       NaN
    2013-01-01 09:01:15       NaN       NaN
    2013-01-01 09:01:20       NaN       NaN
    2013-01-01 09:01:25       NaN       NaN
    2013-01-01 09:01:30 -0.214968 -0.162200
    2013-01-01 09:01:35 -0.052450 -0.169884
    2013-01-01 09:01:40  0.003585 -0.059181
    2013-01-01 09:01:45 -0.055886 -0.111228
    2013-01-01 09:01:50 -0.110191 -0.045032
    2013-01-01 09:01:55  0.093662 -0.036177
    2013-01-01 09:02:00 -0.090708 -0.021484
    2013-01-01 09:02:05 -0.286759  0.020365
    2013-01-01 09:02:10 -0.273221 -0.073886
    2013-01-01 09:02:15 -0.222720 -0.038865
    2013-01-01 09:02:20 -0.175630  0.001389
    2013-01-01 09:02:25 -0.301671 -0.025603
    2013-01-01 09:02:30 -0.160335 -0.135074
    

    【讨论】:

    • 谢谢!如果您不想在滚动重叠窗口中应用滚动平均值而是任意函数怎么办?我使用 time grouper 的原因是提取组,然后分别查看每个组。
    • 您可以将rolling_apply的任意滚动函数与任意函数一起使用
    • 谢谢! rolling_apply回答我的问题
    • 如果我只想返回重叠组以供以后处理怎么办?
    • 我的理解是 TimeGrouper 从一分钟或一秒的开头开始分组,因此例如,如果我执行 TimeGrouper('30s') 它将对 0-30 秒的点和 30- 的点进行分组60 年代。如果我想对 15-45 秒的点进行分组怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多