Search code examples
pythonpython-3.xpandasnumpyyfinance

The same code that used to work is now returning an exception


I'm trying to do a simple correlation between a few stock symbols. I've run the same code before and it worked. Now, it's returning an exception that references a bunch of files and contains other messages that I don't understand. Also, the command has to be manually stopped in the console. I'm very new to python and programming in general. I'm using Spyder 5.0.3 and anaconda3. I've tried reinstalling Spyder and updated the packages used, neither resolved the issue.

The problem appears to be with the yfinance package. When I tried to download price data for a single ticker a similar exception was returned. It doesn't appear to be pandas or numpy because I was able to make a small dataframe and run corrcoeff() without any problem. For what it's worth this is the code I used:

import pandas as pd
import numpy as np

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

r = np.corrcoef(df)

The following is the code that used to work but is no longer working:

import pandas as pd
import numpy as np
import yfinance as yf

AAPL = yf.download('AAPL', period='32D', interval='1D')
MSFT = yf.download('MSFT', period='32D', interval='1D')
NVDA = yf.download('NVDA', period='32D', interval='1D')

corr3 = pd.DataFrame([AAPL['Close'],
                   MSFT['Close'],
                   NVDA['Close']])

r = np.corrcoef(corr3)

When I run the file I receive the following exception in the console:

Exception in thread Thread-10:
Traceback (most recent call last):
  File "C:\Users\jdejo\anaconda3\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\jdejo\anaconda3\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\jdejo\anaconda3\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "C:\Users\jdejo\anaconda3\lib\site-packages\yfinance\multi.py", line 167, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "C:\Users\jdejo\anaconda3\lib\site-packages\yfinance\multi.py", line 179, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "C:\Users\jdejo\anaconda3\lib\site-packages\yfinance\base.py", line 157, in history
    data = data.json()
  File "C:\Users\jdejo\anaconda3\lib\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\jdejo\anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\jdejo\anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\jdejo\anaconda3\lib\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)

I am wanting to know what went wrong with my code. It used to work, now it doesn't. Any guidance would be greatly appreciated.


Solution

  • I have solved the problem. I needed to update yfinance through the console in spyder. I had been using the command prompt to update packages.