Subclasses of pandas' object work differently than subclass of another object?

I am trying to subclass a Panda data structure in order to replace a subclass with a subclass in my code , I do not understand why this sample code does not work. dict Series

from pandas import Series    

class Support(Series):
    def supportMethod1(self):
        print 'I am support method 1'       
    def supportMethod2(self):
        print 'I am support method 2'

class Compute(object):
    supp=None        
    def test(self):
        self.supp()  

class Config(object):
    supp=None        
    @classmethod
    def initializeConfig(cls):
        cls.supp=Support()
    @classmethod
    def setConfig1(cls):
        Compute.supp=cls.supp.supportMethod1
    @classmethod
    def setConfig2(cls):
        Compute.supp=cls.supp.supportMethod2            

Config.initializeConfig()

Config.setConfig1()    
c1=Compute()
c1.test()

Config.setConfig2()    
c1.test()

Perhaps this is not the best way to change the configuration of some objects, anyway, I found this useful in my code, and most of all I want to understand why it works with dict instead of a series , as I expect.

Thank you so much!

+5
source share
2 answers

Current Answer (Pandas> = 0.13)

Pandas 0.13 . Pandas Series , Python:

class MySeries(pd.Series):
    def my_method(self):
        return "my_method"

Legacy Answer (Pandas <= 0,12)

, Series __new__, Series.

:

class Support(pd.Series):
    def __new__(cls, *args, **kwargs):
        arr = Series.__new__(cls, *args, **kwargs)
        return arr.view(Support)

    def supportMethod1(self):
        print 'I am support method 1'       
    def supportMethod2(self):
        print 'I am support method 2'

, , a-a a-a. Series. , Pandas - . - ,

s.ix[:5] 
s.cumsum()

Series . . , , . , , - s.ix[:5] . , .

http://nbviewer.ipython.org/3366583/subclassing%20pandas%20objects.ipynb .

+10

Support() Series.

Series DataFrame . : https://github.com/pydata/pandas/issues/60

In [16]: class MyDict(dict):
   ....:     pass
   ....:

In [17]: md = MyDict()

In [18]: type(md)
Out[18]: __main__.MyDict

In [21]: class MySeries(Series):
   ....:     pass
   ....:

In [22]: ms = MySeries()

In [23]: type(ms)
Out[23]: pandas.core.series.Series
+2

All Articles