python - 使用 pandas 对 OHLC 数据重新采样

标签 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
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)

(date_parse 函数正在删除分钟/秒)

我尝试过:

data_daily = data_hourly.resample('1D').ohlc()

而且,这显然根本不起作用;给我包含大量列的行。

我尝试过:

columns_dict = {'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last', 'volume': 'sum', 'spot': 'average'}

data_daily = data_hourly.resample('1D', how=columns_dict)

但这会因错误而崩溃:

"%r object has no attribute %r" % (type(self).name, attr) AttributeError: 'SeriesGroupBy' object has no attribute 'average'

此外,它告诉我“how”字段无论如何都已被弃用,但我没有看到以"new"方式执行此操作的示例。

最佳答案

您已经很接近了,需要 mean 而不是 average 并将其传递给 Resampler.agg :

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

关于python - 使用 pandas 对 OHLC 数据重新采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57707094/

相关文章:

python - twisted的twistd工具解析命令行参数

Python效率: is it better to create new variables and assign tasks to it rather than keep on using same variable?

python - Pandas 根据 bool 值创建新列

python - 如何从 pandas groupby 的多个列中获取唯一值

python - 计算两个数据帧之间的半正矢距离

python - Pandas.apply 在 spacy doc 列上返回无值

python - 如何使用 python 的 httplib2 保存文件?

python - 运行 python 的 Unix 进程

python - 如何在 python 中编程 Protocol Buffer 以通过套接字发送消息

python - 在Python中将多列转换为行