Il giorno dom, 20/05/2018 alle 00.33 -0700, Miki Tebeka ha scritto:
> [...]
>
> > > How can I get the longest climb?
> >
> > .diff() (applied on the result of np.sign) will tell you if the
> > sign
> > changes
> > np.where() will then tell you _where_ the sign changes
> > diff() (or equivalent numpy indexing) on the result will tell you
> > the
> > length of the streaks
> > You'll then just need to identify which are climbs and which are
> > falls
> > (by looking back at the result of the original diff())
>
> Is there a way to do this vectorized? I can do a for loop on all the
> location where the sign changes but would like to avoid it.
>
Sure! You can do something like
climbs = streaks[result_of_first_np_sign == 1]
But now that I think about it, there is probably a simpler way to solve
the entire problem. Let s be the original series:
diff_sign = (s.diff() > 0).astype(int)
streak_diff = diff_sign.diff().abs()
streak_count = streak_diff.cumsum() * diff_sign
longest_streak = streak_count.value_counts().drop(0).index[0]
streak_count[streak_count == longest_streak]
Pietro