I've two series of bool data, that I'd like to combine to a new series object, but the combination logic depends from "history" (previous values).
Series1 contains mostly False, but single True values. Series2 usually contains periods of True or False values - the probability of repeating values is quite high.
In the resulting series I need periods of bool values that begin with a True-section whenever both are True and end when the section in Series2 ends, i. 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
In row 2 the result switches to True and stays on, until the True-phase in Series2 ends on row 5.
This is what I have come 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
Even though my solution works, I have the impression I am thinking way too difficult to achieve this!?
Any suggestions?
First, we know for sure that result
is always False when s2
is False, and always True when both s1
and s2
are True. That does not depend on previous values:
x.loc[~x['s2'], 'result'] = False
x.loc[x['s1'] & x['s2'], 'result'] = True
Then we fill NA's with "forward fill":
x['result'].fillna(method = 'ffill', inplace = True)
And in case there are some NA's remaining at the beginning of the column, we replace them with False:
x['result'].fillna(False, inplace = True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With