Could you please advice how to change all asset objects ("Equity(24 [AAPL]...") to strings ("AAPL"...) for all assets in dataframe returned from run_pipeline ?
Could you please advice how to change all asset objects ("Equity(24 [AAPL]...") to strings ("AAPL"...) for all assets in dataframe returned from run_pipeline ?
As noted, the dataframe returned by a pipeline is indexed by the asset objects. It's best to leave the index as is so it can be manipulated properly with the various Quantopian methods which assume the index are asset objects. However, if one want's to view or manipulate the dataframe by the asset symbol, or name, or some other asset attribute, the best thing to do is add a column.
First, what attributes does an asset object have? The documentation has the list complete with descriptions etc but below are the attribute names
To get the ticker, or symbol, of an individual asset then one needs to reference the symbol attribute. Something like this
aapl_object = symbols('AAPL')
aapl_symbol = aapl_object.symbol
To get the symbols for all the assets in a dataframe, one first needs to reference the assets. Since the assets are one of the indexes of the dataframe the best way to reference them is with the get_level_values method (see https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.Index.get_level_values.html). The key is to know which index level the asset is. In a notebook, a pipeline returns a multi-index dataframe which has the assets as level 1. In an algo, a pipeline returns a single index dataframe with assets as level=0. So, the code below will fetch the index values in a notebook and algo pipeline respectively
# assume notebook_df and algo_df are the dataframes returned by a pipeline
# the assets can be fetched using get_level_values
notebook_assets = notebook_df.index.get_level_values(level=1)
algo_assets = algo_df.index.get_level_values(level=0)
Once one has the asset values simply reference the symbol attributes and make a new column. Several ways to do this. Either of the following will work. This is an example for a notebook.
# assume notebook_df is the dataframe returned by a pipeline
# one approach is to use the pandas `map` method
notebook_df['symbol'] = notebook_df.index.get_level_values(level=1).map(lambda asset: asset.symbol)
# another approach is to make a list using python's list comprehension
assets = notebook_df.index.get_level_values(level=1)
notebook_df['symbol'] = [asset.symbol for asset in assets]
So far this is straightforward. However, what happens when a company changes their stock symbol? The symbol attribute is the current stock symbol. It's independent of the pipeline date. If a stock had a different symbol in the past this won't be reflected in the dataframe column as created above. To get the symbol as of the pipeline date, then use the Morningstar fundamentals symbol data field in a pipeline definition.
from quantopian.pipeline.data.morningstar import Fundamentals
# Create a classifier for the historical stock symbol as of the pipeline date
symbol = Fundamentals.symbol.latest
Check out the attached notebook for all this in action.
See the attached notebook for these in action.
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.