Skip to content

Easier subclassing #60

Closed
Closed
@yonatanf

Description

@yonatanf

Hi,

Currently, sub-classing pandas objects is not as easy as it could be.
This is the result of many methods explicitly creating specific classes. E.g. the '_combine_const' method from 'matrix' explicitly returns a DataMatrix object:
def _combine_const(self, other, func):
if not self:
return self

    # TODO: deal with objects
    return DataMatrix(func(self.values, other), index=self.index,
                      columns=self.columns)

Therefore, arithmetic operations (and some other operations) performed on a new class MyDataMatrix, inheriting form DataMatrix, return a DataMatrix object, rather than a MyDataMatrix object.

I can get around the problem using the ugly hack of overriding the problem methods, and forcing the output to the new class. E.g.,
def _combine_const(self, other, func):
temp = super(MyDataMatrix, self)._combine_const(other, func)
temp.class = self.class
return temp
However, I feel it would be easier to just change the original methods to return the class of the calling object, rather than a fixed class. E.g.,
def _combine_const(self, other, func):
if not self:
return self

    # TODO: deal with objects
    return self.__class__(func(self.values, other), index=self.index,
                      columns=self.columns)

Hope this makes sense, and I'm not missing something.
Thanks for a very useful package!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions