我无法通过yahoo finance中的pandas下载多种证券的数据。
import pandas as pd
import numpy as np
import pandas_datareader.data as web
df=pd.DataFrame()
symbols = pd.read_excel('C:\Users\alpha\Desktop/sym.xlsx')#eader=None,skiprows=1)
symbols = symbols['values_index'].values.tolist()
for sym in range(2,4):
df[sym]=web.DataReader(symbols[sym],data_source='yahoo',start='2017-2-21',end='2017-2-24')
我收到以下错误
RemoteDataError: Unable to read URL: http://ichart.finance.yahoo.com/table.csv
尽管所有证券在sym.xlsx中都有效
In [] Symbols
Out [] [u"'NESN.VX'", u"'NOVN.VX'", u"'ROG.VX'", u"'HSBA.L'", u"'FP.PA'", u"'BATS.L'"]
答案 0 :(得分:1)
symbols
中有额外的单引号。
演示:
In [12]: web.DataReader("'NESN.VX'", 'yahoo', '2017-02-21', '2017-02-24')
...
skipped
...
RemoteDataError: Unable to read URL: http://ichart.finance.yahoo.com/table.csv
In [13]: web.DataReader("NESN.VX", 'yahoo', '2017-02-21', '2017-02-24')
Out[13]:
Open High Low Close Volume Adj Close
Date
2017-02-21 73.075 73.750 73.00 73.45 756500 73.45
2017-02-22 73.250 74.550 73.20 74.40 1371200 74.40
2017-02-23 74.200 74.900 73.90 74.70 727000 74.70
2017-02-24 74.575 74.775 74.05 74.45 965300 74.45
这是一个有点改进的解决方案:
摆脱额外的单引号
In [71]: s = [x.replace("'",'') for x in symbols]
In [72]: s
Out[72]: ['NESN.VX', 'NOVN.VX', 'ROG.VX', 'HSBA.L', 'FP.PA', 'BATS.L']
一步将所有代码读入Pandas.Panel
In [73]: p = web.DataReader(s, 'yahoo', '2017-02-21', '2017-02-24')
In [74]: p
Out[74]:
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 4 (major_axis) x 6 (minor_axis)
Items axis: Open to Adj Close
Major_axis axis: 2017-02-21 00:00:00 to 2017-02-24 00:00:00
Minor_axis axis: BATS.L to ROG.VX
从Panel
创建DataFrames字典In [75]: df_dict = {sym:p.loc[:,:,sym] for sym in p.minor_axis}
检查
In [76]: df_dict['FP.PA']
Out[76]:
Open High Low Close Volume Adj Close
Date
2017-02-21 47.750 48.815 47.605 48.400 5859500.0 47.78588
2017-02-22 48.920 48.985 47.990 48.305 5448400.0 47.69208
2017-02-23 48.215 48.585 48.100 48.290 3904200.0 47.67727
2017-02-24 48.110 48.350 47.120 47.735 4937300.0 47.12931
In [77]: df_dict['NESN.VX']
Out[77]:
Open High Low Close Volume Adj Close
Date
2017-02-21 73.075 73.750 73.00 73.45 756500.0 73.45
2017-02-22 73.250 74.550 73.20 74.40 1371200.0 74.40
2017-02-23 74.200 74.900 73.90 74.70 727000.0 74.70
2017-02-24 74.575 74.775 74.05 74.45 965300.0 74.45
In [78]: df_dict.keys()
Out[78]: dict_keys(['BATS.L', 'FP.PA', 'HSBA.L', 'NESN.VX', 'NOVN.VX', 'ROG.VX'])