Python – yfinance library JSONDecodeError when downloading

Question:

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:ProgrammePythonAnacondalibsite-packagesyfinancebase.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:ProgrammePythonAnacondalibsite-packagesrequestsmodels.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:ProgrammePythonAnacondalibjson__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:ProgrammePythonAnacondalibjsondecoder.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:ProgrammePythonAnacondalibjsondecoder.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:

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?

Asked By: reCaptcha

||

Answers:

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.

Answered By: barryodev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.