Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Another Newbie Question - At least it's simple

I'm simply trying to determine if it was a down-day or up-day using an IF statement and I'm getting the error
"ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all()."

I understand this is because pandas is mad at me for trying to convert something to a bool but I'm too inexperienced to figure out how to fix it.

Would someone mind helping me?

spy_daily = get_pricing(  
    'SPY',  
    start_date='2017-01-01',  
    end_date = '2018-05-11',  
    frequency='daily',  
)

if spy_daily.open_price < spy_daily.close_price:  
    {my operation}  
else:  
    {another operation}  
2 responses

The 'get_pricing' method returns a pandas dataframe (in this case). The statement 'spy_daily.open_price' takes the single 'open_price' column from that dataframe and returns it as a pandas series (indexed by date). The if statement doesn't now which date you want to compare and therefore returns the error 'The truth value of an array is ambiguous'. To fix this, one just needs to specify the specific dates to compare. This can be done like this.

if spy_daily.close_price['2018-05-11'] > spy_daily.open_price['2018-05-11']:  
    print 'SPY up for the day'  
else:  
    print 'SPY is down'

Good luck.

Here's a notebook with the above code in action...