twelvedata / twelvedata-python

Twelve Data Python Client - Financial data API & WebSocket
https://twelvedata.com
MIT License
392 stars 57 forks source link

[Bug] "values" KeyError generated in multi-index conversion #13

Closed g8a9 closed 4 years ago

g8a9 commented 4 years ago

Describe the bug An uncaught KeyError exception is generated at line 165 of twelvedata/utils.py. The exception is about the key values missing in the dictionary data accessed at that line. The error happens when the module tries to convert the result of the call to the Twelvedata API to a pandas multi-index.

The strangest thing is that I don't experience problems until I add specific tickers to the request (See section below)

To Reproduce Steps to reproduce the behavior:

This code causes the error:

SAMPLES = 1440
SYMBOLS = [
    "BTC/USD",
    "ETH/USD",
    "ADA/USD",
    "DOGE/USD",
    "BCH/USD"
]
EXCHANGE = "Binance"

td = TDClient(apikey=API_KEY)
ts = td.time_series(
    symbol=SYMBOLS,
    outputsize=SAMPLES,
    interval="1min",
    timezone="America/New_York",
    exchange=EXCHANGE
)
ts.as_pandas()

KeyError Traceback (most recent call last)

in 17 exchange=EXCHANGE 18 ) ---> 19 ts.as_pandas() ~/.local/lib/python3.7/site-packages/twelvedata/time_series.py in as_pandas(self, **kwargs) 107 108 if self.price_endpoint_enabled: --> 109 df = self.price_endpoint.as_pandas() 110 else: 111 df = None ~/.local/lib/python3.7/site-packages/twelvedata/mixins.py in as_pandas(self, **kwargs) 42 data = self.as_json() 43 if hasattr(self, 'is_batch') and self.is_batch: ---> 44 df = convert_collection_to_pandas_multi_index(data) 45 else: 46 df = convert_collection_to_pandas(data, **kwargs) ~/.local/lib/python3.7/site-packages/twelvedata/utils.py in convert_collection_to_pandas_multi_index(val) 163 arr = [] 164 for symbol, data in val.items(): --> 165 for quote in data['values']: 166 arr.append(tuple(quote.values())[1:]) 167 KeyError: 'values'

While if I remove the BCH/USD ticker, it runs smoothly:

SAMPLES = 1440
SYMBOLS = [
    "BTC/USD",
    "ETH/USD",
    "ADA/USD",
    "DOGE/USD"
]
EXCHANGE = "Binance"

td = TDClient(apikey=API_KEY)
ts = td.time_series(
    symbol=SYMBOLS,
    outputsize=SAMPLES,
    interval="1min",
    timezone="America/New_York",
    exchange=EXCHANGE
)
ts.as_pandas()

Desktop (please complete the following information):

midasSSS commented 4 years ago

Fixed in the new v1.0.2 release