Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Decode Error with yFinance [JSONDecodeError: Expecting value: line 1 column 1 (char 0)]

I have been using yfinance for the last several weeks to pull historical data on a number of stocks. I normally run the program at the end of each week to store data for that week, but this problem error just randomly starting occurring this past week. Below is a simple example of calling for historical price data for MMM. However, the same error occurs for option contract methods.

import yfinance as yf
mmm = yf.Ticker('MMM')
mmm.history()

Error Stack:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-6-68e978705cca> in <module>
      1 mmm = yf.Ticker('MMM')
----> 2 mmm.history()

~/opt/anaconda3/lib/python3.8/site-packages/yfinance/base.py in history(self, period, interval, start, end, prepost, actions, auto_adjust, back_adjust, proxy, rounding, tz, **kwargs)
    155                                "Our engineers are working quickly to resolve "
    156                                "the issue. Thank you for your patience.")
--> 157         data = data.json()
    158 
    159         # Work with errors

~/opt/anaconda3/lib/python3.8/site-packages/requests/models.py in json(self, **kwargs)
    896                     # used.
    897                     pass
--> 898         return complexjson.loads(self.text, **kwargs)
    899 
    900     @property

~/opt/anaconda3/lib/python3.8/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

~/opt/anaconda3/lib/python3.8/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~/opt/anaconda3/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
like image 834
Andrei Larion Avatar asked Jul 10 '21 19:07

Andrei Larion


6 Answers

Im new to Stack Overflow so I couldn't upvote but I was having a similar issue. After seeing the answer @Barmar left I went to the yfinance github and the author recommends using pip install yfinance --upgrade --no-cache-dir to resolve any current issues. Worked for me, I hope it works for you since we seem to have similar issues :]

like image 71
RJG Avatar answered Oct 23 '22 12:10

RJG


Try this:

pip uninstall yfinance
pip uninstall pandas-datareader
pip install yfinance --upgrade --no-cache-dir
pip install pandas-datareader
like image 26
Gabriele Nicodemi Avatar answered Oct 23 '22 13:10

Gabriele Nicodemi


tl;dr Sometime in July 2021, Yahoo Finance made some change to their code, and the Python libraries that 'talk' to it now have to be updated, so you probably need to update your module.

Here's my story:

I use a slightly different package called yahoo_fin. I also have been getting this error, and it all started on July 9, 2021. Coincidentally, I saw on yahoo_fin's documentation website here that there were some changes made quite recently on Yahoo Finance, and that may be what's impacting all these libraries that seem to pull their stock data from Yahoo Finance.

Update: July 9th, 2021 yahoo_fin 0.8.9.1 is the latest version of yahoo_fin. This includes a second collection of patches due to recent changes in Yahoo Finance’s website, which were affecting get_data, get_live_price, and several other methods. Please update to 0.8.9.1 if you are using an older version. Additionally, there are two new functions, get_company_info and get_company_officers, for scraping company-related data.

Update: July 2021 yahoo_fin 0.8.9 was released in July 2021. This release includes a patch fixing get request issues due to recent changes on Yahoo Finance. These updates affect several functions, including scraping options data, get_quote_table,

In the end, I had to run pip install yahoo_fin --update, and that resolved it.

like image 5
George Hayward Avatar answered Oct 23 '22 13:10

George Hayward


If Anyone Still facing problem with jsonDecoder error. Try deleting yfin from venv of pycharm folder. Somehow my system was not upgrading yfin in venv of pycharm. I had to manually delete yfin folder and upgrad yfin. Its now working for me. Thanks

like image 2
sachin borkar Avatar answered Oct 23 '22 13:10

sachin borkar


I have given up on yfinance and started using yahoo_fin. It's as simple to use but better due to it's upkeep.

from yahoo_fin.stock_info import *
df = get_data('BYND', start_date='2020-08-14')
like image 1
Kenan Avatar answered Oct 23 '22 12:10

Kenan


I was trying yfinance but yahoo_fin is much better. It doesn't print in your prompt lots of useless messages. So as said Kenan, I have given up on yfinance too. yahoo_fin doesn't stop my thread when returns an error too, so here we have another positive point on it.

In your CMD do:

pip install yahoo-fin

In your code do:

from yahoo_fin.stock_info import *
df = get_data("TSLA")
like image 1
Porta Avatar answered Oct 23 '22 12:10

Porta