I'm writing unittest for a method that returns a dataframe, but, while testing the output using:
self.asserEquals(mock_df, result)
I'm getting ValueError:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Right now I'm comparing properties that serves the purpose now,
self.assertEqual(mock_df.size, result.size)
self.assertEqual(mock_df.col_a.to_list(), result.col_a.to_list())
self.assertEqual(mock_df.col_b.to_list(), result.col_b.to_list())
self.assertEqual(mock_df.col_c.to_list(), result.col_c.to_list())
but curious how do I assert dataframes.
The accepted answer from @Mahi did not work for me. It failed for two Dataframes that should have been equal. Not sure why.
As I discovered here under "DataFrame equality", there are some functions built into Pandas for testing.
The following worked for me. I tested it several times, but not exhaustively, to make sure it would work repeatedly.
import unittest
import pandas as pd
class test_something(unittest.TestCase):
def test_method(self):
#... create dataframes df1 and df2...
pd.testing.assert_frame_equal(df1,df2)
Here is related pandas reference for the above function.
import unittest
import pandas as pd
class TestDataFrame(unittest.TestCase):
def test_dataframe(self):
df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df2 = pd.DataFrame({'a': [1, 2], 'b': [3.0, 4.0]})
self.assertEqual(True, df1.equals(df2))
if __name__ == '__main__':
unittest.main()
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