I have stock data where I am trying to find if the price is decreasing continuously for 5 days. sample data:
I have grouped by Symbol
.
df_s = df.groupby(['Symbol'])
The function I am trying to apply for every group is:
def strictly_decreasing(L):
#---it will return True if L(list) is decreasing, eg:[7,5,3,2] = True---
return all(x>y for x, y in zip(L, L[1:]))
def strick_dec(group):
dec = []
for i in range(0,len(group)-4):
chec = group["close"][i:i+5]
dec.append(strictly_decreasing(list(chec)))
dummy = [False,False,False,False] # because we skiped last 4 values
final = dec + dummy # can also be used as dummy + dec, up to you
group['Strictly_decreasing'] = final # adding the results as new column
return group
df_new = df_s.apply(strick_dec)
How can we speed up this process? it's taking too much time.
It will probably never be really fast, but using
pandas
methods only will speed things up a little bit. The nested loops are not really needed. Try something like this:Output: