Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `assert_frame_equal` and `equals`

I'm curious to find the difference between assert_frame_equal and equal. Both are for checking the equality of two data. It applies for assert_series_equal and assert_index_equal. So what is the difference between equals and testing functions?

So far I found was testing functions gives little more flexibility to compare the values, like check_dtpye options etc., and differs from returning values Is this the only difference between them?

or otherwise, When Should I use testing functions other than equals method?

df1=pd.DataFrame({'a':[1,2,3,4,5],'b':[6,7,8,9,10]})
df2=pd.DataFrame({'a':[1,2,3,4,5],'b':[6,7,8,9,10]})
pd.testing.assert_frame_equal(df1,df2)
print df1.equals(df2)

pd.testing.assert_series_equal(df1['a'],df2['a'])
print df1['a'].equals(df2['a'])

pd.testing.assert_index_equal(df1.index,df2.index)
print df1.index.equals(df2.index)
like image 257
Mohamed Thasin ah Avatar asked Jun 25 '18 06:06

Mohamed Thasin ah


People also ask

What does assert_ frame_ equal do?

Check that left and right DataFrame are equal. This function is intended to compare two DataFrames and output any differences. Is is mostly intended for use in unit tests.

How do you know if two data frames 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 compare two DataFrames in pandas?

The compare method in pandas shows the differences between two DataFrames. It compares two data frames, row-wise and column-wise, and presents the differences side by side. The compare method can only compare DataFrames of the same shape, with exact dimensions and identical row and column labels.


1 Answers

assert_frame_equal throws an AssertionError when two DataFrames aren't equal.

pd.testing.assert_frame_equal(df1, df2)            # no result - pass

pd.testing.assert_frame_equal(df1, pd.DataFrame()) # throws error - fail
# AssertionError       

DataFrame.equals simply returns a boolean True/False.

df1.equals(df2)
# True

df1.equals(pd.DataFrame())
# False    

This is also the case for the other functions defined in pd.testing, which are used to develop unit tests for pandas code.

like image 72
cs95 Avatar answered Sep 20 '22 16:09

cs95