Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe values equality test

Tags:

python

pandas

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?

like image 368
MarkNS Avatar asked Nov 12 '13 11:11

MarkNS


People also ask

How do you check Pandas 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.

How do you know if two values are equal in Pandas?

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.

How do you check if all values in a column are equal Pandas?

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.


2 Answers

Ah, of course there is a solution for this already:

from pandas.util.testing import assert_frame_equal 
like image 162
MarkNS Avatar answered Oct 09 '22 04:10

MarkNS


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)

like image 39
Rustam Aliyev Avatar answered Oct 09 '22 03:10

Rustam Aliyev