熊猫concat和OHLC问题

时间:2017-11-24 17:24:30

标签: python pandas aggregate resampling

我有一个旧脚本,它过去工作得很好,它被设计为使用tick(bid和ask)数据并使用pandas .resample.agg将其转换为OHLC数据,如下所示:

df = pd.DataFrame(list(MDB.CHART.find()))
DF = df[['dt','bid','ask']]
DF = DF.set_index('dt')

DB = DF['bid'].resample('2T').agg({ 'openbid' : 'first',
                                    'highbid' : 'max',
                                    'lowbid'  : 'min',
                                    'closebid': 'last'})

DA = DF['ask'].resample('2T').agg({ 'openask' : 'first',
                                    'highask' : 'max',
                                    'lowask'  : 'min',
                                    'closeask': 'last'})

dg = pd.concat([DB, DA], axis = 1)

And it would produce the following DataFrame layout:

dt  openbid  highbid  lowbid  closebid  openask  highask lowask closeask
....
....

但是现在当我运行相同的脚本(具有完全相同的数据)时,我得到以下内容:

                                  bid      ask
         dt                                   
closeask 2015-08-19 06:00:00      NaN  1.10619
         2015-08-19 06:02:00      NaN  1.10636
         2015-08-19 06:04:00      NaN  1.10646
         2015-08-19 06:06:00      NaN  1.10657
         2015-08-19 06:08:00      NaN  1.10649
...                               ...      ...
openbid  2015-08-20 13:28:00  1.11661      NaN
         2015-08-20 13:30:00  1.11683      NaN
         2015-08-20 13:32:00  1.11684      NaN
         2015-08-20 13:34:00  1.11697      NaN
         2015-08-20 13:36:00  1.11673      NaN

[7592 rows x 2 columns]

我收集,或者认为大熊猫已经(再次)改变了resampleagg功能?有人可以挥动他们的魔杖并省去我把头靠近文档的麻烦吗?

感谢。

PS。当我使用以下

df = (DF.resample('2T').agg({'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last'}))
I am getting a future warning:

FutureWarning: using a dict with renaming is deprecated and will be removed in a future version

但在文档中还没有任何内容!

1 个答案:

答案 0 :(得分:0)

因此,从现在开始,我们在使用出价和询问数据时必须使用以下内容,因为使用dict重命名将很快折旧:

    db = DF.bid.resample('2T').ohlc().add_suffix('bid')
    da = DF.ask.resample('2T').ohlc().add_suffix('ask')
    df = pd.concat([db, da], axis = 1)

df.head(3)

                     openbid  highbid   lowbid  closebid  openask  highask   lowask  closeask
dt                                                                                           
2015-08-19 06:00:00  1.10586  1.10620  1.10558   1.10615  1.10587  1.10623  1.10560   1.10619
2015-08-19 06:02:00  1.10615  1.10650  1.10611   1.10634  1.10618  1.10652  1.10614   1.10636
2015-08-19 06:04:00  1.10634  1.10674  1.10623   1.10645  1.10637  1.10676  1.10624   1.10646