【发布时间】:2016-07-13 09:15:41
【问题描述】:
我了解,使用一列数据对 Pandas 中的时间序列数据进行 OHLC 重新采样将完美运行,例如在以下数据帧上:
>>df
ctime openbid
1443654000 1.11700
1443654060 1.11700
...
df['ctime'] = pd.to_datetime(df['ctime'], unit='s')
df = df.set_index('ctime')
df.resample('1H', how='ohlc', axis=0, fill_method='bfill')
>>>
open high low close
ctime
2015-09-30 23:00:00 1.11700 1.11700 1.11687 1.11697
2015-09-30 24:00:00 1.11700 1.11712 1.11697 1.11697
...
但是如果数据已经是 OHLC 格式,我该怎么办?据我所知,API 的 OHLC 方法会为每一列计算一个 OHLC 切片,因此如果我的数据采用以下格式:
ctime openbid highbid lowbid closebid
0 1443654000 1.11700 1.11700 1.11687 1.11697
1 1443654060 1.11700 1.11712 1.11697 1.11697
2 1443654120 1.11701 1.11708 1.11699 1.11708
当我尝试重新采样时,每列都会得到一个 OHLC,如下所示:
openbid highbid \
open high low close open high
ctime
2015-09-30 23:00:00 1.11700 1.11700 1.11700 1.11700 1.11700 1.11712
2015-09-30 23:01:00 1.11701 1.11701 1.11701 1.11701 1.11708 1.11708
...
lowbid \
low close open high low close
ctime
2015-09-30 23:00:00 1.11700 1.11712 1.11687 1.11697 1.11687 1.11697
2015-09-30 23:01:00 1.11708 1.11708 1.11699 1.11699 1.11699 1.11699
...
closebid
open high low close
ctime
2015-09-30 23:00:00 1.11697 1.11697 1.11697 1.11697
2015-09-30 23:01:00 1.11708 1.11708 1.11708 1.11708
有没有人愿意分享的快速(ish)解决方法,而无需我深入了解熊猫手册?
谢谢。
ps,有这个答案 - Converting OHLC stock data into a different timeframe with python and pandas - 但那是 4 年前的事了,所以我希望有一些进展。
【问题讨论】:
标签: python python-2.7 pandas dataframe resampling