Back to Community
KeyError when I try to get column data from pipeline output

I am trying to get data out of a column from my pipeline output.
Please correct me if I'm wrong. The key to the pipeline output dataframe is an Equity. So I got the indexes from the output dataframe, and iterated through it. Each iteration would use the Equity from the iteration to get column data from the output dataframe. However, I get a "KeyError" when I try to do so.

KeyError: Equity(21383, symbol=u'EXEL', asset_name=u'EXELIXIS INC', exchange=u'NASDAQ', start_date=Timestamp('2002-01-01 00:00:00+0000', tz='UTC'), end_date=Timestamp('2017-01-17 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=Timestamp('2017-01-20 00:00:00+0000', tz='UTC'), exchange_full=u'NASDAQ GLOBAL SELECT MARKET')  

I couldn't get my code to syntax properly on this post, so I put it on pastebin.

2 responses

Got the same problem please help

There are several ways to get specific values from a pandas dataframe (ie the data returned from a pipeline). Ishwar M suggested the flexible '.loc' method. This is nice because you can get one or many values. If you want a single value, I like using the '.get_value' method ( ). So your if statement could look like this.

if context.output.get_value(index, 'upFilter') == True:  

However, something to consider... the use of loops in Python is often not required and is typically slow and generally considered not 'pythonic'. Many programmers with backgrounds in more procedural languages such as C or Java use this approach but Python (especially when using the numpy and pandas libraries) allows for much more elegant solutions. Maybe consider changing your code

# These are the securities that we are interested in trading each day.  
    context.security_list = context.output.index  
    i = 0  
    for index in context.security_list:  
        if context.output[index]['upFilter'] == True:  
            i = i+1  
     print i  

To this single line

    i = context.output.query('upFilter == True').upFilter.count()  
    print i