0
class DataFrameAnnotation:

  def __init__(self, df: pd.DataFrame):
    self.df = df

  def transformation_1(self):
    self.df + 1 

  def transformation_2(self):
    self.df + 1 

  def main(self):
    self.transformation_1()
    self.transformation_2()
    ....
    return self.df

My question is if there are any issues keeping a large dataframe in an object's state like the above compared to passing the dataframe around as a parameter:

   def main(self, df):
        df = self.transformation_1(df)
        df = self.transformation_2(df)
        ....
        return df
2

Since the dataframe isn't actually being copied but only referenced, you don't have an inefficiency issue. But, you do have a coupling issue depending on the design of your system.

If the class holding the dataframe object is the responsible entity, it's a plausible choice. However, if you have more than one object that holds a reference to this object, you have a tight coupling between this objects (along the lines; how object A can track changes of the shared object with manipulations that object B invoked).

In order to reduce this coupling you can either ask yourself if there's a distinction between object A to B (and if they really must be separated by design), or if object B can remove its' ownership of the object as a member and expose a well-defined API (with the object as parameter) instead.

Either way, you should strive for that behavior since a two (or more) separated classes manipulating the same data object via an ill-defined API can be hard to maintain in the long run.

0

DataFrameAnnotation has no state besides that pandas dataframe. Since self.df seems public DataFrameAnnotation is not encapsulating operations on the dataframe. I see no reason for that class to exist. Why are the transformation_ methods not module level functions?

p.s: that code is not valid python

  • that's not the only attribute for the class, so there is state besides the df. and also I really don't like module level functions. I know it's acceptable in python (maybe even recommended), but I prefer them to be static methods in a class where possible. I find it more readable instead of opening a file with various functions in it. – Claudiu Creanga Feb 6 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.