yahooquery
yahooquery copied to clipboard
KeyError when retrieving historical prices
Describe the bug
Sometimes I get a KeyError when retrieving historical prices for a valid symbol. This exception is not an appropriate way to deal with missing data.
To Reproduce
yq.Ticker(['ANEW', 'SPY']).history(period='14wk', interval='1wk', adj_ohlc=True)
Note that both ANEW and SPY are valid symbols.
Expected behavior
If data is missing, whether for a single symbol or for all symbols, yq can retry or the dataframe can have missing rows or show pd.NA values. It should never cause KeyError for the first of 30 symbols in the list.
However this is fixed, the resulting behavior should be well defined.
Desktop (please complete the following information):
sys.version
Out[1]: '3.9.2 (default, Feb 20 2021, 20:57:50) \n[GCC 7.5.0]'
yq.__version__
Out[2]: '2.2.15'
Additional context The traceback is:
File "/home/homeuser/PycharmProjects/.venv/price-sync/lib/python3.9/site-packages/yahooquery/ticker.py", line 1242, in history
df = self._historical_data_to_dataframe(data, params, adj_timezone)
File "/home/homeuser/PycharmProjects/.venv/price-sync/lib/python3.9/site-packages/yahooquery/ticker.py", line 1269, in _historical_data_to_dataframe
if "timestamp" in data[symbol]:
KeyError: 'ANEW'
Have the same bug
Agreed - not a great way to handle this. I'm unable to reproduce just copying and pasting above but that doesn't mean this error has gone away. Let me know if this is still happening.
https://www.loom.com/share/a9bd504f841c478bae439ac093c2ad9d