Working on creating a dataframe from pulled data from yahoo finance. Can’t seem to resolve the IndexError: index 10 is out of bounds for axis 0 with size 10
Any help is appreciated
import datetime
import pandas as pd
import yfinance as yf
# Allow the full width of the data frame to show.
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 1000)
def getStock(stk, date_dif):
# Set and show dates
dt = datetime.date.today()
dtPast = dt + datetime.timedelta(days=-date_dif)
df = yf.download(stk,
start=datetime.datetime(dtPast.year, dtPast.month, dtPast.day),
end=datetime.datetime(dt.year, dt.month, dt.day), interval="1d")
start = datetime.datetime(dtPast.year, dtPast.month, dtPast.day)
end = datetime.datetime(dt.year, dt.month, dt.day)
print('************************************************************')
print()
print('Daily Percent Changes -', str(dt), "to", str(dtPast), '*', stk.upper(), '*')
print()
print('************************************************************')
return df
def percent_change(prev, curr):
per_change = (curr - prev) / prev
return per_change
def overall_change(start, end):
overall = (end - start) / start
return overall
option = ''
while option != '2':
print("************************************************************")
print()
print("Stock Report Menu Options")
print()
print("************************************************************")
print("1. Report changes for a stock")
print('2. Quit')
option = input()
if option == '1':
symbol = input('Please enter the stock symbol:\n')
num_of_days = int(input('Please enter the number of days for the analysis:\n'))
df = getStock(symbol.upper(), num_of_days)
column_names = ['Close', 'Volume', 'Volume % Change', 'Close % Change']
# Define empty data frame structure.
stock_dat = pd.DataFrame(columns=column_names)
for i in range(num_of_days):
if i != 0:
# Add rows of data.
stock_dat = stock_dat.append({'Close': df.iat[i, 3],
'Volume': df.iat[i, 5],
'Volume % Change': round(percent_change(df.iat[i - 1, 5],
df.iat[i, 5]), 4),
'Close % Change': round(percent_change(df.iat[i - 1, 3],
df.iat[i, 3]), 4)},
ignore_index=True
)
stock_dat.reset_index(drop=True)
stock_dat.reindex_like(df)
else:
stock_dat = stock_dat.append({'Close': df.iat[i, 3],
'Volume': df.iat[i, 5],
'Volume % Change': 0.0000,
'Close % Change': 0.0000},
ignore_index=True
)
stock_dat.reset_index(drop=True)
stock_dat.reindex_like(df)
print(stock_dat)
dt = datetime.date.today()
dtPast= dt - datetime.timedelta(days= -num_of_days)
print("------------------------------------------------------------")
print('Summary of Cumulative Changes for ' + symbol.lower())
print("------------------------------------------------------------")
print(str(dt), 'to', str(dtPast))
close_overall = overall_change(start=df['Close'][0], end=['Close'][num_of_days - 1])
vol_overall = overall_change(start=df['Volume'][0], end=['Volume'][num_of_days - 1])
print("% Volume Change: ", flush=True)
print(round(vol_overall, 3))
print("% Close Price Change:", flush=True)
print(round(close_overall, 3))