Reputation: 97
I have a dataframe, 'asset', with columns including 'Direction', 'Open', 'Close' and 'Swing'. I want to generate a column, 'Break', thus:
if (((asset['Direction'] == 1) & (asset.open.shift(2) <= asset['Swing']) & (asset.close.shift(2) >= asset['Swing']) & (asset.open.shift(1) >= asset['Swing']) & (asset.close.shift(1) >= asset['Swing']) & (asset.open >= asset['Swing']) & (asset.close >= asset['Swing'])) |
((asset['Direction'] == -1) & (asset.open.shift(2) >= asset['Swing']) & (asset.close.shift(2) <= asset['Swing']) & (asset.open.shift(1) <= asset['Swing']) & (asset.close.shift(1) <= asset['Swing']) & (asset.open <= asset['Swing']) & (asset.close <= asset['Swing']))):
asset['Break'] = True
However, this generates the following error:
"The truth value of a {type(self).__name__} is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
I thought that I had used the correct "bitwise" operators to avoid this, but the error persists...
Upvotes: 0
Views: 34
Reputation: 249434
That huge expression results in an array of bools. You need to do what it says: decide if you want the if
condition to be "all these bools are true", "none of these bools is true", etc.
But I think what you really meant to do is assign True to the break
column where the conditions were met. For that you should do this:
asset['Break'] = ((asset['Direction'] == 1) & ...
That will assign an array to an array, rather than confusing arrays with scalars as your code did.
Upvotes: 1