I have the following DataFrame:
Time: Value:
1000 4.6 - Keep because +ve
2000 3.2 - Keep because +ve
3000 -1.1 - Remove because -ve AND in consecutive group of negatives of size < 3
4000 -0.4 - Remove because -ve AND in consecutive group of negatives of size < 3
5000 0.8 - Keep because +ve
6000 -1.5 - Keep because -ve AND in consecutive group of negatives of size >= 3
7000 -2.1 - Keep because -ve AND in consecutive group of negatives of size >= 3
8000 -3.4 - Keep because -ve AND in consecutive group of negatives of size >= 3
9000 -1.5 - Keep because -ve AND in consecutive group of negatives of size >= 3
10000 -0.3 - Keep because -ve AND in consecutive group of negatives of size >= 3
11000 1.6 - Keep because +ve
12000 2.8 - Keep because +ve
13000 4.0 - Keep because +ve
I want to produce the following DataFrame from it: (Removing small groups of negative values where the group size is less than n, n=3 in this eg.)
Time: Value:
1000 4.6
2000 3.2
5000 0.8
6000 -1.5
7000 -2.1
8000 -3.4
9000 -1.5
10000 -0.3
11000 1.6
12000 2.8
13000 4.0
In the absence of a clever Pandas solution, I am planning to write a element-wise loop which iterates through each row marking consecutive negative values (in a new column), and immediately removing those marked rows when the -ve sequence is interrupted by a +ve value. (Deletion will not occur if the -ve sequence reaches the minimum size of n. The example above shows n=3).
Having deleted the marked rows, I will carry on from where I left off, until the end of the original frame is reached.
I know the proposed solution is not elegant in the Pandas world(!), but cannot figure out how a purist Pandas solution would work. Maybe something using groups or shift?