Does this dataframe look correct?

Peter Otten __peter__ at web.de
Mon Jun 29 03:16:59 EDT 2020


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 at 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 at 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.

My guess is that pandas is not the source of the problem. The error occurs 
when simplejson tries to parse an empty string:

>>> import simplejson
>>> simplejson.loads("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 488, in 
loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in 
decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 389, in 
raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 
0)

This probably means that yahoo returns an empty string instead of the 
expected JSON. If the error occurs only sporadically and you can identify 
the downloading code in the API source you can try and replace (pseudo-code)

json_data = download_from_yahoo()
df = dataframe_from_json(json_data)

with

while True:
    json_data = download_from_yahoo()
    if json_data: break
    time.sleep(1)  # wait a moment, then retry download
df = dataframe_from_json(json_data)







More information about the Python-list mailing list