Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Is it more computationally efficient to manipulate data in the pipeline or as a dataframe?

Let's take a simple example like the Piotroski F score. The first on that list is the change in Return on Assets. I can get this years ROA and last years ROA, and find the difference. But the F score requires a 0 or a 1, somewhat similar to this pseudocode:

if thisyearROA > lastyearROA:  
    value  =  1  
else:  
    value = 0  

Two questions: 1. is it more efficient to do this before the dataframe is created?
2. if it is more efficient to do this before the dataframe is created, how do I go about this? Simple if statements never seem to work

2 responses

I ask myself the same question regularly. The answer to this depends on what type of dataframe you are creating, that is, are you creating a time series, cross section, panel. My dataframes tend to become cross sectional that look like this usually:

date ticker var1 var2 var2

In this case, I would think it would be easier to do the computation before the dataframe is created, using the class within the pipeline. If you wanted to do this in the dataframe, you would need to add all those past ROAs to the dataframe. Instead, if you do it in the pipeline you can do that ahead of time and keep the dataframe simple.

The answer provided by @Niccola Tartaglia is good advice. Generally, do computations in pipeline when possible.

However, it's not always a clear cut answer. For simple arithmetic and boolean logic between factors, pipeline uses numexpr, which is very fast, faster than pandas or numpy manipulation of dataframe columns. However, if one is applying a very selective screen to your pipeline, there may be much less data to operate on after the pipeline is run. In this case, even though the computations may be slower, the total time may be faster (by operating on less data).

Logic inside custom factors is less clear cut. If one executes identical numpy calculations inside a custom factor vs after the pipeline is returned, the speed will probably be comparable. Again, it depends upon how much data is being operated on so pay attention to masks and screens.

Pipeline is typically more memory efficient too. If one is outputting a number of columns just to combine them later in pandas, you'll save memory by doing the work in pipeline. Additionally, it's easy to unintentionally end up with copies of the output dataframe when doing calculations after a pipeline is returned.

So, generally, if you can do something easily in pipeline, it's probably faster and/or more memory efficient to do it there.

Now, the second half to the original question was "how do I go about this? Simple if statements never seem to work" . True, in defining calculations for pipeline, 'if statements' are not defined. Often one doesn't need them however. Maybe take a look at this post for some ideas on how to implement Piotroski F score (https://www.quantopian.com/posts/piotroskis-f-score-algorithm )

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.