Reputation: 8927
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?
Upvotes: 17
Views: 6466
Reputation: 2897
For one-off comparisons between 2 pandas dataframes I don't expect to be equal in a unittest unit test, I just do:
import pandas as pd
with self.assertRaises(AssertionError):
pd.testing.assert_frame_equal(df2, df1)
Upvotes: 1
Reputation: 87084
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.
Upvotes: 20
Reputation: 1336
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
Upvotes: 4