Skip to content Skip to sidebar Skip to footer

Build Stand-alone But Composeable "atomic" Filter Functions For A Sql Database/pandas Dataframe?

Okay, I asked this: Functional chaining / composing filter functions of DataFrame in Python? and it was erroneously marked duplicate. So we're trying again: What I have is a bunch

Solution 1:

So, using hints from this: How to redirect all methods of a contained class in Python? I think I can do it:

deftocontainer(func):
defwrapper(*args, **kwargs):
    result = func(*args, **kwargs)
    return Container(result)
return wrapper

classContainer(object):
    def__init__(self, df):
        self.contained = df

    def__str__(self):
        display(self.contained)

    def__getitem__(self, item):
        result = self.contained[item]
        ifisinstance(result, type(self.contained)):
           result = Container(result)
        return result

    def__getattr__(self, item):
        result = getattr(self.contained, item)
        ifcallable(result):
            result = tocontainer(result)
        return result

    def__repr__(self):
        display(self.contained)

    defmax_price(self, cost):
        return Container(self.contained[self.contained.price < cost])

    defis_shirt(self):
        return Container(self.contained[self.contained.is_shirt == True])

    def_repr_html_(self):
        return self.contained._repr_html_()

so I can do things like:

my_data = pd.read_csv('my_data.csv')
my_clothes = Container(my_data)
cheap_shirts = my_clothes.is_shirt().max_price(20)

which is exactly what I wanted. Note the necessary calls to wrap the contained dataframe back up in to the container class for each simple filter function. This may be bad for memory reasons, but it's the best solution I can think of so far.

I'm sure I'll run into some of the caveats mentioned in the above-linked SO answer, but this will work for now. I see many variations on this question (but not quite the same), so I hope this helps someone.

ADDED BONUS: Took me awhile to figure out how to get the data frames of a composed class to look nice in iPython, but the _repr_html_ function does the trick (note the single, not double, underscore).

Post a Comment for "Build Stand-alone But Composeable "atomic" Filter Functions For A Sql Database/pandas Dataframe?"