Describe the bug
I'm working on the unit test file for data.py
with test_data.py
with the purpose of testing my Dataset
class methods. There are total 13 tests in my unit test file representing 13 methods in my Dataset
class. When I ran the test, the result returned 2 errors related to the get_head()
and get_tail()
methods.
My defined method in data.py
looks like this:
def get_head(self, n=5):
"""
Return Pandas Dataframe with top rows of loaded dataset
"""
return self.df.iloc[:n]
Here is the example of how I run the get_head()
test in the test_data.py
, the command is exactly the same logic with the get_tail()
:
class GetHead(unittest.TestCase):
def test_get_head(self):
# create sample dataframe
data = [['tom', 10], ['nick', 15], ['juli', 14], ['annie', 24], ['julie', 20], ['leah', 18],['patrick', 27]]
test_df = pd.DataFrame(data, columns = ['Name', 'Age'])
test_file_name = "car_accident.csv"
test_dataset = Dataset(test_df, test_file_name)
self.assertEqual(test_dataset.get_head(), test_df.head())
However, the test result return this error:
Traceback (most recent call last):
File "test_data.py", line 234, in test_get_head
self.assertEqual(test_dataset.get_head(2), test_df.head(2).any())
File "C:\Users\LEAH NGUYEN\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 912, in assertEqual
assertion_func(first, second, msg=msg)
File "C:\Users\LEAH NGUYEN\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 902, in _baseAssertEqual
if not first == second:
File "C:\Users\LEAH NGUYEN\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 1537, in __nonzero__
raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I try to look up this issue online but couldn't find a good explaination prior to my test. Any ideas of what is this error and how can I improve it @caijiaping @Laurabayonaf @cartierz ?
To Reproduce
Steps to reproduce the behavior:
- Go to
\src\test
- Open the terminal > Run
python test_data.py
- See error