yfinance: JSONDecodeError base.py

I don’t get this error frequently (maybe once every other day) but it seems to stem from something trying to json decode Python’s None value.

Exception in thread Thread-2960:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/Repositories/trading/env/lib/python3.7/site-packages/multitasking/__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "/home/pi/Repositories/trading/env/lib/python3.7/site-packages/yfinance/multi.py", line 167, in _download_one_threaded
    actions, period, interval, prepost, proxy, rounding)
  File "/home/pi/Repositories/trading/env/lib/python3.7/site-packages/yfinance/multi.py", line 182, in _download_one
    rounding=rounding, many=True)
  File "/home/pi/Repositories/trading/env/lib/python3.7/site-packages/yfinance/base.py", line 155, in history
    data = data.json()
  File "/home/pi/Repositories/trading/env/lib/python3.7/site-packages/requests/models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Because of its infrequent nature and because I run a batch every night I can’t really pinpoint what causes it. The only line I use for retrieving data through yfinance is:

stock_data = yf.download(symbol, start_date, end_date, interval='1d')

I initialize stock_data with this line.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 48

Most upvoted comments

Hi, consistently with the comments from @alanzou, I have realized that the issue is intermittent, and if you retry the call, it probably returns the right data. I have introduced this wrapper into my code. I think that it could be a good idea to enable retries into the function to get history data.


def ticker_history(start, end, interval, yf_ticker):
    for retries in range (0,5):
        try:
            history = yf_ticker.history(start = start,
                                 end = end,
                                 interval = interval)
            return history
        except:
            print ('yfinance JSONDecodeError, retyring: ' + str(retries))
            print ('ticker: ' + yf_ticker.ticker + 'start: ' + str(start) + ';end: ' + str(end) + ';interval: ' + interval)
            time.sleep(5*retries)
            
    return None

Hola, a mi me ha funcionado simplemente sustituyendo el carácter “/” por “-”, he descargado mas de 3000 tickers varias veces en en el mismo día:

datosAnalizar[“Symbol”] = datosAnalizar[“Symbol”].str.replace(“/”,“-”) datosDescargados = pdr.get_data_yahoo(datosAnalizar[“Symbol”].values.tolist(), data_source=‘yahoo’,start=start, end=end, interval=“1wk”)

Saludos

@igor555

That’s correct. Obviously your case is different because you are using yf.ticker rather than yf.download. However, the methodology should be able to work for your case after tinkering with it a bit. Mine has worked flawlessly since I implemented the fix. It’s also doesn’t require time.sleep, which I wanted to avoid using because A )the added time was very annoying and B) it would still produce errors.

If you need all HLOC, you may have some difficulty manipulating the data for each pull. For example, I drop every other column besides Close because the way the dataframes merges caused some difficulty otherwise. This is a relatively easy fix though, as you can just create multiple data frames (one for each data type) and then merge them together after the loop has completed.

Also, go Ravens!

Edit: Looking at your code, i believe each try may overwrite the existing data. In my methodology, I create an initial dataframe outside of the loop, then amend that dataframe with the new data every time the loop is ran.

which version of python are you guys using when got the issue? I had the same issue when using python 3.7.7. But not at all on another mac with python 3.7.1. After I tried upgrade the python 3.7.7 to 3.9, it seems OK now

FYI I’ve been investigating this and made some interesting observations. Here is an error code for a download. The DL fails the first attempt and is then successful in the retry.

1 Failed download:
- CSCO: No data found, symbol may be delisted
CSCO
[*********************100%***********************]  1 of 1 completed

I knew for a fact that Cisco was a listed firm and should have data available. When I searched for CSCO on Yahoo, no results appeared, which makes no sense. It redirected me to a page that said “Symbols similar to ‘csco.’” The same error occurred when I followed a link to the CSCO Yahoo finance page directly from a google search. I used a different computer in my house and the same error occurred. I tested 15 errors and this was the case for all of them.

Here is where it’s interesting: the Yahoo finance website issue only occurred while I was in the process of downloading the data. A few seconds after the download completed, I was able to view the data on the website.

If anyone has the time or inclination, can you see if you can replicate this? I’m inclined to conclude that this has something to do with Yahoo limiting data downloads.

I have a hunch it is an issue/change on Yahoo’s end since it occurred so recently for so many people, unless there were some recent changes to yfinance I’m not aware of. (a new limit from yahoo perhaps?) #363 #361 #360 #359

@seeohsee

This just started becoming an issue for me 2 days ago, and now it happens every time I try to pull data (in a loop). I can get anywhere between 7-14 successful fetches, and then I get the JSONDecode error:

... File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

It is seemingly random which tickers it fails on, as all my tickers are indeed valid.

I’ve also tried adding a time.sleep(10), but this doesn’t prevent the issue. I have ~1000 tickers I’m looking to update, and now can not get past 14-15 tickers. Error is 100% reproducible.

FYI the wrapper I built (above) retries the download until it is successful, without the need for sleep. I still see the JSON errors occurring but it fixes itself by retrying. Antonio’s code does not have the “while true” clause that allows for retries. His method is slightly different.

Unfortunately not. I run this screen on >8000 symbols overnight and wasn’t logging the symbols with errors at the time. I did put a try/except around the data = data.json() line and haven’t had any issues since then