Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – yfinance library JSONDecodeError when downloading

I run

import yfinance as yf
df = yf.Ticker("NOK").history(start_date="2020-11-30", end_date="2021-09-30", interval="1h")

which returns

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-15-c5289e094c7b> in <module>
      1 import yfinance as yf
      2 
----> 3 df = yf.Ticker("NOK").history(start_date="2020-11-30", end_date="2021-09-30", interval="1h")

D:\Programme\Python\Anaconda\lib\site-packages\yfinance\base.py in history(self, period, interval, start, end, prepost, actions, auto_adjust, back_adjust, proxy, rounding, tz, **kwargs)
    155                 # Every valid ticker has a timezone. Missing = problem
    156                 shared._DFS[self.ticker] = utils.empty_df()
--> 157                 shared._ERRORS[self.ticker] = err_msg
    158                 if "many" not in kwargs and debug_mode:
    159                     print('- %s: %s' % (self.ticker, err_msg))

D:\Programme\Python\Anaconda\lib\site-packages\requests\models.py in json(self, **kwargs)
    896             if self.status_code == 0 or self.raw is None:
    897                 self._content = None
--> 898             else:
    899                 self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b""
    900 

D:\Programme\Python\Anaconda\lib\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

D:\Programme\Python\Anaconda\lib\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):

D:\Programme\Python\Anaconda\lib\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)

I tried to update the library with following code from another question here:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pip install yfinance –upgrade –no-cache-dir

Furthermore, I am worried that this kind of hourly data is not available.
but this changed nothing. Could you please help me out?

>Solution :

I think there’s an error with how you’re installing the yfinance module.

The following lines setup a virtual environment with yfinance installed for ubuntu 20.04. (let me know if you’re on something else only minor differences)

python3 -m venv venv
source venv/bin/activate
pip install yfinance

After that the following python code:

import yfinance as yf
print(yf.Ticker("NOK").history(start_date="2020-11-30", end_date="2021-09-30", interval="1h"))

prints out:

Open   High  ...  Dividends  Stock Splits
2022-10-11 12:30:00-04:00  4.3800  4.400  ...        0.0             0
2022-10-11 13:30:00-04:00  4.3700  4.380  ...        0.0             0
2022-10-11 14:30:00-04:00  4.3650  4.370  ...        0.0             0
etc...

So I can confirm that’s no issue with installing the library from pip and the data exists for that ticker/time period. There’s a problem with how you’re setting up your python environment and making libraries available to it. I’d suggest reading the docs for virtual environments and pip.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading