You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pandas_datareader调用Yahoo数据触发TypeError问题求助

解决方案:Yahoo财经数据获取报错修复

问题根源

Yahoo财经的页面结构和数据接口已更新,旧版本的pandas_datareader无法正确解析新的返回内容,因此触发TypeError: string indices must be integers错误。

方法一:用yfinance替代(优先推荐)

yfinance是专门适配Yahoo财经的第三方库,维护更活跃,兼容性更好:

  • 安装库:
pip install yfinance
  • 修改后的代码:
import pandas as pd
import numpy as np
from pyhhmm.gaussian import GaussianHMM
import yfinance as yf
import matplotlib.pyplot as plt

start_date = "2017-01-01"
end_date = "2023-01-04"
symbol = "SPY"

# 调用yfinance下载数据
data = yf.download(symbol, start=start_date, end=end_date)
print(data)

方法二:升级pandas_datareader到开发版

如果一定要用pandas_datareader,可以尝试安装最新的开发版本(修复了部分Yahoo接口的适配问题):

  • 安装命令:
pip install git+https://github.com/pydata/pandas-datareader.git
  • 原有代码可继续使用,但该版本可能仍存在不稳定情况。

内容的提问来源于stack exchange,提问作者CitronW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 00:40:40