engarde-dev / engarde

A library for defensive data analysis.
MIT License
500 stars 40 forks source link

Support decorators for class's methods #32

Closed eyaltrabelsi closed 7 years ago

eyaltrabelsi commented 7 years ago

Hi , I Have many class which do broad use of panda and they can benefit from your solution for defensive programing. I know i can use it in pipe syntax but i think the decorator version is cleaner.

thx

TomAugspurger commented 7 years ago

Can you give an example script showing what you want to be able to do?

eyaltrabelsi commented 7 years ago

something like this:

import pandas as pd from engarde.decorators import none_missing

class c1():
    def __init__(self):
        pass

    @none_missing
    def f(self, df2):
        return df2

c = c1()
c.f(pd.DataFrame(d={'one': [1., 2., 3., 4.],
                    'two': [4., 3., 2., 1.]}))
TomAugspurger commented 7 years ago

That's what I thought. Can you verify that this works for you

import numpy as np
import pandas as pd
import engarde.decorators as ed

class DataPipeline:

    @ed.none_missing()
    def multiply(self, df, n=2):
        return df * n

if __name__ == '__main__':
    df = pd.DataFrame({"A": [1, np.nan, 2]})
    cls = DataPipeline()
    print(cls.multiply(df))

You'll need the parenthesis on none_missing. At some point I'd like to make the decorators very forgiving and accept either @none_missing or @none_missing, but haven't had the time.

eyaltrabelsi commented 7 years ago

Cool :) thank you very much