I have a dataframe. I am performing forward and backward substraction. Later, perform a comparison and produce boolean outputs. Next, I want to perform logical and on these results and produce one result.
Code:
xdf = pd.DataFrame({'data':range(0,6)},index=pd.date_range('2022-06-03 00:00:00', '2022-06-03 00:00:25', freq='5s'))
# perform 1 row backward substraction
bs = xdf['data'].diff(1).abs().le(1)
# perform 1 row forward substraction
fs= xdf['data'].diff(-1).abs().le(1)
bs =
2022-06-03 00:00:00 False
2022-06-03 00:00:05 True
2022-06-03 00:00:10 True
2022-06-03 00:00:15 True
2022-06-03 00:00:20 True
2022-06-03 00:00:25 True
Freq: 5S, Name: data, dtype: bool
fs =
2022-06-03 00:00:00 True
2022-06-03 00:00:05 True
2022-06-03 00:00:10 True
2022-06-03 00:00:15 True
2022-06-03 00:00:20 True
2022-06-03 00:00:25 False
Present and expected output:
xdf['validation'] = np.logical_and(sa,sb)
2022-06-03 00:00:00 False
2022-06-03 00:00:05 True
2022-06-03 00:00:10 True
2022-06-03 00:00:15 True
2022-06-03 00:00:20 True
2022-06-03 00:00:25 False
Freq: 5S, Name: data, dtype: bool
The output is correct and this is what I am expecting. My question, is there a way I can compute all the above (forward substraction and backward substraction) in a single code of line?
abs(a-b)<1It really does not matter whether it isb-aora-bsince you are doing theabs. Hence just calculate the back difference and use that, while changing the first and last values to False since we should not have those values