使用Bloomberg函数从Loop创建DataFrame

时间:2016-09-08 11:45:00

标签: python pandas bloomberg

我有一份公司代码列表:

df = {'Ticker': ['AVON LN EQUITY', 'GFS LN EQUITY'], 'Value': [1., 2.]}
df = pd.DataFrame(df)

我正在使用BLPAPI包装器https://github.com/alex314159/blpapiwrapper

使用此我想获得一个DataFrame,每月的所有价格。为此,我使用BLP类和BDH功能。以下设置(较长的版本在链接中):

def bdh(self, strSecurity='SPX Index', strData='PX_LAST', startdate=datetime.date(2013, 1, 1), enddate=datetime.date(2016, 9, 6),
        adjustmentSplit=False, periodicity='MONTHLY', strOverrideField='', strOverrideValue=''):
为简单起见,我创建了一个迷你功能:

def bloom_func(x, func):
    bloomberg = BLP()
    return bloomberg.bdh(x, func, strOverrideField='BEST_FPERIOD_OVERRIDE', strOverrideValue='1GY' )
    bloomberg.closeSession()

使用这些我可以获得一个股权的DataFrame。

Price = bloom_func('VOD LN EQUITY', 'PX_LAST')
print (c)

哪个有效。

然而,当我尝试在两家公司中使用以下方式运行:

df1 = pd.concat([df.apply(lambda x: bloom_func(x)) for p in df['Ticker']])

我得到ValueError:传递值的形状是(2,0),索引暗示(2,2)。

1 个答案:

答案 0 :(得分:1)

比我想象的要简单

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/io/parsers.py", line 795, in __next__
    return self.get_chunk()
  File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/io/parsers.py", line 836, in get_chunk
    return self.read(nrows=size)
  File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/io/parsers.py", line 815, in read
    ret = self._engine.read(nrows)
  File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/io/parsers.py", line 1761, in read
    alldata = self._rows_to_cols(content)
  File "/nfs/sw/python/python-3.5.1/lib/python3.5/site-packages/pandas/io/parsers.py", line 2166, in _rows_to_cols
    raise ValueError(msg)
ValueError: Expected 18 fields in line 28, saw 22