Subclassing Pandas, modifying self

Viewed 86

Assume you have a DataFrame object with some multi row and column data in it. You can then easily assign this onto itself to modify the object. For example:

test = test.iloc[0:10,:]

will only retain the first 10 rows of the original dataset.

I am trying to replicate this behaviour when I subclass Pandas with multiple inheritance. For example:

class data(pd.DataFrame, time, series):
    
    def __init__(self, *args, **kwargs):
        super(data, self).__init__(*args, **kwargs)

    @property
    def _constructor(self):
        return data

where time and series are classes that provide additional methods. Here, only series is relevant:

class series(object):

    def __init__(self):
        super(series, self).__init__(*args, **kwargs)

    def keep10rows(self):
        self = self.iloc[0:10,:]
        return self

Now, I create a new class instance:

test = data()

now we can fill test with some data. If I apply:

result = test.keep10rows()

Here, result will look correct. However, test.data has not changed. Why? How can I achieve a change of the data inside the object?

0 Answers
Related