I have two bool data series that I would like to combine with a new series object, but the combination logic depends on the βhistoryβ (previous values).
Series 1 contains mostly False, but single True values. Series 2 usually contains periods of True or False values ββ- the probability of a repeat of the values ββis quite high.
In the resulting series, I need periods of bool values ββthat start with the True section, when both are True and end when the section ends in Series2, i.e. e. no longer contains True.
e. g.
s1 s2 result 0 False False False 1 False True False 2 True True True 3 False True True 4 False True True 5 True False False 6 False False False
On line 2, the result switches to True and remains on until the true phase in series 2 ends on line 5.
This is what I came up with so far:
import pandas as pd import numpy as np x = pd.DataFrame() x['s1'] = [False, False, True, False, False, True, False] x['s2'] = [False, True, True, True, True, False, False] x['start'] = (x['s1'] & x['s2']).replace(False, np.nan) x['end'] = (~ (x['s2'].shift() & (~ x['s2']))).replace(True, np.nan) x['result'] = x['start'].fillna(x['end']).fillna(method='ffill').fillna(0) > 0 x
Despite the fact that my solution works, I got the impression that I'm too hard for this !?
Any suggestions?