python - Pandas assert_frame_equal 行为

标签 python pandas testing

我正在尝试通过 pandas 测试 assert_frame_equal 来比较两个 DataFrame。这些帧包含 float ,我想将其与某些用户定义的精度进行比较。

assert_frame_equal 中的check_less_precise 参数似乎表明我可以指定要比较的小数点后的位数。引用 API 引用页面 -

check_less_precise: Specify comparison precision. Only used when check_exact is False. 5 digits (False) or 3 digits (True) after decimal points are compared. If int, then specify the digits to compare

API Reference

但是,当 float 小于 1 时,这似乎不起作用。

这引发了一个 AssertionError

import pandas as pd

expected = pd.DataFrame([{"col": 0.1}])
output = pd.DataFrame([{"col": 0.12}])
pd.testing.assert_frame_equal(expected, output, check_less_precise=1)

虽然这不是

expected = pd.DataFrame([{"col": 1.1}])
output = pd.DataFrame([{"col": 1.12}])
pd.testing.assert_frame_equal(expected, output, check_less_precise=1)

谁能帮忙解释一下这个行为,这是一个错误吗?

最佳答案

check_less_precise 更像是相对公差。请参阅下面的详细信息。

我仔细研究了源代码,发现了发生了什么。最终函数 decimal_almost_equal 被调用,它在普通 Python 中看起来像这样(在 Cython 中)。

def decimal_almost_equal(desired, actual, decimal):
    return abs(desired - actual) < (0.5 * 10.0 ** -decimal)

参见 the source code here这是对该函数的实际调用:

decimal_almost_equal(1, fb / fa, decimal)

在这个例子中

fa = .1
fb = .12
decimal = 1

所以函数调用就变成了

decimal_almost_equal(1, 1.2, 1)

decimal_almost_equal 的计算结果为

abs(1 - 1.2) < .5  * 10 ** -1

或者

.2 < .05

为假

所以比较是基于百分比差异而不是总差异。

如果您想要绝对比较,请查看 np.allclose

np.allclose(expected, output, atol=.1)
True

关于python - Pandas assert_frame_equal 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46304621/

相关文章:

python - 在 Python tkinter 3.4 中使 Logo 透明

python - Windows 10 资源管理器文件打开对话框 : filenames disappearing in compiled executable

python - np.logic_or 与reduce 返回不同的结果

python - 检查前 n 行的值是否都大于当前行的值

testing - 使用 plone.app.testing 导入错误

jquery - 使用 jinja 和 jQuery 循环浏览 HTML 列表?

python - 'NoneType' 对象在泡沫中没有属性 'str'

python - 将序号添加到 Pandas 中的 groupby().head(n) 表达式中

c - 如果 diff 命令导致 bash 没有差异,如何输出 'passed'?

testing - Moxios - 类型错误 : Cannot read property 'adapter' of undefined