Skip to content
Advertisement

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). & [duplicate]

I have a dataframe ‘signal’:

    coresym open_orders direction
27  EURUSD  2.0         1.0
76  XAUUSD  2.0         9.0

As I need some condition to set up my ‘_exec_dict’ which is a dictionary to tell the trading platform what my order is?

for symbol in do_symbols:
    #close positon
    if ((signal['coresym']==symbol) & (signal['direction'] == 9)):
        _exec_dict['_action'] = 'CLOSE'
        _exec_dict['_symbol'] = symbol
        _exec_dict['_magic'] = symbols_magic_dic[symbol]
        _ret = self._execution._execute_(_exec_dict)
        print('{} is traded'.format(symbol))

The problem is I cannot use

if ((signal['coresym']==symbol) & (signal['direction'] == 9)):

to do the condition determination.

To do the testing:

if((signal['coresym']=='XAUUSD') & (signal['direction'] == 9)):
    print('ok')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-f0dcc93290aa> in <module>
----> 1 if((signal['coresym']=='XAUUSD') & (signal['direction'] == 9)):
      2     print('ok')

~Anaconda3libsite-packagespandascoregeneric.py in __nonzero__(self)
   1476 
   1477     def __nonzero__(self):
-> 1478         raise ValueError(
   1479             f"The truth value of a {type(self).__name__} is ambiguous. "
   1480             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

to have some alternation,

if signal[signal['coresym'] == 'XAUUSD']['direction'] == 9:
       print('ok')

the result is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-49-67440d7a0b81> in <module>
----> 1 if signal[signal['coresym'] == 'XAUUSD']['direction'] == 9:
      2     print('ok')

~Anaconda3libsite-packagespandascoregeneric.py in __nonzero__(self)
   1476 
   1477     def __nonzero__(self):
-> 1478         raise ValueError(
   1479             f"The truth value of a {type(self).__name__} is ambiguous. "
   1480             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

After searching, like Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() I do use the ‘&’.

Advertisement

Answer

The & is not the problem.

if((signal['coresym']=='XAUUSD') & (signal['direction'] == 9)): print('ok')

do you nean to say if all the elements of signal['coresym'] are equal to XAUUSD and all the elements of signal['direction'] are =9.

Or do you mean if any of them.

This is the ambiguity that the error describes.

use all() or any() around your predcats. for example:

if((signal['coresym']=='XAUUSD').all() & (signal['direction'] == 9).all()): print('ok')

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement