如何从 pandas 的第一个元素开始重新采样? [英] How to resample starting from the first element in pandas?

查看:64
本文介绍了如何从 pandas 的第一个元素开始重新采样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am resampling the following table/data:

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

桌子就这样继续下去. 时间戳以毫秒为单位.我进行了以下操作以将其重新采样为100毫秒的bin时间:

The table goes on and on like that. The timestamps are in milliseconds. I did the following to resample it into 100milliseconds bin time:

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

  1. I changed the timestamp index into a datetime format

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

我在100毫秒内对其进行了重新采样:

I resampled it in 100milliseconds:

df = df.resample('100L')

重新采样后的数据如下:

The resulting resampled data look like the following:

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开始.如下所示:

As we can see the first bin time is 2403900, which is 50milliseconds behind the first timestamp index of the original table. But i wanted the bin time to start from the first timestamp index from the original table, which is 2403950. like the following:

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

推荐答案

您可以指定一个偏移量:

You can specify an offset:

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

更新

当然,您始终可以计算出偏移量:

Of course you can always calculate the offset:

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

这篇关于如何从 pandas 的第一个元素开始重新采样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆