我想创建一个DataFrame
,它包含我定义的许多不同的不同的Series
子类。然而,当分配给DataFrame
时,似乎子类从Series
中剥离。在
下面是一个玩具例子来说明这个问题:
>>> import pandas as pd
>>> class SeriesSubclass(pd.Series):
... @property
... def _constructor(self):
... return SeriesSubclass
... def times_two(self):
... """Method I need in this subclass."""
... return self * 2
...
>>> subclass = SeriesSubclass([7, 8, 9])
>>> type(subclass) # fine
<class '__main__.SeriesSubclass'>
>>> subclass.times_two() # fine
0 14
1 16
2 18
dtype: int64
>>>
>>> data = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=list('ABC'))
>>> data['D'] = subclass
>>> type(data['D']) # not good
<class 'pandas.core.series.Series'>
>>> data['D'].times_two() # not good
Traceback (most recent call last):
...
AttributeError: 'Series' object has no attribute 'times_two'
我见过这个问题以前可能已经提过了,但我不知道实际的解决方案。作为一个如此庞大的库,很难遵循各种pr、doc版本等,而且子类化机制似乎没有我所能描述的那么好(this seems to be it)。在
对于任何有类似需求的人来说:我认为最好的解决方案是定义
DataFrame
的子类并使用__getitem__
逻辑进行干预。在我最初的问题是基于这样一个假设:一个
DataFrame
被实现为一个容器,而实际上它不是。。。在因此,为了检索作为
Series
的子类的列,您需要修改__getitem__
。在我已经在我自己的包中实现了这一点,可以作为一个例子:https://github.com/jmackie4/activityio/blob/master/activityio/_util/types.py
不过,我很想听到任何人有一个更优雅的解决方案!在
我认为除非你还定义了你自己的
pd.DataFrame
子类,否则你就走运了。这将是一项更艰巨的任务。在考虑这个例子
df.A
不是{df.A
从s
构造而忽略它是什么类型。在相关问题 更多 >
编程相关推荐