OHLC数据的重采样

2024-09-28 21:46:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把1h-OHLC数据转换成xh-OHLC数据。我正在使用重采样方法,就像在类似的线程中提出的一样,但它不会导致想要的结果。数据:

                            open     high      low    close    Volume USD
date
2021-07-10 21:00:00  132.060  133.350  131.885  133.195  259057.35815
2021-07-10 22:00:00  133.195  134.160  132.885  134.045  813078.76500
2021-07-10 23:00:00  134.045  134.620  133.690  133.995  338032.62200
2021-07-11 00:00:00  133.995  135.515  133.745  134.390  560713.74425

2h的重采样方法:

df.resample('2H').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
    'Volume USD': 'sum'
})

结果是:

                        open     high      low    close    Volume USD
date
2021-07-10 20:00:00  132.060  133.350  131.885  133.195  2.590574e+05
2021-07-10 22:00:00  133.195  134.620  132.885  133.995  1.151111e+06
2021-07-11 00:00:00  133.995  135.515  133.745  134.390  5.607137e+05

我想要的是一个从22:00开始的数据帧,其中包含21:00和22:00的数据,第二行包含00:00,使用23:00和00.00的数据

非常感谢你的帮助


Tags: 数据方法dfclosedateopen线程agg
1条回答
网友
1楼 · 发布于 2024-09-28 21:46:38

要获得所需结果,请将resampleclosedlabel参数设置为right

df.resample('2H', label='right', closed='right').agg({
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last',
    'Volume USD': 'sum'
})
                        open     high      low    close    Volume USD
date                                                                 
2021-07-10 22:00:00  132.060  134.160  131.885  134.045  1.072136e+06
2021-07-11 00:00:00  134.045  135.515  133.690  134.390  8.987464e+05

closed参数控制区间的哪一端是包含的,而label参数控制区间的哪一端出现在结果索引上rightleft分别表示间隔的结束和开始

相关问题 更多 >