Search code examples
pythonpandasdataframeseriesambiguous

Working with pandas dataframe and Combining conditions gives ambiguity error


I am trying to calculate the candle stick pattern called Doji. It requires two calculation of two conditions values is a pandas dataframe with the historical stock data with columns Date, High, Low, open and Close.

With the if condition I tried to explicitly make condition1 and condition2 bool and also tried it by typecasting it with any(). Both of them did not give the desired result.Printing condition1 and condition 2 separately give appropriate boolean value but combining it with '&' goes horribly wrong.

51315    True
51316    True
51317    True
51318    True
51319    True
         ... 
53790    True
53791    True
53792    True
53793    True
53794    True
Length: 2480, dtype: bool

ValueError                                Traceback (most recent call last)
<ipython-input-58-3f42eed169f4> in <module>
      4 values = pd.DataFrame(stocks_data.loc[stocks_data['Company']=='TCS'])
      5 values.reset_index()
----> 6 study_candlesticks(values)

<ipython-input-57-fd67b4117699> in study_candlesticks(values)
     21 #     for row in values
     22 
---> 23     if calc_doji(values):
     24         values['Pattern']='Doji'
     25 

<ipython-input-57-fd67b4117699> in calc_doji(values)
     81     condition2=((values['High'] - values['Low'])>values['Close']*min_candle_size)
     82     print(condition2)
---> 83     if ((condition1).bool()&(condition2).any()):
     84         return True
     85     else:

~\Anaconda3\lib\site-packages\pandas\core\generic.py in bool(self)
   1581             )
   1582 
-> 1583         self.__nonzero__()
   1584 
   1585     def __abs__(self):

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1553             "The truth value of a {0} is ambiguous. "
   1554             "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
-> 1555                 self.__class__.__name__
   1556             )
   1557         )

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

I am not sure where I am going wrong. Any suggestions?

Below is the code

calc_doji(values)

def calc_doji(values):
    max_candle_size=0.1/100
    min_candle_size=1/100
    condition1 =(abs(values['Open'] - values['Close'])<values['Close']*max_candle_size)
    print(condition1)
    condition2=((values['High'] - values['Low'])>values['Close']*min_candle_size)
    print(condition2)
    if ((condition1).bool()&(condition2).any()): 
        return True
    else:
        return False

Solution

  • If you have two pd.Series, where dtype('bool'). You can compare them in the following way. Without knowing what your data looks like, I've created two pd.Series with either True or False.

    import pandas as pd
    import numpy as np
    
    condition1= pd.Series(np.random.choice([True, False], 100)) 
    condition2= pd.Series(np.random.choice([True, False], 100)) 
    

    Then you can compare by doing the following.

    (condition1) & (condition2) # which returns a `pd.Series` where each row is either `True` or `False`.
    

    To find any index position from each pd.Series where both values are True.

    ((condition1) & (condition2)).any() # Which returns either `True` or `False`
    

    From your code, I would guess this line is the issue.

    if ((condition1).bool()&(condition2).any()):
    

    which should be

    if ((condition1) & (condition2)).any():