无法从雅虎财经提取索引列表

Can't extract index list from yahoo finance

我正在尝试从雅虎金融指数中提取数据,但出于某种原因,当我创建索引列表并尝试提取它时出现错误,但当我单独提取时没有错误。 当我创建包含股票的列表时,相同的代码通常有效。

import pandas_datareader.data as wb
import pandas as pd

tickers = ['^GSPC', '^IXIC', '^GDAXI']

index_data = pd.DataFrame()

for t in tickers:
    index_data[t] = wb.DataReader(t, data_source='yahoo', start='1997-1-1')['Adj Close']

print(index_data.head())

这个错误:

我编辑代码来测试这样的单个调用:

import pandas_datareader.data as wb
import pandas as pd

tickers = ['^GSPC', '^IXIC', '^GDAXI']

index_data = pd.DataFrame()

i = wb.DataReader('^GSPC', data_source='yahoo', start='1997-1-1')['Adj Close']

# for t in tickers:
#     index_data[t] = wb.DataReader(t, data_source='yahoo', start='1997-1-1')['Adj Close']

# print(index_data.head())

print(i)

并得到错误信息:

Exception has occurred: RemoteDataError
Unable to read URL: https://finance.yahoo.com/quote/^GSPC/history?period1=852091200&period2=1648353599&interval=1d&frequency=1d&filter=history

这告诉您错误不是由于列表引起的(这似乎没问题),而是由于调用列表中的第一项。

不知道为什么,昨天的日期 (2022-03-25) 出现了两次。

修复:

tickers = ['^GSPC', '^IXIC', '^GDAXI']

data = []
for t in tickers:
    sr = wb.DataReader(t, data_source='yahoo', start='1997-1-1')['Adj Close']
    sr = sr[~sr.index.duplicated()].rename(t)
    data.append(sr)
    
df = pd.concat(data, axis=1)

输出:

>>> df
                  ^GSPC         ^IXIC        ^GDAXI
Date                                               
1997-01-02   737.010010   1280.699951   2820.810059
1997-01-03   748.030029   1310.680054   2863.260010
1997-01-06   747.650024   1316.400024   2890.199951
1997-01-07   753.229980   1327.729980   2876.340088
1997-01-08   748.409973   1320.349976   2904.080078
...                 ...           ...           ...
2022-03-21  4461.180176  13838.459961  14326.969727
2022-03-22  4511.609863  14108.820312  14473.200195
2022-03-23  4456.240234  13922.599609  14283.650391
2022-03-24  4520.160156  14191.839844  14273.790039
2022-03-25  4543.060059  14169.299805  14305.759766

[6512 rows x 3 columns]