0

如何设置 5 分钟窗口大小以通过滚动平均值重新采样数据?我尝试了不同的方法,但我总是收到关于 DateTimeIndex 的错误,即使我的数据也有 Index as DateTimeIndex..

有谁知道正确的做法吗??

数据

                index   Speed   rolling_meanVal
DateTime            
1/1/2011 0:04   2165    0.057579    NaN
1/1/2011 0:07   3438    0.044646    NaN
1/1/2011 0:10   4713    0.043154    NaN
1/1/2011 0:13   6054    0.014403    NaN
1/1/2011 0:16   7385    0.038972    0.039751
1/1/2011 0:19   8734    0.019927    0.036447
1/1/2011 0:21   10045   0.039548    0.03689
1/1/2011 0:24   11374   0.089709    0.043492
1/1/2011 0:27   12661   0.102816    0.050084
1/1/2011 0:30   13960   0.119699    0.057045
1/1/2011 0:33   15261   0.095108    0.060505
1/1/2011 0:36   16579   0.051854    0.059784
1/1/2011 0:40   17848   0.035654    0.057928
1/1/2011 0:43   19163   0.083695    0.059769
1/1/2011 0:46   20458   0.091149    0.061861
1/1/2011 0:49   21784   0.082233    0.063134
1/1/2011 0:52   23105   0.043388    0.061972
1/1/2011 0:55   24415   0.032073    0.060311
1/1/2011 0:58   25689   0.108548    0.06285
1/1/2011 0:59   27117   0.140965    0.066756
1/1/2011 1:02   28492   0.029816    0.065368
1/1/2011 1:05   29861   0.028124    0.064542
1/1/2011 1:09   31195   0.042464    0.064507
1/1/2011 1:12   32471   0.065898    0.067082
1/1/2011 1:15   33793   0.128899    0.071578
1/1/2011 1:18   35094   0.019488    0.071556
1/1/2011 1:21   36407   0.041034    0.071631
1/1/2011 1:24   37728   0.038828    0.069087
1/1/2011 1:27   39053   0.039328    0.065912
1/1/2011 1:30   40340   0.080378    0.063946

这是样本数据,如果我想取 5 分钟窗口大小的滚动平均值?我试过代码

result_frame['Speed'].rolling(window=20,min_periods=5).mean().rename('rollingmenaVal')

但是不明白如何设置5分钟的频率?任何帮助

4

1 回答 1

3

你的窗口将持续'5T'5 分钟,

df['rollingmeanVal'] = df.rolling('5T').Speed.mean()

                     index     Speed  rollingmeanVal
DateTime                                            
2011-01-01 00:04:00   2165  0.057579        0.057579
2011-01-01 00:07:00   3438  0.044646        0.051112
2011-01-01 00:10:00   4713  0.043154        0.043900
2011-01-01 00:13:00   6054  0.014403        0.028779
2011-01-01 00:16:00   7385  0.038972        0.026687
2011-01-01 00:19:00   8734  0.019927        0.029449
2011-01-01 00:21:00  10045  0.039548        0.029738
2011-01-01 00:24:00  11374  0.089709        0.064629
2011-01-01 00:27:00  12661  0.102816        0.096263
2011-01-01 00:30:00  13960  0.119699        0.111258
2011-01-01 00:33:00  15261  0.095108        0.107404
2011-01-01 00:36:00  16579  0.051854        0.073481
2011-01-01 00:40:00  17848  0.035654        0.043754
2011-01-01 00:43:00  19163  0.083695        0.059675
2011-01-01 00:46:00  20458  0.091149        0.087422
2011-01-01 00:49:00  21784  0.082233        0.086691
2011-01-01 00:52:00  23105  0.043388        0.062811
2011-01-01 00:55:00  24415  0.032073        0.037731
2011-01-01 00:58:00  25689  0.108548        0.070311
2011-01-01 00:59:00  27117  0.140965        0.093862
2011-01-01 01:02:00  28492  0.029816        0.093110
2011-01-01 01:05:00  29861  0.028124        0.028970
2011-01-01 01:09:00  31195  0.042464        0.035294
2011-01-01 01:12:00  32471  0.065898        0.054181
2011-01-01 01:15:00  33793  0.128899        0.097399
2011-01-01 01:18:00  35094  0.019488        0.074194
2011-01-01 01:21:00  36407  0.041034        0.030261
2011-01-01 01:24:00  37728  0.038828        0.039931
2011-01-01 01:27:00  39053  0.039328        0.039078
2011-01-01 01:30:00  40340  0.080378        0.059853
于 2018-03-03T21:11:39.187 回答