嗨,我想子类一个熊猫数据帧,但子类的数据帧也将继承自我自己的自定义类。我想这样做是因为我想创建多个子类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]
您在评论部分
I've read that
中指出。但是,你没有。这就是问题的根源。因为that
描述了对pandas数据帧进行子类化的步骤,包括定义原始属性的方法。你知道吗考虑下面对代码的修改。关键部分是
_metadata
。我删除了thing
类中的所有属性,因为它们增加了原始属性名的数量—它们都必须添加到_metadata
。我还添加了__repr__
方法来修复另一个RecursionError
。最后,我删除了directory
属性,因为它给了我TypeError
。你知道吗编辑。一个稍微增强的版本-相当差的实现。你知道吗
相关问题 更多 >
编程相关推荐