如何将 assertSequenceEqual 与 pandas 系列一起使用?
How to use assertSequenceEqual with pandas Series?
我想使用 unittest
模块检查两个 pandas.Series
对象是否具有相同的内容:
self.assertSequenceEqual(
df['some_column'],
someOtherSeries)
根据 unittest
文档,上述内容应该有效(基于 the docs)。但是,当我 运行 我的上述单元测试时,我得到了这个:
======================================================================
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().
如何测试系列是否相同?
请注意,将 Series 转换为 list
对象似乎可以解决这个问题,但感觉就像一个 hack:
self.assertSequenceEqual(
list(df['some_column']),
list(someOtherSeries))
注意 df['some_column'].values
是一个 numpy
数组。要测试 numpy
数组的相等性(o 等价性),您可以使用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)
因为直接等于浮点数是有问题的。
我想使用 unittest
模块检查两个 pandas.Series
对象是否具有相同的内容:
self.assertSequenceEqual(
df['some_column'],
someOtherSeries)
根据 unittest
文档,上述内容应该有效(基于 the docs)。但是,当我 运行 我的上述单元测试时,我得到了这个:
======================================================================
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().
如何测试系列是否相同?
请注意,将 Series 转换为 list
对象似乎可以解决这个问题,但感觉就像一个 hack:
self.assertSequenceEqual(
list(df['some_column']),
list(someOtherSeries))
注意 df['some_column'].values
是一个 numpy
数组。要测试 numpy
数组的相等性(o 等价性),您可以使用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)
因为直接等于浮点数是有问题的。