Vipin Menon
Vipin Menon

Reputation: 3302

Idenify for sudden drops/dips in time series

I have a timeseries data as follows:

90
100
101
112
115
112
113
120
10
5
2
90
95
111
112
120

In a timeseries like this the range 10->5->2 is a dip. Is there a way to identify this using pandas or numpy?

Currently, I'm using a for-loop like as follows:

for i in range(0, len(timeseries)-1):
   _10percent = timeseries[i] * 0.1
   if timeseries[i+1] <= _10percent and \
      timeseries[i+2] <= _10percent and \
      print("there is a dip")
      break

This logic checks if the next 2/3 data point are consecutively below the 10% value and consider it as a dip

Upvotes: 2

Views: 873

Answers (1)

Simon Hawe
Simon Hawe

Reputation: 4539

You put that data into a series, shift it twice and compare.

ts = pd.Series(data)
ts_10percent = ts * .1
dips = (ts.shift(-1) <= ts_10percent) & (ts.shift(-2) <= ts_10percent)

dips would now be a series of boolean values that is True for dips. If you only want to know if there exists any dip, you can get that via

if dips.any():
    print("There as at least one dip") 

Upvotes: 1

Related Questions