Combining two pandas series with changing logic
<p>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).</p>

<p>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.</p>

<p>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.</p>

<p>e. g.</p>

<pre><code>  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
</code></pre>

<p>In row 2 the result switches to True and stays on, until the True-phase in Series2 ends on row 5.</p>

<p>This is what I have come up with so far:</p>

<pre><code>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'] &amp; x['s2']).replace(False, np.nan)
x['end'] = (~ (x['s2'].shift() &amp; (~ x['s2']))).replace(True, np.nan)
x['result'] =  x['start'].fillna(x['end']).fillna(method='ffill').fillna(0) &gt; 0
x
</code></pre>

<p>Even though my solution works, I have the impression I am thinking way too difficult to achieve this!?</p>

<p>Any suggestions?</p>