alpha-xone / xbbg

An intuitive Bloomberg API
https://xbbg.readthedocs.io/
Apache License 2.0
244 stars 51 forks source link

BUG: `ValueError` when calling `BDH` with currency overrides #98

Open jgmarcel opened 1 year ago

jgmarcel commented 1 year ago

Reproducible Example

import datetime
from xbbg import blp

fields = [
    "PX_OPEN",
    "PX_HIGH",
    "PX_LOW",
    "PX_LAST",
    "PX_VOLUME",
    "EQY_WEIGHTED_AVG_PX",
]
tickers = [
    "CC1 Comdty",
    "CO1 Comdty",
    "CRB CMDT Index",
    "CT1 Comdty",
    "DA1 Comdty",
    "KC1 Comdty",
    "LC1 Comdty",
    "LH1 Comdty",
    "RR1 Comdty",
    "S 1 Comdty",
    "SB1 Comdty",
    "W 1 Comdty",
]
start_date = datetime.date(1993, 3, 12)
end_date = datetime.date(2023, 3, 12)
currency = "INR"
df = blp.bdh(
    tickers=tickers,
    flds=fields,
    start_date=start_date,
    end_date=end_date,
    currency=currency,
)

Issue Description

When running the snippet above in a Jupyter cell results:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[77], line 1
----> 1 df = blp.bdh(tickers=tickers, flds=fields, start_date=start_date, end_date=end_date, currency=currency)

File ~\miniconda3\envs\foot\Lib\site-packages\xbbg\blp.py:187, in bdh(tickers, flds, start_date, end_date, adjust, **kwargs)
    181 if res.empty or any(fld not in res for fld in ['ticker', 'date']):
    182     return pd.DataFrame()
    184 return (
    185     res
    186     .set_index(['ticker', 'date'])
--> 187     .unstack(level=0)
    188     .rename_axis(index=None, columns=[None, None])
    189     .swaplevel(0, 1, axis=1)
    190     .reindex(columns=utils.flatten(tickers), level=0)
    191     .reindex(columns=utils.flatten(flds), level=1)
    192 )

File ~\miniconda3\envs\foot\Lib\site-packages\pandas\core\frame.py:9112, in DataFrame.unstack(self, level, fill_value)
   9050 """
   9051 Pivot a level of the (necessarily hierarchical) index labels.
   9052 
   (...)
   9108 dtype: float64
   9109 """
   9110 from pandas.core.reshape.reshape import unstack
-> 9112 result = unstack(self, level, fill_value)
   9114 return result.__finalize__(self, method="unstack")

File ~\miniconda3\envs\foot\Lib\site-packages\pandas\core\reshape\reshape.py:476, in unstack(obj, level, fill_value)
    474 if isinstance(obj, DataFrame):
    475     if isinstance(obj.index, MultiIndex):
--> 476         return _unstack_frame(obj, level, fill_value=fill_value)
    477     else:
    478         return obj.T.stack(dropna=False)

File ~\miniconda3\envs\foot\Lib\site-packages\pandas\core\reshape\reshape.py:499, in _unstack_frame(obj, level, fill_value)
    497 def _unstack_frame(obj: DataFrame, level, fill_value=None):
    498     assert isinstance(obj.index, MultiIndex)  # checked by caller
--> 499     unstacker = _Unstacker(obj.index, level=level, constructor=obj._constructor)
    501     if not obj._can_fast_transpose:
    502         mgr = obj._mgr.unstack(unstacker, fill_value=fill_value)

File ~\miniconda3\envs\foot\Lib\site-packages\pandas\core\reshape\reshape.py:137, in _Unstacker.__init__(self, index, level, constructor)
    129 if num_cells > np.iinfo(np.int32).max:
    130     warnings.warn(
    131         f"The following operation may generate {num_cells} cells "
    132         f"in the resulting pandas object.",
    133         PerformanceWarning,
    134         stacklevel=find_stack_level(),
    135     )
--> 137 self._make_selectors()

File ~\miniconda3\envs\foot\Lib\site-packages\pandas\core\reshape\reshape.py:189, in _Unstacker._make_selectors(self)
    186 mask.put(selector, True)
    188 if mask.sum() < len(self.index):
--> 189     raise ValueError("Index contains duplicate entries, cannot reshape")
    191 self.group_index = comp_index
    192 self.mask = mask

ValueError: Index contains duplicate entries, cannot reshape

Expected Behavior

The code above should return a valid DataFrame.

Installed Versions

>pip show blpapi xbbg
Name: blpapi
Version: 3.19.3
Summary: Python SDK for Bloomberg BLPAPI
Home-page: http://www.bloomberglabs.com/api/
Author: Bloomberg L.P.
Author-email: open-tech@bloomberg.net
License:
Location: ~\miniconda3\envs\foot\Lib\site-packages
Requires:
Required-by:
---
Name: xbbg
Version: 0.7.7
Summary: Intuitive Bloomberg data API
Home-page: https://github.com/alpha-xone/xbbg
Author: Alpha x1
Author-email: alpha.xone@outlook.com
License: Apache
Location: ~\miniconda3\envs\foot\Lib\site-packages
Requires: numpy, pandas, pyarrow, pytest, pytz, ruamel.yaml
Required-by:
``` INSTALLED VERSIONS ------------------ commit : 2e218d10984e9919f0296931d92ea851c6a6faf5 python : 3.11.0.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19044 machine : AMD64 processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252 pandas : 1.5.3 numpy : 1.24.2 pytz : 2022.7.1 dateutil : 2.8.2 setuptools : 67.6.0 pip : 23.0.1 Cython : None pytest : 7.2.2 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 4.9.2 html5lib : 1.1 pymysql : None psycopg2 : 2.9.5 jinja2 : None IPython : 8.11.0 pandas_datareader: None bs4 : 4.11.2 bottleneck : None brotli : fastparquet : None fsspec : 2023.3.0 gcsfs : None matplotlib : 3.7.1 numba : None numexpr : None odfpy : None openpyxl : 3.1.1 pandas_gbq : None pyarrow : 11.0.0 pyreadstat : None pyxlsb : None s3fs : None scipy : None snappy : None sqlalchemy : 2.0.5.post1 tables : None tabulate : None xarray : None xlrd : 2.0.1 xlwt : None zstandard : None tzdata : None ```