检查两个包含序列的对象是否相等

2024-10-03 02:46:46 发布

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

作为单元测试的一部分,我尝试比较两个Cashflow对象,它们只包含一个Series

在单元测试中,我有以下内容,其中cashflowobj_1cashflowobj_2是包含SeriesCashflow对象:

self.assert_(cashflowobj_1==cashflowobj_2)

这两个系列看起来与下面看到的完全相同,但是测试返回Series are not equal,所以我想知道我用来检查它们相等性的函数中是否有问题cashflowobj_1.amountscashflowobj_2.amounts分别是Series

cashflowobj_1.amounts

     1983-05-15      1
     1983-11-15      1
     1984-05-15      1
     1984-11-15     101

cashflowobj_2.amounts

     1983-05-15      1
     1983-11-15      1
     1984-05-15      1
     1984-11-15     101

用于检查Cashflow对象是否相等的函数,该函数位于Cashflow类中:

def __eq__(self, other):
    '''
    Series being equal
    '''
    if ((len(self.amounts) == len(other.amounts))) and ((all(i in self.amounts for i in other.amounts))):
        print('Series are equal')
        return(1)
    else:
        print('Series are not equal')
        return(0)

编辑:解决@matt的问题:索引都是datetime.date()对象。值为float64


Tags: 对象函数inselflennotequal单元测试
1条回答
网友
1楼 · 发布于 2024-10-03 02:46:46

你不是只想看看级数是否相等吗?假设你说的是熊猫系列

使用Series.equals()函数

def __eq__(self, other):
    '''
    Series being equal
    '''
    if self.amounts.equals(other.amounts)
        print('Series are equal')
        return(1)
    else:
        print('Series are not equal')
        return(0)

相关问题 更多 >