Combining two pandas series with changing logic

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?

+5
source share
1 answer

First, we know for sure that result always False when s2 is False, and always True when both s1 and s2 are True. It does not depend on the previous values:

 x.loc[~x['s2'], 'result'] = False x.loc[x['s1'] & x['s2'], 'result'] = True 

Then we fill NA "forward fill":

 x['result'].fillna(method = 'ffill', inplace = True) 

And if some NA remains at the beginning of the column, we will replace them with False:

 x['result'].fillna(False, inplace = True) 
+1
source

Source: https://habr.com/ru/post/1258612/


All Articles