Issue
all help appreciated on the following:
I have the following code implemented, which filters results from a pandas dataFrame in 4 steps:
mask = ( (stock_hist['confirmed']== True) &\
(stock_hist['prevday_confirmed'] == False) & \
(stock_hist['nextday_confirmed'] == False) &\
(stock_hist['nextday_above_supp'] == True) &\
(stock_hist['prevday_above_supp'] == True)
)
result1 = stock_hist[mask]
mask = ( \
(stock_hist['confirmed'] == True) & \
(stock_hist['prevday_confirmed'] == False) & \
(stock_hist['prevday_above_supp'] == True) &\
(stock_hist['nextday_confirmed'] == True) & \
(stock_hist['current_dist'] < stock_hist['nextday_dist']) \
)
result2 = stock_hist[mask]
mask = ( (stock_hist['confirmed']== True) &\
(stock_hist['prevday_confirmed'] == True) & \
(stock_hist['nextday_confirmed'] == False) &\
(stock_hist['nextday_above_supp'] == True) &\
(stock_hist['current_dist'] < stock_hist['prevday_dist'])
)
result3 = stock_hist[mask]
mask = ( (stock_hist['confirmed']== True) &\
(stock_hist['prevday_confirmed'] == True) & \
(stock_hist['nextday_confirmed'] == True) &\
(stock_hist['current_dist'] < stock_hist['prevday_dist']) &\
(stock_hist['current_dist'] < stock_hist['nextday_dist'])
)
result4 = stock_hist[mask]
result = result1.append([result2, result3, result4])
Now, this code does exactly what I expect it to do.
However, I would expect that I should be able to do this in one single mask, like so:
mask = ( (stock_hist['confirmed']== True) &\
~(stock_hist['prevday_confirmed'] == False) & \
~(stock_hist['nextday_confirmed'] == False) &\
~(stock_hist['nextday_above_supp'] == True) &\
~(stock_hist['prevday_above_supp'] == True) \
| \
(stock_hist['confirmed'] == True) & \
~(stock_hist['prevday_confirmed'] == False) & \
~(stock_hist['prevday_above_supp'] == True) &\
~(stock_hist['nextday_confirmed'] == True) & \
~(stock_hist['current_dist'] < stock_hist['nextday_dist']) \
| \
:
:
:
But when I do that, it is as if the | acts as an & ? Because it renders FALSE for the entire mask, also for those rows that get filtered out succesfully with the first code....
What am i missing here?
Solution
This is a typical example of why the order of operations matters: just like 2 + 3 x 4
is not equal to (2 + 3) x 4
, you need to add one more layer of parenthesis between your conditions.
(A & B | C & D) != ((A & B) | (C & D))
In your case, if of the mask has to be put between parenthesis in order to cumulate them in one condition using |
Answered By - Hugolmn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.