Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas pipe throws error that df to be passed as an argument

Pandas pipe throws error that df to be passed as argument

Ideally pipe should take the dataframe as argument by default which is not happening in my case.

class Summary:
def get_src_base_df(self):
    <do stuff>
    return df
    
@staticmethod
def sum_agg(df):
    cols = 'FREQUENCY_ID|^FLAG_'
    df = (df.filter(regex=cols).fillna(0)
          .groupby('FREQUENCY_ID').agg(lambda x: x.astype(int).sum()))
    return df

# few other @static methods

def get_src_df(self):
    df = self.get_src_base_df().pipe(self.sum_agg()) #pipe chain continues  
    # --> error: sum_agg() missing 1 required positional argument: 'df'
    # but the below line works
    # df = self.get_src_base_df().pipe((lambda x: self.sum_agg(x))) #pipe chain continues


   

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By doing self.sum_agg(), you’re calling the sum_agg function (@staticmethods in Python are pretty much indistinguishable from functions), and since it doesn’t have an argument right there in that call, it rightfully fails. You need to pass the function object, not the value returned by the function.

Do this, instead :

def get_src_df(self):
    df = self.get_src_base_df().pipe(self.sum_agg)  # note: no parentheses
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading