Python Forum

Full Version: IF statement to apply at each date
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am trying to write a function that includes an IF statement that looks at two columns in the dataframe to highlight the dates when the 'value' is higher than the 'trend':

def arrow(trend, close):
    signal = []
    for date, value in close.iteritems():
        if value > trend: 
            signal.append(value*1.05)
            
        else:
            signal.append(np.nan)
    return signal
but I keep getting this message:
Error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I tried using the suggestions provided by the error but can't get it to work.

Any help is appreciated.
Matt
What is value? What is trend?

I would modify the code to do this:
def arrow(trend, close):
    print('arrow', trend)
    signal = []
    for date, value in close.iteritems():
        print('value', value)
        if value > trend: 
            signal.append(value*1.05)
             
        else:
            signal.append(np.nan)
    return signal
Either trend or value will print out as a list or tuple. Once you know which, you can modify your code to get two numbers you can compare.
sorry for the delay. I forgot about this. I figured it out doing this:

def arrow(data): 
        signal = []
        x = data['Close'] > data['Trend']
        
        for row in x:             
            if row is True:
                  signal.append(x['Close']*1.05))   
            elif row is False:
                signal.append(np.nan)
    
        return signal