简体   繁体   English

断言两个帧不相等

[英]Assert Two Frames Are Not Equal

I need to test that two pandas dataframes are not equal. 我需要测试两个pandas数据帧是相等的。 Is there an equivalent to pandas assert_frame_equal function that does this? 有没有相当于pandas assert_frame_equal函数来做到这一点? If not, what's the best/safest way to assert that the frames aren't equal? 如果不是,那么断言帧不相等的最佳/最安全的方法是什么?

You could write your own assertion function that uses assert_frame_equal() and inverts the result: 您可以编写自己的断言函数,该函数使用assert_frame_equal()并反转结果:

def assert_frame_not_equal(*args, **kwargs):
    try:
        assert_frame_equal(*args, **kwargs)
    except AssertionError:
        # frames are not equal
        pass
    else:
        # frames are equal
        raise AssertionError

This will use the same logic that assert_frame_equal() uses for comparing data frames, so the question of what constitutes equality is avoided - inequality is simply the opposite of whatever assert_frame_equal() determines. 这将使用assert_frame_equal()用于比较数据帧的相同逻辑,因此避免了构成相等的问题 - 不等式与assert_frame_equal()确定的相反。

Assuming assert_frame_equal behaves like assert (meaning either nothing happens or it raises AssertionError ) then you probably can just wrap it in a try : 假设assert_frame_equal行为类似于assert (意味着没有任何反应或它引发AssertionError ),那么你可能只需将它包装在try

def assert_frame_not_equal(df1, df2):
    try:
        assert_frame_equal(df1, df2)
        raise AssertionError('DataFrames are equal.')
    except AssertionError:
        pass

Add *args and/or **kwargs as desired for flexibility. 根据需要添加*args和/或**kwargs以获得灵活性。

Yes there is : 就在这里 :

# Let us suppose you have two dataframes df1 and df2
# Check for equality by using
df1.equals(df2)

Use not to assert that they are not equal not断言他们不平等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM