OHLC 数据的重采样

时间:2021-08-01 19:42:58

标签: python pandas ohlc

我想将 1h OHLC 数据转换为 xh OHLC 数据。我正在使用 resample 方法,就像在类似线程中提出的那样,但它不会导致想要的结果。数据:

                            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

r 2h的resample方法:

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 的数据.

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

要获得所需的结果,请将 closedlabelresample 参数设置为 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 分别表示区间的结束和开始。