简体   繁体   中英

Resampling in pandas

I have asked a question on another thread Link . But I got an incomplete answer. And no one is willing to reply. That is why I am making another modified question. Let me explain the question briefly, I wanted to resample the following 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
`**

The table goes on and on like that. All the timestamps are in milliseconds. And I wanted to resample it into 100L bin time.

df = df.resample('100L')

The resulting table is: 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

But that is not the result I want. because the first timestamp index in the original table is 2403950. So the first bin time should contain from 2403950 to 2404050 but instead it is 2403900 - 2404000. like the following: Timestamp L_x L_y L_a R_x R_y R_a 2403950 ... ... ... ... ... ... 2404050 ... ... ... ... ... ... 2404150 ... ... ... ... ... ... 2404250 ... ... ... ... ... ... 2404350 ... ... ... ... ... ... The rest of the column are the mean of the values of the original table. So to do that someone sugested that I have to calculate the offset. In my case it is 50 milliseconds. And do the following:

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

The offset only moves the labels 50 milliseconds forward but it doesnot change the mean values. It is still calculating the mean of, for instance for the first bin time, values from 2403900 to 2404000 instead of 2403950 to 2404050.

Thanks for your help

You're looking for the base kwarg.

base : int, default 0
For frequencies that evenly subdivide 1 day, the “origin” of the aggregated intervals. For example, for '5min' frequency, base could range from 0 through 4. Defaults to 0


In your case it looks like you want:

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

Note: resample without a DatetimeIndex/PeriodIndex/TimedeltaIndex raises an error in recent pandas, so you should convert to DatetimeIndex before doing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM