I'm looking for a way to remove the points that ruin the monotonicity of a series.
For example
s = pd.Series([0,1,2,3,10,4,5,6])
or
s = pd.Series([0,1,2,3,-1,4,5,6])
we would extract
s = pd.Series([0,1,2,3,4,5,6])
NB: we assume that the first element is always correct.
Monotonic could be both increasing or decreasing, the functions below will return exclude all values that brean monotonicity.
However, there seems to be a confusion in your question, given the series
s = pd.Series([0,1,2,3,10,4,5,6])
,10
doesn't break monotonicity conditions,4, 5, 6
do. So the correct answer there is0, 1, 2, 3, 10
Output is
0, 1, 2, 3, 10
for increasing and0
for decreasing.Perhaps you want to find the longest monotonic array? because that's a completely different search problem.
----- EDIT -----
Below is a simple way of finding the longest monotonic ascending array given your constraints using plain python:
Note that this solution involves sorting which is O(nlog(n)) + a second step which is O(n).