2017-02-16 98 views
0

我已经创建了所有包含另一种数据结构(在这种情况下一个熊猫数据帧的新类。我可以将属性的方法扩展到其父类吗?

这个类就会有其他的属性和它的其他方法,除了大熊猫数据帧。有些方法的命名方式与DataFrame中的方法类似,例如,to_excel,但在调用DataFrame方法之前做了一些其他的事情,无论如何,其主要组成部分就是这个DataFrame,因此我希望能够使用所有其他方法数据帧,例如getitem的,直接在这个类的对象。

class NewDataStructure: 
    def __init__(self): 
     self.df = pd.DataFrame() 
     # have some extra attributes here that the pandas DataFrame doesn't have 

    def __getitem__(self, key): 
     return self.df.__getitem__(key) 

    def to_excel(self, writer): 
     # do some extra stuff here that the pandas DataFrame doesn't do but use the pandas method eventually 
     self.df.to_excel(writer) 

是否有办法扩展一个属性的方法到它的父类?或者,我是否以这种错误的方式去做? NewDataStructure应该从DataFrame继承吗?

回答

1

要么覆盖__getattr__

class NewDataStructure: 
    def __init__(self): 
     self.df = pd.DataFrame() 
     # have some extra attributes here that the pandas DataFrame doesn't have 

    def __getitem__(self, key): 
     return self.df.__getitem__(key) 

    def __getattr__(self, item): 
     try: 
      return vars(self)[item] 
     except KeyError: 
      return getattr(self.df, item) 

    def to_excel(self, writer): 
     # do some extra stuff here that the pandas DataFrame doesn't do but use the pandas method eventually 
     self.df.to_excel(writer) 

obj = NewDataStructure() 
print(obj.ix) 
# <pandas.core.indexing._IXIndexer object at 0x01FE7090> 
# pandas' ix 
print(obj.to_excel) 
# <bound method NewDataStructure.to_excel of <__main__.NewDataStructure object at 0x005670F0>> 
# NewDataStructure's to_excel 

如果我们从NewDataStructure类中删除to_excel,我们将使用大熊猫to_excel

class NewDataStructure: 
     def __init__(self): 
      self.df = pd.DataFrame() 
      # have some extra attributes here that the pandas DataFrame doesn't have 

     def __getitem__(self, key): 
      return self.df.__getitem__(key) 

     def __getattr__(self, item): 
      try: 
       return vars(self)[item] 
      except KeyError: 
       return getattr(self.df, item) 

obj = NewDataStructure() 
print(obj.to_excel) 
#  <bound method DataFrame.to_excel of Empty DataFrame 
#  Columns: [] 
#  Index: []> 

或者从pd.DataFrame(可能更容易继承和更好的方式去):

class NewDataStructure(pd.DataFrame): 
    def __init__(self, *args, **kwargs): 
     super().__init__(*args, **kwargs) 

obj = NewDataStructure() 
print(obj.to_excel) 
#  <bound method DataFrame.to_excel of Empty DataFrame 
#  Columns: [] 
#  Index: []> 
# pandas to_excel 

如果我们将to_excel添加到NewDataStructure中:

def to_excel(self, *args, **kwargs): 
    # do some extra stuff here that the pandas DataFrame doesn't do but use the pandas method eventually 
    super().to_excel(*args, **kwargs) 
. 
. 

obj = NewDataStructure() 
print(obj.to_excel) 
# <bound method NewDataStructure.to_excel of Empty NewDataStructure 
# Columns: [] 
# Index: []> 
相关问题