Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 使用DataFrame的频率数据的继承与组合python类_Oop_Python 2.7_Pandas - Fatal编程技术网

Oop 使用DataFrame的频率数据的继承与组合python类

Oop 使用DataFrame的频率数据的继承与组合python类,oop,python-2.7,pandas,Oop,Python 2.7,Pandas,我正在学习Python 2.7中的OOP,我想用以下结构组合一个处理频率数据的类: "Freq" "Device 1" "Device 2" etc..... 100 90 95 500 95 100 . . . . . . 我的第一个想法是使用一个数据帧的组合来表示上面的数据,同时使用一个units属性来跟踪单位ex

我正在学习Python 2.7中的OOP,我想用以下结构组合一个处理频率数据的类:

"Freq"    "Device 1"    "Device 2"    etc.....
 100        90           95
 500        95           100
  .          .            .
  .          .            .
我的第一个想法是使用一个数据帧的组合来表示上面的数据,同时使用一个units属性来跟踪单位ex Volts vs milliVolts

我希望能够使用pandas的所有内置功能,如能够将数据帧合并在一起、切片、索引、绘图等。我还希望跟踪数据的单位,以便在合并、绘图等时,我可以调整值,使它们一致

如果我创建一个基于合成的类,我发现合并是困难的

class FreqData(object):
    __init__(self, data=None, index=None, columns=None, dtype=None, 
                   copy=False, units=None):
        self.df = DataFrame(data, index, columns, dtype, copy)
        self.units = units
然后我的合并方法看起来像这样

    def combine(self, fds, axis=1):

        try:
            chk_units = self.units == fds.units
            fds = [fds]
        except AttributeError:
            chk_units = all([self.units == f.units for f in fds])

        combined_fd = FreqData()
        if chk_units:
            combined_fd.units = self.units
            df_list = [f.df for f in fds]
            df_list.insert(0, self.df)
            combined_fd.df = pd.concat(df_list, axis=1)
            return combined_fd
        else:
            raise TypeError("""One or more of the FreqData objects measurement
                        types does not match""")
有没有一种更简单的方法可以使用组合来实现这一点,还是应该尝试使用继承

另外,我希望使用像df[]这样的切片方法,但我希望能够在FreqData对象上执行此操作,而不必编写类似以下的方法:

    def __getitem__(self, key):
        fd = FreqData()
        fd.df = self.df.__getitem__(key)
        fd.units = self.units
        return fd
这似乎是一堆冗余代码。我该如何改进这一点