Home > Mobile >  How do I resample to 5 min correctly
How do I resample to 5 min correctly

Time:09-28

I am trying to resample 1 min bars to 5 min but I am getting incorrect results.

1 min data:

enter image description here

I am using this to resample:

df2.resample("5min").agg({'open':'first',
                          'high':'max',
                          'low:'min',
                          'close':'last'})

I get:

enter image description here

For the second row bar (00:00:00) the high should be 110.34 not 110.35, and the close shoulb be 110.33.

How do I fix this?

EDIT 1 To create data:

import datetime
import pandas as pd
idx = pd.date_range("2021-09-23 23:55", periods=11, freq="1min")
df = pd.DataFrame(index = idx)
data = [110.34,
        110.33,110.34,110.33,110.33,110.33,
        110.32,110.35,110.34,110.32,110.33,
        ]
df['open'] = data
df['high'] = data
df['low'] = data
df['close'] = data

df2 = df.resample("5min").agg({'open':'first',
                          'high':'max',
                          'low':'min',
                          'close':'last'})
print(df)
print("----")
print(df2)

CodePudding user response:

We can specify the closed='right' and label='right' optional keyword arguments

d = {'open':'first','high':'max',
        'low':'min','close':'last'}
df.resample("5min", closed='right', label='right').agg(d)

                       open    high     low   close
2021-09-23 23:55:00  110.34  110.34  110.34  110.34
2021-09-24 00:00:00  110.33  110.34  110.33  110.33
2021-09-24 00:05:00  110.32  110.35  110.32  110.33
  • Related