Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Analyzing Pipeline Data from Research notebook - How to get data using Equity object?

Hi there,

Say if I have a data that I got from running a pipeline i.e.

my_pipe = make_pipeline()  
data = run_pipeline(my_pipe, '2014-05-05', '2015-05-05')  

How do I get, for example, data for only AAPL equity? In Pipeline result, looks like equities are zipline.assets._assets.Equity objects and we are not allowed to run from zipline.assets._assets import Equity. How else do I create Equity object?

15 responses

you have 2 levels that are not columns. Convert at least one of them.
Then access as normal. Here is how I did it.

result.reset_index(inplace =True)  
result.rename(columns ={ 'level_0': 'Current Day',  
                        'level_1': 'sid'  

Where 'Ps' is price to sales ratio

for i in xrange(0,len(result)):  
    if result.sid[i].symbol == u'AAPL':  
        print 'AAPL',result['Ps'][i]  

Thank you for replying to this thread.

Unfortunately, I cannot use for iteration nor == operator here. I need to be able to get directly from pandas. See following code:

eq1 = # How do I create Equity object here?  
df = pd.DataFrame(data.ix[:, 'close_price'].xs(eq1, level=1))  

Any idea?


this will give you Equity object for SPY

p = get_pricing(['SPY','TLT'])  
SPY = p.axes[2][0]  

But I cannot use that equity object for selection i.e. result[result.index == SPY]. Any idea?

@Jay Teguh Wijaya
You should be able to select and compare to an equity object in the manner posted above. There are better ways but that should work.

The problem you may be having is that both the 'get_pricing' and 'run_pipeline' methods return dataframes (or panels) sometimes with a multi-index and sometimes not.

The key is understanding what type of object 'result' is, how it's indexed, and how many dimensions it has. This varies depending upon if using 'get_pricing' or 'run_pipeline' and then also on what parameters are passed (see https://www.quantopian.com/help#quantopian_research_get_pricing and https://www.quantopian.com/help#quantopian_research_run_pipeline )

Could you be more explicit what 'result' is and what 'SPY' is. Maybe post a notebook?

Thanks for replying, @Dan Whitnable

Please see attached for the example notebook.

A lot of ways to get to data for a specific security. The output of run_pipeline is a multi-indexed dataframe. The major index are the dates. Again, many ways to do this but I like the '.xs' method to extract a single days slice.

result_today = result.xs(today)

Now, select a particular security and data column (remember that the pipeline can return many columns of data). I like using the 'at' method. The 'loc' method is perhaps more ubiquitous though.

# Use the symbols method to get a security object when the symbol is known  
arnc_object = symbols('ARNC')

# Use the 'at' method to get a single value from a dataframe.  
# The first parameter is the index, the second is the column name  
result_today.at[arnc_object, 'exchange_id']

See the attached notebook.

That's awesome! Thank you @Dan ! I have not used multi-index dataframes much so I did not know the xs trick.

I ran into the same issue. When running a pipeline that brings back several equities over multiple days (multi index dataframe), I perform the following steps:

# get a df from my pipeline  
result= run_pipeline(make_pipeline(), '2017-01-03','2017-04-03')

# name the first two columns for easier identification  
result.index.names = ['dates','equities']

# retrieve the slice of data for a specific equity over the entire date range  

#alternatively retrieve all equities for a specific date  

I hope this is useful to others.

Thanks Mitch - this helped!


how could I select a stock, when I am working with international stocks. I mean, the solution to work with USA equities would be as follows:

# retrieve the slice of data for a specific equity over the entire date range  

But for international stocks we don't have the symbols('stock') method. For example, to select the Brazilian equity Equity(1178909286093910 [PEAB4]), how could I select it. I attached a notebook.


To select a specific security, use the SID (without quotes) instead of the ticker. This works for all equities including US. Something like this:

peab4 = symbols(1178909286093910)

# retrieve the slice of data for a specific equity over the entire date range  
# for international symbols (works with US equities too) use the SID  
result.xs(symbols(1178909286093910), level='equities')

You may also want to look at this post for more info on referencing international equities https://www.quantopian.com/posts/identifying-the-ticker-symbols-for-global-equity-indian-market .

Good question.


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

It works!

Thank you so much Dan, you helped me a lot.


In Jay's initial post, I noticed an interesting customfactor:

class First(CustomFactor):  
    """Direct opposite of Latest factor.  
    # Pre-declare inputs and window_length  
    inputs = [USEquityPricing.close, ]  
    window_length = 1  
    def compute(self, today, assets, out, close):  
        x=close[0, :]  
        out[:] = x  

I am wondering if there is a better way than the one I coded in Notebook attached to print past values of a factor into a pipeline.

Also, how we can add a parameter called 'length' into Histo_SMA_20. I tried to use 'params' as recommended here https://www.quantopian.com/posts/adding-my-own-parameters-to-customfactor but failed to do so.
The idea is window_length = length+X with params=('length') in customfactor
In my example, window_length=25 and X=5.