0

So, I really want to add on a few new methods to pandas.core.frame.DataFrames. For example, I want a method called .idx:

pandas.core.frame.DataFrame.idx(self, rows, cols):
    return self.iloc[rows].loc[cols]

Is this possible? I'm not sure what the syntax should be - the class already exists from a library, so I can't just put the function inside the class definition.

MeetTitan
  • 3,383
  • 1
  • 13
  • 26
Hillary Sanders
  • 5,778
  • 10
  • 33
  • 50

2 Answers2

2

Could you not extend your DataFrame, inheriting all of its functions, and having the freedom to then define your own, on top?

In python it would look something like:

class ExtendedDataFrame(DataFrame):
    ...

Then, simply use an instance of ExtendedDataFrame, instead of DataFrame, for your extra goodies.

I also suggest taking a look at this tutorial covering inheritance.

Also be sure to check out your version of python's implementation of super(), if you're trying to override functions of the super (parent) class.

Here is a SO question with great information regarding super(), because I'm noticing the tutorial I linked only briefly covers super(), and in the comments no less.

Community
  • 1
  • 1
MeetTitan
  • 3,383
  • 1
  • 13
  • 26
1

This is actually really easy in Python. While I would recommend inheriting from DataFrame, as MeetTitan answered, sometimes that won't work, but you can just add on functions like this:

class Abc(object):
    pass

def new_funct(self):
    print 1234

Abc.instance_funct = new_funct
a = Abc()
a.instance_funct()
user3757614
  • 1,776
  • 12
  • 10