如何访问Bloomberg API调用的多索引格式中的数据

问题描述 投票:1回答:2

我正在构建一个交易回测应用程序,并设法使用pdblp获取jupyter笔记本中的数据。但是,数据是多层次的,我对数据框架知之甚少,无法正确解压缩。

我需要的是能够访问df [PX_LAST],无论使用何种库存,它都应该相同。它并不像df.keys()那样简单

MultiIndex(levels=[['AHT LN Equity'], ['BEST_PE_RATIO', 'PX_LAST']],
       labels=[[0, 0], [1, 0]],
       names=['ticker', 'field'])

我试过了

df = pd.DataFrame(df.to_records())

但这会导致标题混乱,我在更改名称时遇到了问题。

import pdblp

con = pdblp.BCon(debug=False, port=8194, timeout=5000)

con.start()

df = con.bdh('AHT LN Equity', ['PX_LAST', 'BEST_PE_RATIO'], '20190102', '20190331')

我试过了

DF1 = df.unstack(等级= 1).reset_index()

这没用,而且

import pandas as pd

import numpy as np

df = pd.DataFrame(df.to_records())

后者部分工作,但是很棘手,因为我想将列重命名为没有自动收报机的东西,并且还有撇号的问题,我想重命名失败了。

df.rename(columns={'('AHT LN Equity', 'PX_LAST')': 'Close'},      inplace=True)

File "<ipython-input-37-7677eac9ff45>", line 2
df.rename(columns={'('AHT LN Equity', 'PX_LAST')': 'Close'}, inplace=True)
                        ^
SyntaxError: invalid syntax

任何形式的帮助表示赞赏。

python pandas bloomberg
2个回答
1
投票

你可以用xbbg玩几个例子:

In [1]: from xbbg import blp
In [2]: df = blp.bdh(['AHT LN Equity', 'AGK LN Equity'], ['PX_LAST', 'BEST_PE_RATIO'], start_date='20190102', end_date='20190331')
In [3]: df.tail()
Out[3]:
ticker     AHT LN Equity               AGK LN Equity
field            PX_LAST BEST_PE_RATIO       PX_LAST BEST_PE_RATIO
2019-03-25      1,827.50         11.09        749.96         14.62
2019-03-26      1,805.50         10.96        755.63         14.73
2019-03-27      1,809.00         10.98        751.52         14.71
2019-03-28      1,827.50         11.09        753.48         14.74
2019-03-29      1,852.50         11.24        770.71         15.08

要引用PX_LAST,您可以:

In [4]: df.xs('PX_LAST', axis=1, level=1).tail()
Out[4]:
ticker      AHT LN Equity  AGK LN Equity
2019-03-25       1,827.50         749.96
2019-03-26       1,805.50         755.63
2019-03-27       1,809.00         751.52
2019-03-28       1,827.50         753.48
2019-03-29       1,852.50         770.71

要引用AHT LN Equity的数据,您可以:

In [5]: df['AHT LN Equity'].tail()
Out[5]:
field       PX_LAST  BEST_PE_RATIO
2019-03-25 1,827.50          11.09
2019-03-26 1,805.50          10.96
2019-03-27 1,809.00          10.98
2019-03-28 1,827.50          11.09
2019-03-29 1,852.50          11.24

要么

In [6]: df.loc[:, 'AHT LN Equity'].tail()
Out[6]:
field       PX_LAST  BEST_PE_RATIO
2019-03-25 1,827.50          11.09
2019-03-26 1,805.50          10.96
2019-03-27 1,809.00          10.98
2019-03-28 1,827.50          11.09
2019-03-29 1,852.50          11.24

要引用BEST_PE_RATIOAHT LN Equity,你可以(注意Series名称的细微差别):

In [7]: df['AHT LN Equity']['BEST_PE_RATIO'].tail()
Out[7]:
2019-03-25   11.09
2019-03-26   10.96
2019-03-27   10.98
2019-03-28   11.09
2019-03-29   11.24
Name: BEST_PE_RATIO, dtype: float64

要么

In [8]: df[('AHT LN Equity', 'BEST_PE_RATIO')].tail()
Out[8]:
2019-03-25   11.09
2019-03-26   10.96
2019-03-27   10.98
2019-03-28   11.09
2019-03-29   11.24
Name: (AHT LN Equity, BEST_PE_RATIO), dtype: float64

0
投票

使用pd.IndexSlice

当尝试在多索引中切片列或索引时,pd.IndexSlice非常有效。

# set idx for ease of use.
idx = pd.IndexSlice

使用.loc的切片

# if you are slicing in the column
df.loc[:, idx[:,'PX_LAST']]

# if you have more than one item in level 1
df.loc[:, idx[:,['BEST_PE_RATIO','PX_LAST']]

# if you wish to add slice up one level
df.loc[:, idx['AHT LN Equity','BEST_PE_RATIO']]

# if you are slicing in the index
df.loc[idx[:,'SOME_INDEX'],:]

# if you are slicing in both index and column
df.loc[idx[:,'SOME_INDEX'], idx[:,'PX_LAST']]
© www.soinside.com 2019 - 2024. All rights reserved.