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

8

21

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)
Vidicon answered 10/7, 2021 at 19:46 Comment(3)
Did you read the message? It looks like the Yahoo Finance service is having an outage.Trident
Premature assumption that a http response contains JSON can cause disappointment, as you’re finding - you should either do a basic check that the response is JSON before attempting to decode it, for example by checking for first character is [ or {, or protect the decoding with try/except.Nomology
I think I see this problem in third question today. And always it the same problem - it uses data from Yahoo which changed its pages and you have to update module to newer version.Evzone
H
39

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 :]

Heartwarming answered 10/7, 2021 at 21:16 Comment(3)
Worked for me too.Hermelindahermeneutic
I have found even you do nothing, but just try enough time, it still has certain possibilities work, and if I have done 'upgrade --no-cache-dir' like your suggestion, I still have the problem. I am using python3.9 with pandas 1.3.4, matplotlib 3.4.3, yfinance 0.1.64Quarterly
Worked for me to, after also having to upgrade pyarrow!Immunize
F
19

Try this:

pip uninstall yfinance
pip uninstall pandas-datareader
pip install yfinance --upgrade --no-cache-dir
pip install pandas-datareader
Flosi answered 19/7, 2021 at 18:49 Comment(1)
It works, it turns out to uninstall the yfinance and reinstall it again with new version, thank you Gabriele NicodemiLalittah
G
5

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.

Gallaway answered 17/7, 2021 at 6:6 Comment(0)
F
2

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

Featherston answered 31/7, 2021 at 11:35 Comment(0)
M
1

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')
Monophagous answered 16/8, 2021 at 3:45 Comment(0)
F
1

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")
Francoisefrancolin answered 9/11, 2021 at 1:52 Comment(0)
L
0

I encountered the same when using "from yahoo_fin import stock_info as si".

The problem solved by:

pip install yahoo_fin --upgrade
Labile answered 6/9, 2021 at 13:55 Comment(0)
C
0

Solution is to replace strings ' ', '^', '/' in the List with ticker names (ticker_list):

ticker_list = [i.strip(' ') for i in ticker_list]
ticker_list = [i.replace('^', '-P') for i in ticker_list]
ticker_list = [i.replace('/', '-') for i in ticker_list]

Cheers.

Cashman answered 15/10, 2021 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.