I'm trying to extract data from yahoo finance indices, but for some reason when I create a list of indices and try to extract it gives an error, but when I extract individually there is no error. When I create a list with stocks the same code normally works.
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())
This error:
I don't know why but the yesterday date (2022-03-25) appears twice.
Fix with:
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)
Output:
>>> 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]