首页 文章

如何从pandas中的第一个元素开始重新取样?

提问于
浏览
1

我正在重新采样以下表/数据:

Timestamp  L_x   L_y    L_a     R_x     R_y     R_a
2403950   621.3 461.3   313     623.3   461.8   260
2403954   622.5 461.3   312     623.3   462.6   260
2403958   623.1 461.5   311     623.4   464     261
2403962   623.6 461.7   310     623.7   465.4   261
2403966   623.8 461.5   309     623.9   466.1   261
2403970   620.9 461.4   309     623.8   465.9   259
2403974   621.7 461.1   308     623     464.8   258
2403978   622.1 461.1   308     621.9   463.9   256
2403982   622.5 461.5   308     621     463.4   255
2403986   622.4 462.1   307     620.7   463.3   254

table 就这样继续下去 . 时间戳以毫秒为单位 . 我做了以下操作,将其重新采样到100毫秒的bin时间:

  • 我将时间戳索引更改为日期时间格式

df.index = pd.to_datetime((df.index.values*1e6).astype(int))

  • 我在100毫秒内重新采样:

df = df.resample('100L')

生成的重新采样数据如下所示:

Timestamp  L_x   L_y    L_a     R_x     R_y     R_a
2403900   621.3 461.3   313     623.3   461.8   260
2404000   622.5 461.3   312     623.3   462.6   260
2404100   623.1 461.5   311     623.4   464     261
2404200   623.6 461.7   310     623.7   465.4   261
2404300   623.8 461.5   309     623.9   466.1   261

我们可以看到第一个bin时间是2403900,比原始表的第一个时间戳索引落后50毫秒 . 但我希望bin时间从原始表的第一个时间戳索引开始,即2403950.如下所示:

Timestamp  L_x   L_y    L_a     R_x     R_y     R_a
2403950   621.3 461.3   313     623.3   461.8   260
2404050   622.5 461.3   312     623.3   462.6   260
2404150   623.1 461.5   311     623.4   464     261
2404250   623.6 461.7   310     623.7   465.4   261
2404350   623.8 461.5   309     623.9   466.1   261

1 回答

  • 1

    您可以指定偏移量:

    df.resample('100L', loffset='50L')
    

    UPDATE

    当然你可以随时计算偏移量:

    offset = df.index[0] % 100
    df.index = pd.to_datetime((df.index.values*1e6).astype(int))
    df.resample('100L', loffset='{}L'.format(offset))
    

相关问题