Pandas OHLC aggregation on OHLC data

前端 未结 5 1483
刺人心
刺人心 2020-12-22 23:57

I understand that OHLC re-sampling of time series data in Pandas, using one column of data, will work perfectly, for example on the following dataframe:

>         


        
5条回答
  •  情歌与酒
    2020-12-23 00:28

    You need to use an OrderedDict to keep row order in the newer versions of pandas, like so:

    import pandas as pd
    from collections import OrderedDict
    
    df['ctime'] = pd.to_datetime(df['ctime'], unit='s')
    df = df.set_index('ctime')
    df = df.resample('5Min').agg(
        OrderedDict([
            ('open', 'first'),
            ('high', 'max'),
            ('low', 'min'),
            ('close', 'last'),
            ('volume', 'sum'),
        ])
    )
    

提交回复
热议问题