如何正确地重新采样到 5 分钟
How do I resample to 5 min correctly
我正在尝试将 1 分钟柱重采样为 5 分钟,但我得到的结果不正确。
1 分钟数据:
我正在使用它重新采样:
df2.resample("5min").agg({'open':'first',
'high':'max',
'low:'min',
'close':'last'})
我得到:
对于第二行柱线 (00:00:00),最高价应为 110.34 而不是 110.35,收盘价应为 110.33。
我该如何解决这个问题?
编辑 1 创建数据:
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)
我们可以指定 closed='right'
和 label='right'
可选关键字参数
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
我正在尝试将 1 分钟柱重采样为 5 分钟,但我得到的结果不正确。
1 分钟数据:
我正在使用它重新采样:
df2.resample("5min").agg({'open':'first',
'high':'max',
'low:'min',
'close':'last'})
我得到:
对于第二行柱线 (00:00:00),最高价应为 110.34 而不是 110.35,收盘价应为 110.33。
我该如何解决这个问题?
编辑 1 创建数据:
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)
我们可以指定 closed='right'
和 label='right'
可选关键字参数
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