Python Forum
IF statement to apply at each date
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IF statement to apply at each date
#1
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
Reply
#2
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.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 220 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 589 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format and past date check function Turtle 5 4,235 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,229 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,857 Sep-03-2020, 02:25 PM
Last Post: NG0824
  How to apply VLookup formula jonzee 2 3,539 Jan-12-2020, 04:16 PM
Last Post: Clunk_Head
  Issue in .apply function fullstop 0 1,496 Dec-17-2019, 01:29 PM
Last Post: fullstop
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,126 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt
  Apply a function with pandas Jompie96 2 2,224 Jun-13-2019, 07:04 PM
Last Post: Jompie96
  How to change existing date to current date in a filename? shankar455 1 2,289 Apr-17-2019, 01:53 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020