On 6/28/20 8:53 PM, MRAB wrote:
On 2020-06-28 23:11, Jim wrote:
linux mint 19.3, python 3.6

I wrote a program to download stock info from yahoo using yfinance. I
have been running it unchanged for the past 3 months, today it gave an
error. When looping through a list of stocks the error is random, never
the same position in the list.

I wrote the following little test script to show the error:

import yfinance as yf
import pandas as pd
day = '2020-06-25'
aapl = yf.Ticker('AAPL')
hist = aapl.history(start=day)
print(hist)
close = hist.loc[day]['Close']

I ran it 10 times 8 times I got a dataframe and 2 times I got the error
shown below:

(env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3
/home/jfb/Dev/Python/test_yfinance.py
                Open    High     Low   Close    Volume
Date

2020-06-25  360.70  365.00  357.57  364.84  34380600
2020-06-26  364.41  365.32  353.02  353.63  51270100

(env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3
/home/jfb/Dev/Python/test_yfinance.py
Traceback (most recent call last):
    File "/home/jfb/Dev/Python/test_yfinance.py", line 13, in <module>
      hist = aapl.history(start=day)
    File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/yfinance/base.py", line
155, in history
      data = data.json()
    File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py",
line 897, in json
      return complexjson.loads(self.text, **kwargs)
    File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/__init__.py",
line 518, in loads
      return _default_decoder.decode(s)
    File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py",
line 370, in decode
      obj, end = self.raw_decode(s)
    File
"/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py",
line 400, in raw_decode
      return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I don't know pandas that well. My only contact with it is when a module
I am using depends on it. So does the dataframe look correct?

The error complains of line 1, column 1. Just looking at the dataframe
it looks like Date is on a different line from the rest of the headers
or is that just the result of being printed in the terminal?

On the yfinance github issues page there were a few people reporting
this error. A couple of people reported a work around using try/except.
It worked for some people and not others. It didn't work for me.

I'd appreciate any advice you could give.

It's complaining about the JSON data that it's getting. What does that data look like when it complains?

It might be that there's some kind of limit to how often you can get the data and it's trying to tell you that, but you're not expecting anything back except the data.

You could add some temporary code at line 897 of "/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py" to save the data to a file just before the decoding. Remember to make a backup copy of any source file that you modify!

I don't think it is a limit problem. It happened the first time I ran the script after a week of not using it. I am only geting info on 33 stocks and I know that people use this module to get info on 100's of stocks.

Anyway I was wrong about the try/except not solving the problem. I made a mistake in the try/except and when I corrected it, like a dummy, I never saved the change before running it again.

I will use your suggestions to see if I can figure out the root cause of the problem. as before today it ran for months with no errors. Sorry for taking up the lists time with my mistake and thanks for your help.

Regards,  Jim

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to