Python Panda datareader - skip erroneous or not available ticker values

Viewed 73

The following python code works for a few stocks, but throws errors randomly for other few stocks. Please can someone help what is it with the

import pandas_datareader as web

tickers=["AMA.AX","AMC.AX"]

mkt_data = web.get_quote_yahoo(tickers)['marketCap']

for stock, mkt in zip(tickers, mkt_data):
    print(stock, mkt)

The above produces the expected output. However, the moment I add another ticker(s) to the list as follows:

tickers=["AMA.AX","AMC.AX","ANA.AX","AAP.AX"]

It throws the following error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-18-255a3bb5b917> in <module>
      4 
      5 # Get market cap
----> 6 market_cap_data = web.get_quote_yahoo(tickers)['marketCap']
      7 
      8 # print stock, price and mktcap and prc

C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\data.py in get_quote_yahoo(*args, **kwargs)
    100 
    101 def get_quote_yahoo(*args, **kwargs):
--> 102     return YahooQuotesReader(*args, **kwargs).read()
    103 
    104 

C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\yahoo\quotes.py in read(self)
     28             data = OrderedDict()
     29             for symbol in self.symbols:
---> 30                 data[symbol] = self._read_one_data(self.url, self.params(symbol)).loc[
     31                     symbol
     32                 ]

C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\base.py in _read_one_data(self, url, params)
    110         else:
    111             raise NotImplementedError(self._format)
--> 112         return self._read_lines(out)
    113 
    114     def _read_url_as_StringIO(self, url, params=None):

C:\ProgramData\Anaconda3\lib\site-packages\pandas_datareader\yahoo\quotes.py in _read_lines(self, out)
     41 
     42     def _read_lines(self, out):
---> 43         data = json.loads(out.read())["quoteResponse"]["result"][0]
     44         idx = data.pop("symbol")
     45         data["price"] = data["regularMarketPrice"]

IndexError: list index out of range

​

I am aware that this error is occurring because the third (added) ticker "ANA.AX" is delisted and has no data available.

Right now, the program just stops at the first instance of missing/unavailable data error.

What I want to achieve is to skip this ticker (ANA.AX), and continue printing the data with the rest of the tickers after it ("AAP.AX" - fourth one). Any idea how to achieve that?

0 Answers
Related