如何在pandas系列中使用assertSequenceEqual?

时间:2018-04-26 06:25:16

标签: python pandas python-unittest

我想使用<ul> <li> <h3>h3<br>h3<br>h3</h3> <h3>h3</h3> <h3>h3</h3> </li> <li> <h4>h4</h4> <h4>h4</h4> <h4>h4</h4> </li> <li> <p>p</p> <p>p</p> <p>p<br>p</p> </li> </ul>模块检查两个pandas.Series对象是否具有相同的内容:

unittest

根据 self.assertSequenceEqual( df['some_column'], someOtherSeries) 文档,上述内容应该有效(基于the docs)。但是,当我运行上面的单元测试时,我得到了这个:

unittest

如何测试系列是否相同?

请注意,将系列转换为====================================================================== ERROR: test_my_test (my_module.test.test_my_module.SimpleTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/root/src/my_module/my_module/test/test_my_test.py", line 28, in test_my_test someOtherSeries) File "/usr/local/lib/python2.7/unittest/case.py", line 663, in assertSequenceEqual if seq1 == seq2: File "/usr/local/lib/python2.7/site-packages/pandas/core/generic.py", line 917, in __nonzero__ .format(self.__class__.__name__)) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 对象似乎可以解决这个问题,但感觉就像是黑客攻击:

list

1 个答案:

答案 0 :(得分:3)

请注意,df['some_column'].valuesnumpy数组。要测试 numpy数组的等值(等价),您可以使用numpy.testing

from numpy import testing

testing.assert_array_equal(df['some_column'].values, someOtherSeries.values)

如果数组是浮点数,则应考虑numpy.testing.assert_almost_equal

testing.assert_almost_equal(df['some_column'].values, someOtherSeries.values)

直接等同浮点数是有问题的。