Pandas DataReader 在从雅虎财经获取股票行情时抛出日期错误
Pandas DataReader Throws a Date Error When Getting Stock Quotes From Yahoo Finance
当我 运行 DateReader 程序从雅虎财经获取报价时,它给了我一个错误信息“KeyError: 'Date'”
import pandas as pd
import pandas_datareader.data as web
from datetime import datetime
start = datetime(2015, 1, 1)
end = datetime.today()
ticker_dict = {}
for idx, ticker in enumerate(['AAPL', 'TSLA', 'IBM', 'LNKD']):
df_ticker = web.DataReader(ticker, 'yahoo', start, end)
ticker_dict[ticker] = df_ticker['Close']
stocks = DataFrame(ticker_dict)
但是,如果我只是 运行 DataReader 行,它就起作用了。
df = web.DataReader(['AAPL', 'TSLA', 'IBM', 'LNKD'], 'yahoo', start, end)
有人知道第一个代码有什么问题吗?
问题就在那里,因为它在第一个代码中为 'LNKD' 抛出了异常。放一个try/except
块。
import pandas as pd
import pandas_datareader.data as web
from datetime import datetime
start = datetime(2015, 1, 1)
end = datetime.today()
ticker_dict = {}
for idx, ticker in enumerate(['AAPL', 'TSLA', 'IBM', 'LNKD']):
try:
df_ticker = web.DataReader(ticker, 'yahoo', start, end)
ticker_dict[ticker] = df_ticker['Close']
except:pass
stocks = pd.DataFrame(ticker_dict)
输出:
AAPL TSLA IBM
Date
2014-12-31 110.379997 222.410004 160.440002
2015-01-02 109.330002 219.309998 162.059998
2015-01-05 106.250000 210.089996 159.509995
2015-01-06 106.260002 211.279999 156.070007
2015-01-07 107.750000 210.949997 155.050003
... ... ... ...
2020-08-03 435.750000 1485.000000 124.309998
2020-08-04 438.660004 1487.000000 125.839996
2020-08-05 440.250000 1485.020020 125.449997
2020-08-06 455.609985 1489.579956 126.120003
2020-08-07 444.450012 1452.709961 124.959999
[1411 rows x 3 columns]
第二个代码也没有'LNKD'的数据。都是 NaN
当我 运行 DateReader 程序从雅虎财经获取报价时,它给了我一个错误信息“KeyError: 'Date'”
import pandas as pd
import pandas_datareader.data as web
from datetime import datetime
start = datetime(2015, 1, 1)
end = datetime.today()
ticker_dict = {}
for idx, ticker in enumerate(['AAPL', 'TSLA', 'IBM', 'LNKD']):
df_ticker = web.DataReader(ticker, 'yahoo', start, end)
ticker_dict[ticker] = df_ticker['Close']
stocks = DataFrame(ticker_dict)
但是,如果我只是 运行 DataReader 行,它就起作用了。
df = web.DataReader(['AAPL', 'TSLA', 'IBM', 'LNKD'], 'yahoo', start, end)
有人知道第一个代码有什么问题吗?
问题就在那里,因为它在第一个代码中为 'LNKD' 抛出了异常。放一个try/except
块。
import pandas as pd
import pandas_datareader.data as web
from datetime import datetime
start = datetime(2015, 1, 1)
end = datetime.today()
ticker_dict = {}
for idx, ticker in enumerate(['AAPL', 'TSLA', 'IBM', 'LNKD']):
try:
df_ticker = web.DataReader(ticker, 'yahoo', start, end)
ticker_dict[ticker] = df_ticker['Close']
except:pass
stocks = pd.DataFrame(ticker_dict)
输出:
AAPL TSLA IBM
Date
2014-12-31 110.379997 222.410004 160.440002
2015-01-02 109.330002 219.309998 162.059998
2015-01-05 106.250000 210.089996 159.509995
2015-01-06 106.260002 211.279999 156.070007
2015-01-07 107.750000 210.949997 155.050003
... ... ... ...
2020-08-03 435.750000 1485.000000 124.309998
2020-08-04 438.660004 1487.000000 125.839996
2020-08-05 440.250000 1485.020020 125.449997
2020-08-06 455.609985 1489.579956 126.120003
2020-08-07 444.450012 1452.709961 124.959999
[1411 rows x 3 columns]
第二个代码也没有'LNKD'的数据。都是 NaN