Another Pandas question!
I am writing some unit tests that test two data frames for equality, however, the test does not appear to look at the values of the data frame, only the structure:
dates = pd.date_range('20130101', periods=6) df1 = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD')) df2 = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD')) print df1 print df2 self.assertItemsEqual(df1, df2)
-->True
Do I need to convert the data frames to another data structure before asserting equality?
Pandas 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.
equals() function is used to determine if two dataframe object in consideration are equal or not. Unlike dataframe. eq() method, the result of the operation is a scalar boolean value indicating if the dataframe objects are equal or not.
Check if all values are equal in a columnSelect the column by name using subscript operator of DataFrame i.e. df['column_name']. It gives the column contents as a Pandas Series object. Compare the Series object (selected column) with the first value. It will return a boolean Series.
Ah, of course there is a solution for this already:
from pandas.util.testing import assert_frame_equal
While assert_frame_equal is useful in unit tests, I found the following useful on analysis as one might want to further check which values are not equal: df1.equals(df2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With