can anyone tell me why im getting:
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
on this:
quotesstock['crossup'] = (quotesstock['volatility']*5.5 > quotesstock['vix'] & quotesstock['volatility'].shift(-1, axis = 0) <= quotesstock['vix'].shift(-1, axis = 0))
the individual parts do give a lists of dates(index) and booleans but i cant seem to implement an and.
quotesstock['volatility']*5.5 > quotesstock['vix']
Out[158]:
Date
2019-12-31 False
2020-01-02 False
2020-01-03 False
2020-01-06 False
2020-01-07 False
2022-04-19 True
2022-04-20 True
2022-04-21 True
2022-04-22 False
2022-04-25 False
Length: 584, dtype: bool
quotesstock['volatility'].shift(-1, axis = 0) <= quotesstock['vix'].shift(-1, axis = 0)
Out[159]:
Date
2019-12-31 False
2020-01-02 False
2020-01-03 False
2020-01-06 False
2020-01-07 False
2022-04-19 True
2022-04-20 True
2022-04-21 True
2022-04-22 True
2022-04-25 False
Length: 584, dtype: bool
but
quotesstock['crossup'] = (quotesstock['volatility']*5.5 > quotesstock['vix'] & quotesstock['volatility'].shift(-1, axis = 0) <= quotesstock['vix'].shift(-1, axis = 0))
Traceback (most recent call last):
File "D:\MachineLearning\Anaconda\lib\site-packages\pandas\core\ops\array_ops.py", line 302, in na_logical_op
result = op(x, y)
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
CodePudding user response:
This is due to Operator precedence, &
is more binding (more sticky) than <
, <=
, >
, >=
, !=
, ==
so for example
1.5 < 2.5 & True
is understood as
1.5 < (2.5 & True)
which cause
TypeError: unsupported operand type(s) for &: 'float' and 'bool'
you need to use brackets to get <
comparison evaluted first, in above example that mean you need to do
(1.5 < 2.5) & True
in order to get True