dpguthrie / dpguthrie/yahooquery
KeyError when retrieving historical prices
- Dominant language
- Python
- Stars
- 920
- Forks
- 164
- PR merge metrics
- No merged PRs in 30d
Description
**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'
```
Contributor guide
Assessment
This issue has not been assessed yet.