Whenever i try to perform any operation with the results dataframe, i.e, the result returned by my pipeline function it shows an error 'unhashable type'. That is why i am unable to use the results for visualization or for any operation.
Whenever i try to perform any operation with the results dataframe, i.e, the result returned by my pipeline function it shows an error 'unhashable type'. That is why i am unable to use the results for visualization or for any operation.
First off, pipelines when run in a notebook, return a multi-index dataframe. The level_0 index are dates. The level_1 index are the security objects. The columns are whatever factors were defined in the pipeline definition.
In an algo (ie the IDE environment), a pipeline returns only a single indexed dataframe. The index is the securities. There is no date index. During an algo simulation, pipeline only returns data for the current simulation day. Therefore, the date isn't included in the index and is implied to be the simulation date. In a notebook however, a pipeline returns data for multiple dates. Each date is the dataframe which would be returned for that date in an algo. One can think of a notebook pipeline dataframe as multiple algo dataframes 'stacked' up. One for each date.
So, how to manipulate this multi-index dataframe? First, generally do not reset the index. Leave it alone.
If one wants to look at a specific date use the pandas xs method. This will return a single index slice of the dataframe just for one date. This is convenient because the result will be identical to the dataframe returned by a pipeline in the IDE. One can manipulate the dataframe the same as one would do in an algo. The following will return a single index (ie 1 dimension) slice of the dataframe for the date '2017-01-03'.
results.xs('2017-01-03')
If one wants to analyze, or look at, a single security over many dates use the same xs method but specify the security and level=1 (remember the level_1 index contains the securities). Again, the result will be a single indexed dataframe.
results.xs(symbols('ARNC')
For more complex filtering of dates and securities I like using the query method. One can use index names in the query to make selection easier. Here we want to get data for two securities ('ARNC', 'AKRX') for a single date ('2017-01-03').
selected_securities = symbols(['ARNC', 'AKRX'])
results.query("date == '2017-01-03' and security in @selected_securities")
Many of the pandas methods expect values to be presented as rows and columns. For example, to plot 'Daily Return' using the plot method, pandas expects dates to be the index and the securities to be columns. The unstack method will do the trick and turn one of the multi-indexes into columns. Think of it as taking a tall 'stack' of securities and shortening it (and making the columns wider). Below will plot the 'Daily Return' for our two stocks 'ARNC' and 'AKRX'.
results.query("security in @selected_securities")['Daily Return'].unstack().plot()
The attached notebook goes into more detail and highlights some other methods such as groupby and nlargest.
A final note, the compute functions in the custom factors use for loops. Generally avoid using loops in calculations. It's both slow and often makes for difficult reading. Use the numpy vector calculations instead. Most calculations can be done on numpy arrays directly without the need for loops.
Hope that helps get around some of the errors trying to manipulate the dataframe in the initial notebook.
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.