0

I'm sure there's probably a simple solution but anyway the problem is this: I've got two classes, say A and B, both of which have attributes that are dataframe like -- those attributes are instances of a dataframe class, call it C, which has its own methods. I'd like to define an 'interface like' class D which has methods that can operate on those attributes (i.e. operate on the dataframes which are attributes of A and B).

edit for clarity: in what follows below, let a and b be dataframes (i.e. instances) from class C. So that the methods of C are available to a and b.

To be more explicit: Suppose a is the dataframe like attribute of A, with attributes Series1,...,Seriesn. Since a is dataframe like, I can call a.Series1, a.Series2, ... etc to access the contents of Series1, Series2 in a. Of course a is an attribute of A so I'm actually calling A.a.Series1, A.a.Series2.. etc, and a has its own methods from class C so I can call A.a.Series1.methodfromclassC() no problem. Anyhow. Now suppose I want to make a transformation in a consistent fashion to the contents of a.Seriesj, or b.Seriesj, implemented as a method in class D, that both A and B can access. The idea being that I'd like to be able to call a member of class A like so: A.a.Seriesj.transformseries(). The problem I run into is that Seriesj has its own methods (inherited from class C) and transformseries() is not one of them.

This probably seems a bit convoluted but the idea being that eventually I can chain multiple calls to the various methods of D changing the state of the dataframe attributes: A.a.Series2.transform1().transform2().transformj() or B.b.Seriesj.transform6().transform3() etc, so that the final representation of A.a and B.b is in the form that I'd like.

9
  • 1
    The explanation it's not clear to me. About the last sentences I think it's enough to return self to obtain that kind of thing. Commented Apr 22, 2013 at 15:03
  • You lose me here: If a inherits from C it doesn't necessarily follow that a.Series1 inherits from C as well. Did you think it should, or is part of the explanation missing? Commented Apr 22, 2013 at 15:07
  • kojiro: Sorry for the ambiguity, yes a inherits from C and a.Series1 inherits from C as well. I'll edit the post for clarity. Thanks Commented Apr 22, 2013 at 15:09
  • Can you explain how this is OOP? It seems like AOP to me. Commented Apr 22, 2013 at 15:15
  • kojiro: I didn't really think it was controversial to include the oo tag for the post, since my question deals with objects, classes, etc. I'm not familiar with AOP so I'll look into it. Commented Apr 22, 2013 at 15:33

1 Answer 1

1

Have you considered "injecting" new methods to your Seriesn? In Python you can add methods to classes dynamically like this:

setattr(MyClass, 'new_method', lambda self: 'return value')

It will work even in objects instanced before. So you could add custom methods to Pandas' Series and/or Dataframe classes.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.