python - 使用求和函数在列表中添加对象时出现不支持的操作数类型错误

标签 python pandas python-2.7 add series

我实现了一个 Serie 类来表示一系列数据及其标签。当我添加 Serie 对象和数字时,我得到了预期的输出。但是,当我对列表中的相同元素求和时,出现以下错误消息:

TypeError: unsupported operand type(s) for +: 'int' and 'Serie'

玩具示例代码

作为玩具示例代码来理解我们可以使用的问题:

import pandas as pd
import numpy as np

class Serie(object):

    def __str__(self):

        s = "> SERIE " + str(self.tag) + ": \n"
        s += str(self.data)
        return s

    def __init__(self, tag=None, data=pd.DataFrame()):
        """
        Creates object Serie

        @type tag: str
        @param tag: Tag for the Serie
        """
        self.tag = tag
        self.data = data

    def __add__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            tag = str(other) + "+" + self.tag
            serie = Serie(tag)
            serie.data = self.data + other
        else:
            try:
                tag = self.tag + "+" + other.tag
            except:
                print ("ERROR: You can't add to somehing that is not a Serie or a number." )
                return None
            serie = Serie(tag)
            serie.data = self.data + other.data
        return serie

s1 = Serie("fibonacci",pd.Series([1,1,2,3,5,8]))
s2 = Serie("2power",pd.Series(np.linspace(1,6,6)**2))
s3 = 10
sumSerie = s1+s2+s3
print sumSerie

这会按预期打印结果:

>>> 
> SERIE 10+fibonacci+2power: 
0    12.0
1    15.0
2    21.0
3    29.0
4    40.0
5    54.0
dtype: float64

使用列表中的对象总和时出错

但是当我运行以下行时:

l = [s1,s2,s3]
sum(l)

我收到错误信息:

sum(l) TypeError: unsupported operand type(s) for +: 'int' and 'Serie'

运行时显示相同的错误消息:

l2 = [s1,s2]
sum(l2)

但是在l2列表中没有int变量。

问题

为什么会显示此错误消息?这很令人困惑,因为我能够对列表外的对象求和。

我可以做些什么来实现列表中对象的总和吗?

编辑

按照评论中的建议,我添加了 __radd__ 以正确重载 add 方法。所以我将以下几行添加到 Serie 类中:

def __radd__(self,other):
    return self.__add__(other)

然后求和。但不如预期。

如果我运行下面的代码:

>>> print sum(l)

我得到这个输出:

> SERIE 10+0+fibonacci+2power: 
0    12.0
1    15.0
2    21.0
3    29.0
4    40.0
5    54.0
dtype: float64

这与我预期的完全不同。标签内有一个额外的 +0 。怎么会这样?但是,如果我使用我在回答 print np.array(l).sum() 中陈述的选项,结果是正确的。

编辑 2

在正确重载 add 方法后,我被建议使用下面的方法来按预期执行求和:

reduce(lambda a, b: a+b, l)

这种方法能够对列表使用 sum 函数并获得正确的结果。

pault 所述在 sum 方法的注释中,“start defaults to 0”详见 sum function's documentation .这就是之前将额外的 +0 添加到标签的原因。

总而言之,我相信我更愿意使用使用 numpy.sum 函数的选项:

np.array(l).sum()

最佳答案

不确定为什么不能使用求和函数。但是从列表中获取所需输出的解决方法是从列表中创建一个 numpy 数组并使用 numpy.sum 函数,如下所示:

np.array(l).sum()

这按预期给出了列表中对象的总和。

关于python - 使用求和函数在列表中添加对象时出现不支持的操作数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54153552/

相关文章:

python - 如何在 Django 1.11 中查找给定月份的每周记录数?

Python Sklearn train_test_split() : how to set Which Data is Taken for Training?

Python:将初始 numpy 数组分配给另一个变量,更改新变量的几个元素会更改初始 numpy 数组

python - 如何遍历 MeshGrid?

python-2.7 - 连续向子进程发送数据

python - Kivy 应用程序具有多个屏幕

python - PyQt4 和写入串行是否需要多线程

python - 查找客户在 pandas 中活跃的最大连续月份数

python - 有效地从字符串列中删除多个短语

python - 如何在Python中对无序离散数据进行热编码?