使用pandas_datarader加载数据时遇到JSONDecodeError


import json
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
import yfinance as yf
yf.pdr_override()
style.use('ggplot')
start = dt.datetime(2020,1,1)
end = dt.datetime.now()
df = web.get_data_yahoo('AMD', start, end)
df1 = json.load(df)
print(df1)

错误

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:UsersmonstAppDataLocalProgramsPythonPython37libthreading.py", line 917, in _bootstrap_inner
self.run()
File "C:UsersmonstAppDataLocalProgramsPythonPython37libthreading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:UsersmonstAppDataLocalProgramsPythonPython37libsite-packagesmultitasking__init__.py", line 102, in _run_via_pool
return callee(*args, **kwargs)
File "C:UsersmonstAppDataLocalProgramsPythonPython37libsite-packagesyfinancemulti.py", line 168, in _download_one_threaded
actions, period, interval, prepost, proxy, rounding)
File "C:UsersmonstAppDataLocalProgramsPythonPython37libsite-packagesyfinancemulti.py", line 183, in _download_one
rounding=rounding, many=True)
File "C:UsersmonstAppDataLocalProgramsPythonPython37libsite-packagesyfinancebase.py", line 157, in history
data = data.json()
File "C:UsersmonstAppDataLocalProgramsPythonPython37libsite-packagesrequestsmodels.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:UsersmonstAppDataLocalProgramsPythonPython37libjson__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:UsersmonstAppDataLocalProgramsPythonPython37libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersmonstAppDataLocalProgramsPythonPython37libjsondecoder.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)

我将pandas_datarader更新为0.7,将pandas更新为0.24.1,但它仍然存在。请解释这里发生了什么,也可以尝试这个方法.json((和to_json((,但什么都不起作用

amd = yf.Ticker("amd")
amd_price = amd.history(period="max")
amd_Volume = amd_price["Volume"]
amd_Volume = amd_Volume.reset_index()
amd_Volume = pd.DataFrame(amd_Volume)
print(amd_Volume)
amd_Volume = amd_Volume.sort_values(by="Volume")
print(amd_Volume.tail(1))

看到这个代码它给我同样的错误为什么?

pandas-datareader get_data API返回pandas。DataFrames,而不是字符串json对象或文件。

json.load需要一个文件对象,而不是DataFrame。您得到的错误只是说JSON模块需要不同的对象类型。

您应该能够跳过最后一行,只使用数据帧df

相关内容

  • 没有找到相关文章

最新更新