falses following trues, getting pairs

Viewed 38

i have the following table and need all dates or more specific the time difference between row where a true was followed by a false

         date   sent
0  2022-06-01   true
1  2022-06-02   true
2  2022-06-04  false
3  2022-06-08   true
4  2022-06-09   true
5  2022-06-10  false
6  2022-06-11  false
7  2022-06-12  false
8  2022-06-12   true
9  2022-06-14   true

i have the following code for falses followed by a true but am stuck to make the vice versa.

# ensure datetime
df['date'] = pd.to_datetime(df['date'])

# identify False followed by True
# you can easily adapt to identify the True
m = df['sent'].shift(-1)&~df['sent']

# slice True, subtract last False
df.shift(-1).loc[m, 'date'].sub(df.loc[m, 'date'])

it's importnt to only get trues followed by falses(these pairs) and not things in between or falses on falses or trues on trues

0 Answers
Related