Python Forum
IF statement to apply at each date - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: IF statement to apply at each date (/thread-32855.html)



IF statement to apply at each date - illmattic - Mar-10-2021

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


RE: IF statement to apply at each date - deanhystad - Mar-11-2021

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.


RE: IF statement to apply at each date - illmattic - Apr-08-2021

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