创建具有多个inheritan的dataframe子类

2024-04-20 13:06:22 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我想子类一个熊猫数据帧,但子类的数据帧也将继承自我自己的自定义类。我想这样做是因为我想创建多个子类dataframes,以及其他子类(不是dataframes),它们将共享这个基类的属性和方法。你知道吗

开始我的基础课是

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

我只是尝试创建一个空白的数据帧,并只给它命名为file location

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]


Tags: pathnameselfreturninitosdefcustom
1条回答
网友
1楼 · 发布于 2024-04-20 13:06:22

您在评论部分I've read that中指出。但是,你没有。这就是问题的根源。因为that描述了对pandas数据帧进行子类化的步骤,包括定义原始属性的方法。你知道吗

考虑下面对代码的修改。关键部分是_metadata。我删除了thing类中的所有属性,因为它们增加了原始属性名的数量—它们都必须添加到_metadata。我还添加了__repr__方法来修复另一个RecursionError。最后,我删除了directory属性,因为它给了我TypeError。你知道吗

import pandas as pd

class thing(object):

    def __init__(self, item_location, name):
        self.name = name
        self.file = item_location

    def __repr__(self):
        return 'dummy_repr'

class custom_dataframe(thing, pd.DataFrame):

    _metadata = ['name', 'file', 'directory']

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

    @property
    def _constructor(self):
        return custom_dataframe

if __name__ == '__main__':
    cd = custom_dataframe('/foobar/foobar/foobar.html', 'name')

编辑。一个稍微增强的版本-相当差的实现。你知道吗

import pandas as pd

class thing:

    _metadata = ['name', 'file']

    def __init__(self, item_location, name):
        self.name = name
        self.file = item_location

class custom_dataframe(thing, pd.DataFrame):

    def __init__(self, *args, **kwargs):
        item_location = kwargs.pop('item_location', None)
        name = kwargs.pop('name', None)
        thing.__init__(self, item_location, name)
        pd.DataFrame.__init__(self, *args, **kwargs)

    @property
    def _constructor(self):
        return custom_dataframe

if __name__ == '__main__':

    cd = custom_dataframe(
        {1: [1, 2, 3], 2: [1, 2, 3]},
        item_location='/foobar/foobar/foobar.html',
        name='name')

相关问题 更多 >