Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert Two Frames Are Not Equal

I need to test that two pandas dataframes are not equal. Is there an equivalent to pandas assert_frame_equal function that does this? If not, what's the best/safest way to assert that the frames aren't equal?

like image 706
Batman Avatar asked Aug 04 '16 22:08

Batman


People also ask

How do you assert two DataFrames are equal?

DataFrame - equals() function The equals() function is used to test whether two objects contain the same elements. This function allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements. NaNs in the same location are considered equal.

How do I check if two rows have the same value in pandas?

The equals() function is used to test whether two Pandas objects contain the same elements.


2 Answers

You could write your own assertion function that uses assert_frame_equal() and inverts the result:

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.

like image 97
mhawke Avatar answered Oct 19 '22 00:10

mhawke


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

like image 44
Gaurav Dhama Avatar answered Oct 19 '22 00:10

Gaurav Dhama