Python 使用熊猫对OHLC数据重新采样

Python 使用熊猫对OHLC数据重新采样,python,pandas,Python,Pandas,有很多类似的问题,都有具体的问题和答案,但我没有找到合适的解决方案,也没有理解如何去做 我有典型的数据: date open high low close volume spot 1507842000 5313.3 5345.6 5272 5295.1 22612561 5301.462201 1507845600 5295.1 5326.7 5286.1 5301.1 12127159 5308.487754

有很多类似的问题,都有具体的问题和答案,但我没有找到合适的解决方案,也没有理解如何去做

我有典型的数据:

date        open    high    low     close   volume      spot
1507842000  5313.3  5345.6  5272    5295.1  22612561    5301.462201
1507845600  5295.1  5326.7  5286.1  5301.1  12127159    5308.487754
1507849200  5301.1  5467.5  5301.1  5464.5  54568881    5401.331605
1507852800  5464.7  5497    5394.9  5402.5  58411322    5446.552171
1507856400  5402.1  5542    5402.1  5541.2  50272286    5466.652636
1507860000  5540.4  5980    5440.1  5694.5  182746217   5717.856124
1507863600  5689.8  5800    5604.5  5739.6  78341266    5709.488508
1507867200  5742    5897    5713.1  5753.2  79738461    5794.402674
1507870800  5753.1  5798.9  5520.3  5574.5  87621428    5640.727381
1507874400  5574.6  5672.6  5503.2  5608.4  56964404    5591.237093
1507878000  5607.5  5689.1  5570    5660    46132190    5640.761482
1507881600  5660    5743    5634.8  5652    50173714    5690.219952
但不仅仅是OHLC,还有数量和现货价格

我正在尝试将小时重新采样到天

因此,我加载csv:

data_hourly = pd.read_csv('../data/hourly.csv', parse_dates=True, date_parser=date_parse, index_col=0, header=0)
(日期解析函数正在删除分钟/秒)

我试过:

data_daily = data_hourly.resample('1D').ohlc()
而且,这显然根本不起作用;给我一个包含大量列的行

我试着:

columns_dict = {'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last', 'volume': 'sum', 'spot': 'average'}
每日数据=每小时数据。重新采样('1D',方式=列数)

但这会因为一个错误而崩溃:

%r对象没有属性%r%(类型(self)。名称,属性) AttributeError:“SeriesGroupBy”对象没有属性“average”


此外,它告诉我“如何”字段无论如何都是不推荐的,但我没有看到一个以“新”方式执行的示例。

您很接近,需要
mean
而不是
average
并将其传递给:

columns_dict = {'open': 'first', 'high': 'max', 'low': 'min', 
               'close': 'last', 'volume': 'sum', 'spot': 'mean'}
data_daily = data_hourly.resample('1D').agg(columns_dict)
print (data_daily)
              open    high     low   close     volume         spot
date                                                              
2017-10-12  5313.3  5467.5  5272.0  5464.5   89308601  5337.093853
2017-10-13  5464.7  5980.0  5394.9  5652.0  690401288  5633.099780