如何将此yfinance数据转换为pandas数据帧?

2024-09-27 20:17:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图将我从yfinance获得的股票数据转换成数据帧。它不允许我调用日期或调整关闭,因此我无法将其合并到我拥有的其他数据帧中。这看起来很简单,但我真的被卡住了。希望你能帮忙,谢谢

price_history_i = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']
price_history_a = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']
price_history_c = sp.get_price_history('tickername', '2019-12-31', '2020-09-01')['Adj Close']

df = pd.DataFrame([price_history_i, price_history_a, price_history_c])
average_price = df.mean()
print(average_price)

输出:

Date
2019-12-31     9.863333
2020-01-02     9.903333
2020-01-03     9.883333
2020-01-06     9.883333
2020-01-07     9.883333
                ...    
2020-08-25    10.133333
2020-08-26    10.173333
2020-08-27    10.183333
2020-08-28    10.206667
2020-08-31    10.203334
Length: 169, dtype: float64

正如你所看到的,调整收盘价并没有列在价格之上。我正在使用一个简单的函数,我在后台制作了这个函数,它可以让我更快地下载yfinance信息,名为get_price_history

import yfinance as yf



def get_price_history(ticker, sdate, edate):
    data = []
    data = yf.download(ticker, start=sdate, end=edate)
    return (data)

Tags: 数据函数dfclosedatagetpricehistory

热门问题