1

I have the following Pandas Series:

s = pd.Series([False, True, True, False, True, True, True, False, True])

How can I get a list of tuples that each tuple represents the start and end index of continuous True intervals. For the above snippet the expected result would be:

[(1, 2), (4, 6), (8, 8)]
Mo Kamyab
  • 27
  • 4
  • I agree, it is somehow similar to that question; but the first step is to convert the series to indices as you initially answered by doing `trues = s[s].index.values`. And from there you can use that answer. – Mo Kamyab Jul 22 '18 at 02:15
  • Use `start_stop(s, trigger_val=True, len_thresh=0)` from linked dup Q&A. – Divakar Jul 22 '18 at 05:22

1 Answers1

1

IIUC

t=s[s].index.to_series()
t.groupby(t.diff().ne(1).cumsum()).agg(['first','last']).apply(tuple,1).tolist()
Out[257]: [(1, 2), (4, 6), (8, 8)]
BENY
  • 317,841
  • 20
  • 164
  • 234