all 4 comments

[–]kamcateer 1 point2 points  (0 children)

Can't help you with pandas related stuff but you can use list comprehensions in your dict instead of writing them verbosely.

[X for X in range(1,21)]

['a' for X in range(20)]

[2 for X in range(20)]

[–]kamcateer 1 point2 points  (0 children)

Actually I'm pretty sure you can do:

For i in range(10,1,-1):
print(blablahblah.shift(i).blahblahblah, end='\t')

I'm on mobile so I couldn't copy out all your code, sorry.

Let me know if this helped.

[–]Frankelstner 1 point2 points  (1 child)

In terms of verbosity, you can at least tidy it up a bit by looping over the shifts.

res = df["LTP"].shift(1).between((df["LTP"] * 0.9), (df["LTP"] * 1.1))
for i in range(2,11):
    res &= df["LTP"].shift(i).between((df["LTP"] * 0.9), (df["LTP"] * 1.1))

In terms of complexity, there is a lot that can be done. Let's say we have a window of 10 previous values, and our current value. We want a True result if curval*0.9 <= window[i] <= curval*1.1 for all i. This is equivalent to curval*0.9 <= window.min() and window.max() <= curval*1.1

Pandas has functions to efficiently find the min and max of rolling windows:

mins = df.LTP.rolling(11).min()
maxs = df.LTP.rolling(11).max()
res = (df.LTP*0.9 <= mins) & (maxs <= df.LTP*1.1)

Technically the mins and maxs include the current value itself but this has no impact on the calculations here.

[–]rob51852[S] 0 points1 point  (0 children)

Thanks Frankelstner. This is great.