创建具有多重继承的数据框的子类

我想对 pandas 数据框进行子类化,但数据框的子类也将从我自己的自定义类继承。我想这样做是因为我想创建多个子类数据框,以及其他将共享此基类的属性和方法的子类(不是数据框)。


开始我的基类是


class thing(object):

   def __init__(self, item_location, name):

      self.name = name

      self.file = item_location

      self.directory = os.path.join(*item_location.split(os.path.sep)[0:-1])

   @property

   def name(self):

       return self._name

   @name.setter

   def name(self,val):

       self._name = val


   @property

   def file(self):

       return self._file

   @file.setter

   def file(self,val):

       self._location = val


   @property

   def directory(self):

      return self._directory

   @directory.setter

   def directory(self,val):

      self._directory = val

现在我的子类之一将从熊猫和事物继承


class custom_dataframe(thing,pd.DataFrame):

   def __init__(self, *args, **kwargs):

      super(custom_dataframe,self).__init__(*args,**kwargs)


   @property

   def _constructor(self):

      return custom_dataframe

我只是尝试制作一个空白数据框,并且只给它命名文件位置


custom_dataframe('/foobar/foobar/foobar.html','name')


我得到一个错误


(我无法将整个堆栈跟踪作为其发布在未连接到 Internet 的计算机上)


File "<stdin>", line 1, in <module>

File "<path to file with classes>", line x, in __init__

  self.name = name

<a bunch of stuff going through pandas library>

File "<path to pandas generic.py>", line 4372, in __getattr__

  return object.__getattribute__(self,name)

RecursionError: maximum recursion depth exceeded while calling a Python object

我正在使用熊猫 0.23.4


编辑:


改为item_location.split(os.pathsep)[0:-1]_*item_location.split(os.path.sep)[0:-1]


慕雪6442864
浏览 80回答 1
1回答

慕神8447489

你在评论区说I've read that。但是,你没有。这就是问题的根源。由于that描述了对 pandas 数据框进行子类化的步骤,包括定义原始属性的方法。考虑对您的代码进行以下修改。关键部分是_metadata。我从thing类中删除了所有属性,因为它们夸大了原始属性名称的数量 - 它们都必须添加到_metadata. 我还添加__repr__了修复另一个RecursionError. 最后,我删除directory了属性,因为它给了我TypeError.import pandas as pdclass thing(object):&nbsp; &nbsp; def __init__(self, item_location, name):&nbsp; &nbsp; &nbsp; &nbsp; self.name = name&nbsp; &nbsp; &nbsp; &nbsp; self.file = item_location&nbsp; &nbsp; def __repr__(self):&nbsp; &nbsp; &nbsp; &nbsp; return 'dummy_repr'class custom_dataframe(thing, pd.DataFrame):&nbsp; &nbsp; _metadata = ['name', 'file', 'directory']&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super(custom_dataframe, self).__init__(*args, **kwargs)&nbsp; &nbsp; @property&nbsp; &nbsp; def _constructor(self):&nbsp; &nbsp; &nbsp; &nbsp; return custom_dataframeif __name__ == '__main__':&nbsp; &nbsp; cd = custom_dataframe('/foobar/foobar/foobar.html', 'name')编辑。有点增强的版本 - 实施很差。import pandas as pdclass thing:&nbsp; &nbsp; _metadata = ['name', 'file']&nbsp; &nbsp; def __init__(self, item_location, name):&nbsp; &nbsp; &nbsp; &nbsp; self.name = name&nbsp; &nbsp; &nbsp; &nbsp; self.file = item_locationclass custom_dataframe(thing, pd.DataFrame):&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; item_location = kwargs.pop('item_location', None)&nbsp; &nbsp; &nbsp; &nbsp; name = kwargs.pop('name', None)&nbsp; &nbsp; &nbsp; &nbsp; thing.__init__(self, item_location, name)&nbsp; &nbsp; &nbsp; &nbsp; pd.DataFrame.__init__(self, *args, **kwargs)&nbsp; &nbsp; @property&nbsp; &nbsp; def _constructor(self):&nbsp; &nbsp; &nbsp; &nbsp; return custom_dataframeif __name__ == '__main__':&nbsp; &nbsp; cd = custom_dataframe(&nbsp; &nbsp; &nbsp; &nbsp; {1: [1, 2, 3], 2: [1, 2, 3]},&nbsp; &nbsp; &nbsp; &nbsp; item_location='/foobar/foobar/foobar.html',&nbsp; &nbsp; &nbsp; &nbsp; name='name')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python