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

非 Y 不嫁゛ 提交于 2020-01-02 02:59:47

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/51017249/what-is-the-difference-between-assert-frame-equal-and-equals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!