I am getting the following error:
ERROR: test_average_age_men (test_module.DemographicAnalyzerTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/EnlightenedWhirlwindMemorypool/test_module.py", line
16, in test_average_age_men
self.assertAlmostEqual(actual, expected, "Expected different value fo
r average age of men.")
File "/usr/lib/python3.8/unittest/case.py", line 957, in assertAlmostEq
ual
if round(diff, places) == 0:
TypeError: an integer is required (got type str)
I am getting this error for 5 test cases out of 10.
The code that I am using is:
average_age_men = df.loc[df['sex']=='Male', 'age'].mean()
I checked the type of the variable and it is numpy.float64
.
Please help me with this.
I didn’t withdraw the post. I am looking for help with that error.
if you upload the code to github I might take a look
It seems that the usage of assertAlmostEqual
in test suite is wrong.
Third argument of assertAlmostEqual
should be places
(i.e. number of decimal places to be compared), instead of msg
(i.e. message to display when the test is failed.)
For example, the 16th line of test_module.py should be:
self.assertAlmostEqual(actual, expected, 1, "Expected different value for average age of men.")
For workaround (without changing test suite), round to first places for all non-integer numbers. (i.e. round(result, 1)
)
4 Likes
You could try this.
average_age_men =round( df.groupby(‘sex’, as_index=True).age.mean()[“Male”],1)
1 Like
try this code
df[df[‘sex’]==‘Male’][‘age’].mean().round(1)
Hi guys, I;m so far 2 projects in.
However, it seems we cant really tell if i got the answer correct or not.
My current solution is to compare my individual "cell " answer to the answer mentioned in test module.py.
Curretnly both my project 1 and project 2 simply reply with OK, and I am not sure if i got it right or not.
At the same time, I got this error msg for my project2
I was having the same problem as OP and this was a nice practical solution. Thanks!
I also had the very same problem. Using round(1) fixed it. Thanks a lot !