Python pandas obtain max consecutive times for a dataframe

Viewed 85

I have a dataframe df as below

time rate
0 1.0
1 0.7
2 0.5
3 0.4
5 0.2
6 0.1

I want to get the maximum consecutive 'time' of it. In this example that will be 4 ('0','1','2','3'; as we don't have a '4' as time) How can I do it in Python / pandas ? Thanks!

1 Answers

In your case do diff with cumsum

df.time.diff().ne(1).cumsum().value_counts().max()
Out[132]: 4
Related