python - 断言不相等的相同数据帧 - Python Pandas

标签 python unit-testing pandas dataframe python-unittest

我正在尝试对我的代码进行单元测试。我有一个给定 MySQL 查询的方法,将结果作为 pandas 数据框返回。请注意,在数据库中,createdexternal_id 中的所有返回值为 NULL。这是测试:

def test_get_data(self):

    ### SET UP

    self.report._query = "SELECT * FROM floor LIMIT 3";
    self.report._columns = ['id', 'facility_id', 'name', 'created', 'modified', 'external_id']
    self.d = {'id': p.Series([1, 2, 3]),
              'facility_id': p.Series([1, 1, 1]),
              'name': p.Series(['1st Floor', '2nd Floor', '3rd Floor']),
              'created': p.Series(['None', 'None', 'None']),
              'modified': p.Series([datetime.strptime('2012-10-06 01:08:27', '%Y-%m-%d %H:%M:%S'),
                                    datetime.strptime('2012-10-06 01:08:27', '%Y-%m-%d %H:%M:%S'),
                                    datetime.strptime('2012-10-06 01:08:27', '%Y-%m-%d %H:%M:%S')]),
              'external_id': p.Series(['None', 'None', 'None'])
              }
    self.df = p.DataFrame(data=self.d, columns=['id', 'facility_id', 'name', 'created', 'modified', 'external_id'])
    self.df.fillna('None')
    print(self.df)
    ### CODE UNDER TEST

    result = self.report.get_data(self.report._cursor_web)
    print(result)
    ### ASSERTIONS

    assert_frame_equal(result, self.df)

这是控制台输出(注意测试代码中的打印语句。手动构造的数据框在顶部,从被测函数派生的数据框在底部):

.   id  facility_id       name created            modified external_id
0   1            1  1st Floor    None 2012-10-06 01:08:27        None
1   2            1  2nd Floor    None 2012-10-06 01:08:27        None
2   3            1  3rd Floor    None 2012-10-06 01:08:27        None
   id  facility_id       name created            modified external_id
0   1            1  1st Floor    None 2012-10-06 01:08:27        None
1   2            1  2nd Floor    None 2012-10-06 01:08:27        None
2   3            1  3rd Floor    None 2012-10-06 01:08:27        None
F
======================================================================
FAIL: test_get_data (__main__.ReportTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/file/ReportsTestCase.py", line 46, in test_get_data
    assert_frame_equal(result, self.df)
   File "/usr/local/lib/python2.7/site-packages/pandas/util/testing.py", line 1313, in assert_frame_equal
obj='DataFrame.iloc[:, {0}]'.format(i))
  File "/usr/local/lib/python2.7/site-packages/pandas/util/testing.py", line 1181, in assert_series_equal
obj='{0}'.format(obj))
  File "pandas/src/testing.pyx", line 59, in pandas._testing.assert_almost_equal (pandas/src/testing.c:4156)
  File "pandas/src/testing.pyx", line 173, in pandas._testing.assert_almost_equal (pandas/src/testing.c:3274)
  File "/usr/local/lib/python2.7/site-packages/pandas/util/testing.py", line 1018, in raise_assert_detail
raise AssertionError(msg)

AssertionError: DataFrame.iloc[:, 3] 不同

DataFrame.iloc[:, 3] values are different (100.0 %)
[left]:  [None, None, None]
[right]: [None, None, None]

----------------------------------------------------------------------
Ran 1 test in 0.354s

FAILED (failures=1)

根据我的估计,“created”列在左右数据框中都包含三个字符串值“None”。为什么断言不相等?

最佳答案

Python 还有一个内置常量None,它不同于字符串'None'。来自docs :

None

The sole value of the type NoneType. None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to None are illegal and raise a SyntaxError.

如果将 None'None' 进行比较(None == 'None'),结果将为 False。因此,如果其中一个 DataFrame 包含 None 而另一个包含 'None'assert_frame_equal 将引发 AssertionError。

关于python - 断言不相等的相同数据帧 - Python Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44399185/

相关文章:

python - 在 Python 中打开一个没有换行符的大型 JSON 文件进行 csv 转换 Python 2.6.6

python - 过滤元组列表以包含最大值和最小值

java - 集成测试包含哪些内容以及如何设置它们

unit-testing - 使用 JUnit 进行参数化测试

python - 合并范围交集上的两个 DataFrame

python - 使用列表匹配包含整个单词的正则表达式

python -gnupg : retrieve public key of a signed message

python - Python 中引用计数作为垃圾收集方法会导致内存泄漏吗?

ruby-on-rails - 如何在 Rails 测试中填充查找表

python - 如何根据序列关系对数据框列进行分组