Let dt denote a dataframe indexed by a Timestamp column and a continuous column called "Value". I'm looking for the intervals in which the curve df["Value"] is stable (the function np.std() is less than 1). I'm asking this problem because my solution is very slow and inefficient. First of all I create a function that finds the number of seconds in which the np.std() is less than 1.
def seconds_of_stability(df,ts):
for seconds_lag in range(1,30):
if df[((df['time']-ts).apply(lambda x: x.total_seconds())).apply(abs)<seconds_lag]['pesos'].std()>1:
return seconds_lag
else:
return seconds_lag
df['seconds_of_stability'] = df['time'].progress_apply(lambda ts: seconds_of_stability(df,ts))
With this information it is possible to construct the intervals (Note that I considered an upper bound of 30 seconds at most).
Is there anyway this can be done in a more fancy way?