pythonpandasohlc

Resampling of OHLC data


I want to convert 1h OHLC data to xh OHLC data. I am using the resample method like it was proposed in similar threads, but it does not lead to the wanted result. The data:

                            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

The resample method ofr 2h:

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

The result:

                        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

What I would like instead is a dataframe starting at 22:00 and having the data of 21:00 and 22:00 in it and with a second row consisting of 00:00 which uses the data of 23:00 and 00.00.

Thanks a lot for your help!


Solution

  • To get the desired result set the closed and label parameters of resample to 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
    

    The closed parameter controls which end of the interval is inclusive while the label parameter controls which end of the interval appears on the resulting index. right and left refer to end and the start of the interval, respectively.