我使用API从Yahoo获取股票;但是,第一列是['Date'],我需要索引列-以便进一步获取Date与感兴趣的参数。
我尝试添加索引列,但似乎无法正常工作。
import yfinance as yf
import datetime
stocks = ["MU"]
start = datetime.datetime(2016,10,1)
end = datetime.datetime(2019,10,1)
data = yf.download(stocks, start=start, end=end)
------------------------------------------------
#Use pandas to make data frame
df = pd.DataFrame(data)
------------------------------------------------
The data frame like this:
Open High Low Close Adj Close Volume
Date
2016-10-03 17.99 18.02 17.60 17.73 17.73 26856100
2016-10-04 17.98 18.04 17.66 17.80 17.80 54502800
2016-10-05 17.55 18.33 17.28 17.70 17.70 67739500
2016-10-06 17.64 17.89 17.54 17.73 17.73 21606800
2016-10-07 17.72 17.80 17.30 17.61 17.61 23849000
df.index=pd.to_datetime(df.index)
df['Open']
Date
2016-10-03 17.99
2016-10-04 17.98
2016-10-05 17.55
2016-10-06 17.64
2016-10-07 17.72
...
2019-09-25 48.47
2019-09-26 49.38
2019-09-27 45.90
2019-09-30 43.00
2019-10-01 43.10
答案 0 :(得分:1)
Date是索引的名称,而不是列。请尝试以下操作:
df.index=pd.to_datetime(df.index)