Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Beginner: Trying to filter grades on pipeline

Hello,

I have built a basic pipeline and have tried a bunch of ways to filter results for only specific fundamental grades (in my notebook, just looking to simply make Growth Grade = 'A'). Have tried to do something simple like query at the end, but cannot figure out why it won't work (I commented the Growth grade code towards the bottom). Once that piece of the code becomes live, I get a bool error when I run it. Is there an easy way to modify this? Thanks

6 responses

Welcome!

The issue is the pandas query method doesn't support spaces (or other special characters) in column names. See this github post https://github.com/pandas-dev/pandas/issues/6508 ). So the following will work.

results = results.rename(columns={'Growth Grade': 'Growth_Grade'})  
a_results = results.query("Growth_Grade == 'A'")

Good luck

Disclaimer

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.

Perfect! Thanks

Glad that worked. I also like using the 'in' operator especially in cases where one may want to compare several values. Like this:

a_results = results.query("Growth_Grade in ['A']")  
a_b_results = results.query("Growth_Grade in ['A', 'B']")

Note that categoricals (like Growth Grade) can be compared within the pipeline using the element_of method. See https://www.quantopian.com/help#quantopian_pipeline_classifiers_Classifier . So this would do something similar to the above but in the pipeline definition. The result is a filter object.

gs = Fundamentals.growth_score.latest  
gs_a_or_b = gs.element_of(['A', 'B'])

Thanks again Dan! Yeah I'm definitely going to use the one with the 'in' factor.

Is it possible to get the previous growth_grade? I tried the following CustomFactor and get the error
"ValueError: setting an array element with a sequence at the very end of running the pipeline

class Previous(CustomFactor):
# Returns value of input x trading days ago where x is the window_length
# Both the inputs and window_length must be specified as there are no defaults

def compute(self, today, assets, out, inputs):  
    out[:] = inputs[0]

growth_grade_1 = Previous(inputs = [morningstar.asset_classification.growth_grade], window_length = 63)

Apologies in advance i am not a very good python coder

ValueError: setting an array element with a sequence error