Looking for a method to do the following, but more efficient.
I am taking stock data and looking at the Highs, and lows for the last 5 days and putting it in a new data frame:
Date(index) High Low
datetime obj1 1 1
datetime obj2 2 2
datetime obj3 3 3
datetime obj4 4 4
datetime obj5 5 5
datetime obj6 6 6
Will become
Date(index) High Low
datetime obj1 [] []
datetime obj2 [] []
datetime obj3 [] []
datetime obj4 [] []
datetime obj5 [1,2,3,4,5] [1,2,3,4,5]
datetime obj6 [2,3,4,5,6] [2,3,4,5,6]
Here is the code I have, it works, but is a brute forced nested for loop. Is there a way to vectorize this or at least pull the data faster?
df = getdata("SWBI",today,days_back) #just makes the df for the stock data
date_list = df.index.to_list() #makes a list of dates to iterate over
counter= 0
df_predictions= pd.DataFrame({
"date":[],
"hi_his":[],
"lo_his":[]
})
for i in date_list:
dates = date_list[counter-5:counter] #Makes a list of the previous 5 dates
counter += 1
hi = []
lo = []
for date in dates: #makes a list of the values for those 5 days
lo.append(df.loc[date]["Low"])
hi.append(df.loc[date]["High"])
#Make a temporary df to append
df_temp= pd.DataFrame({
"date":i,
"hi_his":[hi],
"lo_his":[lo]
})
df_predictions = df_predictions.append(df_temp) #df ready to de linear regression predictions
I know