pandas pdr.数据读取器-类型错误:字符串索引必须是整数

8ftvxx2r  于 2022-12-21  发布在  其他
关注(0)|答案(2)|浏览(204)

这段代码直到今天都工作得很好。现在我得到了这个错误消息:TypeError:字符串索引必须为整数

import pandas_datareader as pdr
Equity_Indices = ['^GSPC', 'ES=F', 'NQ=F', 'YM=F', '^RUT', '^DJT', 
                  '^GDAXI', '^N225', '^SSMI', '^STOXX50E', '^FCHI', 
                  '^GSPTSE', '^HSI', '000001.SS', '^KS11', '^NSEI', '^AXJO']
Equity_Indices_df = pdr.DataReader(Equity_Indices, 'yahoo', start='1990-01-01', end='today')

怎么了?我有点迷路了。
我完全迷路了,我什么也没改。这个代码昨天工作得很好。

rdrgkggo

rdrgkggo1#

因此,如果您一定要使用pandas_datareader,则需要将其降级为:

pip install pandas_datareader==0.9.0

您的代码将运行

import pandas_datareader as pdr 
import timedelta
import pandas as pd
from datetime import date

Equity_Indices = ['^GSPC', 'ES=F', 'NQ=F', 'YM=F', '^RUT', '^DJT', '^GDAXI', '^N225', '^SSMI', '^STOXX50E', '^FCHI', '^GSPTSE', '^HSI', '000001.SS', '^KS11', '^NSEI', '^AXJO']

print(pdr.__version__)
from datetime import datetime
startdate = '1990-01-01'
today = date.today()
enddate = today

dd = []
for symbol in Equity_Indices:
    try:
        Equity_Indices_df = pdr.DataReader(symbol,'yahoo',startdate,enddate)
        print(Equity_Indices_df.head(5))
        dd.append(Equity_Indices_df)
    except:
        print('did not find: '+symbol)

但将返回:

did not find: ^GSPC
did not find: ES=F
did not find: NQ=F
did not find: YM=F
did not find: ^RUT
did not find: ^DJT
did not find: ^GDAXI
did not find: ^N225
did not find: ^SSMI
did not find: ^STOXX50E
did not find: ^FCHI
did not find: ^GSPTSE
did not find: ^HSI
did not find: 000001.SS
did not find: ^KS11
did not find: ^NSEI
did not find: ^AXJO

这意味着Yahoo已更改了要从中检索符号数据的页面上的某些内容。
现在,唯一的解决办法是执行以下操作:
您需要使用y_finance和使用pdr_overide()

from pandas_datareader import data as pdr
from datetime import datetime,date

import yfinance as yf
yf.pdr_override()

y_symbols = ['^GSPC', 'ES=F', 'NQ=F', 'YM=F', '^RUT', '^DJT', '^GDAXI', '^N225', '^SSMI', '^STOXX50E', '^FCHI', '^GSPTSE', '^HSI', '000001.SS', '^KS11', '^NSEI', '^AXJO']

from datetime import datetime
startdate = datetime(1990,1,1)
today = date.today()
enddate = today

data = pdr.get_data_yahoo(y_symbols, start=startdate, end=enddate)

该函数返回:

print(data.head(2)

   Adj Close                                                           \
           000001.SS ES=F NQ=F YM=F ^AXJO ^DJT ^FCHI       ^GDAXI       ^GSPC   
Date                                                                            
1990-01-02       NaN  NaN  NaN  NaN   NaN  NaN   NaN  1788.890015  359.690002   
1990-01-03       NaN  NaN  NaN  NaN   NaN  NaN   NaN  1867.290039  358.760010   

                         ... Volume                                          \
                ^GSPTSE  ... ^GDAXI        ^GSPC   ^GSPTSE ^HSI ^KS11 ^N225   
Date                     ...                                                  
1990-01-02  3994.230957  ...    0.0  162070000.0  164600.0  0.0   NaN   NaN   
1990-01-03  3999.317871  ...    0.0  192330000.0  147600.0  0.0   NaN   NaN   

                                               
           ^NSEI         ^RUT ^SSMI ^STOXX50E  
Date                                           
1990-01-02   NaN  162070000.0   NaN       NaN  
1990-01-03   NaN  192330000.0   NaN       NaN  

[2 rows x 102 columns]
ni65a41a

ni65a41a2#

我的代码也停止工作了。
看来雅虎已经改变了他们的界面。用麦考伊博士的不朽名言说:“我了解工程师,他们喜欢改变事物。”
我对代码进行了以下更改:

import yfinance as yf

yf.pdr_override()

df = yf.download(ticker, dateStart, dateEnd)

这又造成了一个小麻烦,现在的列顺序不同了,而以前是:

Date, High, Low, Open, Close, Volume, Adj Close

它们现在是:

Date, Open, High, Low, Close, Adj Close, Volume

这应该不是问题,除非你硬编码所有的列代码,所以要小心。
我没有做任何基准测试,但下载感觉更快。

相关问题