我正在尝试创建从雅虎财经下载数据后三十天内的增长率。我使用了以下代码和函数,但我的输出在所有天(1-30)天的生长中打印相同的数字。感谢任何帮助。
# Extracting data from yahoo finance
import pandas as pd
import requests
import yfinance as yf
import numpy as np
import matplotlib.pyplot as plt
import time
import datetime
symbols = ['MSFT', 'HD', 'TSLA']
stocks_df = pd.DataFrame({'A' : []})
symbols_daily = yf.download(symbols,
period = "max",
interval = "1d")
for i,ticker in enumerate(symbols):
print(i,ticker)
# Work with stock prices
historyPrices = yf.download(tickers = ticker,
period = "max",
interval = "1d")
# generate features for historical prices, and what we want to predict
historyPrices['Ticker'] = ticker
historyPrices['Year']= historyPrices.index.year
historyPrices['Month'] = historyPrices.index.month
historyPrices['Weekday'] = historyPrices.index.weekday
historyPrices['Date'] = historyPrices.index.date
if stocks_df.empty:
stocks_df = historyPrices
else:
stocks_df = pd.concat([stocks_df, historyPrices], ignore_index=True) and
I created the function:
**# function to create growth**
def get_growth_df(df:pd.DataFrame)->pd.DataFrame:
for i in range(1, 31):
df['future_growth_'+str(i)+'d'] = df['Adj Close'].shift(-1) / df['Adj Close']
GROWTH_KEYS = [k for k in df.columns if k.startswith('future_growth_')]
return df[GROWTH_KEYS]
get_growth_df(historyPrices).describe()
这三十天的每一天都给我相同的值。
我期待这三十天的成长中的每一天都会发生这样的事情。 在此输入图片描述
你不断变换
-1
而不是-i