Skip to content

TST: more strict testing in lint.sh #13334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if [ "$LINT" ]; then
echo "Linting DONE"

echo "Check for invalid testing"
grep -r --include '*.py' --exclude nosetester.py --exclude testing.py 'numpy.testing' pandas
grep -r -E --include '*.py' --exclude nosetester.py --exclude testing.py '(numpy|np)\.testing' pandas
if [ $? = "0" ]; then
RET=1
fi
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/tests/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_frame_non_unique_index(self):
orient='split'))
unser = read_json(df.to_json(orient='records'), orient='records')
self.assert_index_equal(df.columns, unser.columns)
np.testing.assert_equal(df.values, unser.values)
tm.assert_almost_equal(df.values, unser.values)
unser = read_json(df.to_json(orient='values'), orient='values')
tm.assert_numpy_array_equal(df.values, unser.values)

Expand Down
6 changes: 3 additions & 3 deletions pandas/io/tests/test_packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,14 @@ def _test_small_strings_no_warn(self, compress):
with tm.assert_produces_warning(None):
empty_unpacked = self.encode_decode(empty, compress=compress)

np.testing.assert_array_equal(empty_unpacked, empty)
tm.assert_numpy_array_equal(empty_unpacked, empty)
self.assertTrue(empty_unpacked.flags.writeable)

char = np.array([ord(b'a')], dtype='uint8')
with tm.assert_produces_warning(None):
char_unpacked = self.encode_decode(char, compress=compress)

np.testing.assert_array_equal(char_unpacked, char)
tm.assert_numpy_array_equal(char_unpacked, char)
self.assertTrue(char_unpacked.flags.writeable)
# if this test fails I am sorry because the interpreter is now in a
# bad state where b'a' points to 98 == ord(b'b').
Expand All @@ -688,7 +688,7 @@ def _test_small_strings_no_warn(self, compress):
# always be the same (unless we were able to mutate the shared
# character singleton in which case ord(b'a') == ord(b'b').
self.assertEqual(ord(b'a'), ord(u'a'))
np.testing.assert_array_equal(
tm.assert_numpy_array_equal(
char_unpacked,
np.array([ord(b'b')], dtype='uint8'),
)
Expand Down
20 changes: 13 additions & 7 deletions pandas/src/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ cpdef assert_dict_equal(a, b, bint compare_keys=True):

return True

cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,
cpdef assert_almost_equal(a, b,
check_less_precise=False,
bint check_dtype=True,
obj=None, lobj=None, robj=None):
"""Check that left and right objects are almost equal.

Parameters
----------
a : object
b : object
check_less_precise : bool, default False
check_less_precise : bool or int, default False
Specify comparison precision.
5 digits (False) or 3 digits (True) after decimal points are compared.
If an integer, then this will be the number of decimal points to compare
check_dtype: bool, default True
check dtype if both a and b are np.ndarray
obj : str, default None
Expand All @@ -91,6 +94,8 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,
if robj is None:
robj = b

assert isinstance(check_less_precise, (int, bool))

if isinstance(a, dict) or isinstance(b, dict):
return assert_dict_equal(a, b)

Expand Down Expand Up @@ -145,7 +150,7 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,

for i in xrange(len(a)):
try:
assert_almost_equal(a[i], b[i], check_less_precise)
assert_almost_equal(a[i], b[i], check_less_precise=check_less_precise)
except AssertionError:
is_unequal = True
diff += 1
Expand Down Expand Up @@ -173,11 +178,12 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,
# inf comparison
return True

decimal = 5

# deal with differing dtypes
if check_less_precise:
if check_less_precise is True:
decimal = 3
elif check_less_precise is False:
decimal = 5
else:
decimal = check_less_precise

fa, fb = a, b

Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def test_group_var_generic_1d(self):
expected_counts = counts + 3

self.algo(out, counts, values, labels)
np.testing.assert_allclose(out, expected_out, self.rtol)
self.assertTrue(np.allclose(out, expected_out, self.rtol))
tm.assert_numpy_array_equal(counts, expected_counts)

def test_group_var_generic_1d_flat_labels(self):
Expand All @@ -601,7 +601,7 @@ def test_group_var_generic_1d_flat_labels(self):

self.algo(out, counts, values, labels)

np.testing.assert_allclose(out, expected_out, self.rtol)
self.assertTrue(np.allclose(out, expected_out, self.rtol))
tm.assert_numpy_array_equal(counts, expected_counts)

def test_group_var_generic_2d_all_finite(self):
Expand All @@ -616,7 +616,7 @@ def test_group_var_generic_2d_all_finite(self):
expected_counts = counts + 2

self.algo(out, counts, values, labels)
np.testing.assert_allclose(out, expected_out, self.rtol)
self.assertTrue(np.allclose(out, expected_out, self.rtol))
tm.assert_numpy_array_equal(counts, expected_counts)

def test_group_var_generic_2d_some_nan(self):
Expand All @@ -631,11 +631,11 @@ def test_group_var_generic_2d_some_nan(self):
expected_out = np.vstack([values[:, 0]
.reshape(5, 2, order='F')
.std(ddof=1, axis=1) ** 2,
np.nan * np.ones(5)]).T
np.nan * np.ones(5)]).T.astype(self.dtype)
expected_counts = counts + 2

self.algo(out, counts, values, labels)
np.testing.assert_allclose(out, expected_out, self.rtol)
tm.assert_almost_equal(out, expected_out, check_less_precise=6)
tm.assert_numpy_array_equal(counts, expected_counts)

def test_group_var_constant(self):
Expand Down
34 changes: 19 additions & 15 deletions pandas/tests/test_nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,30 +799,31 @@ def setUp(self):
def test_nanvar_all_finite(self):
samples = self.samples
actual_variance = nanops.nanvar(samples)
np.testing.assert_almost_equal(actual_variance, self.variance,
decimal=2)
tm.assert_almost_equal(actual_variance, self.variance,
check_less_precise=2)

def test_nanvar_nans(self):
samples = np.nan * np.ones(2 * self.samples.shape[0])
samples[::2] = self.samples

actual_variance = nanops.nanvar(samples, skipna=True)
np.testing.assert_almost_equal(actual_variance, self.variance,
decimal=2)
tm.assert_almost_equal(actual_variance, self.variance,
check_less_precise=2)

actual_variance = nanops.nanvar(samples, skipna=False)
np.testing.assert_almost_equal(actual_variance, np.nan, decimal=2)
tm.assert_almost_equal(actual_variance, np.nan, check_less_precise=2)

def test_nanstd_nans(self):
samples = np.nan * np.ones(2 * self.samples.shape[0])
samples[::2] = self.samples

actual_std = nanops.nanstd(samples, skipna=True)
np.testing.assert_almost_equal(actual_std, self.variance ** 0.5,
decimal=2)
tm.assert_almost_equal(actual_std, self.variance ** 0.5,
check_less_precise=2)

actual_std = nanops.nanvar(samples, skipna=False)
np.testing.assert_almost_equal(actual_std, np.nan, decimal=2)
tm.assert_almost_equal(actual_std, np.nan,
check_less_precise=2)

def test_nanvar_axis(self):
# Generate some sample data.
Expand All @@ -831,8 +832,8 @@ def test_nanvar_axis(self):
samples = np.vstack([samples_norm, samples_unif])

actual_variance = nanops.nanvar(samples, axis=1)
np.testing.assert_array_almost_equal(actual_variance, np.array(
[self.variance, 1.0 / 12]), decimal=2)
tm.assert_almost_equal(actual_variance, np.array(
[self.variance, 1.0 / 12]), check_less_precise=2)

def test_nanvar_ddof(self):
n = 5
Expand All @@ -845,13 +846,16 @@ def test_nanvar_ddof(self):

# The unbiased estimate.
var = 1.0 / 12
np.testing.assert_almost_equal(variance_1, var, decimal=2)
tm.assert_almost_equal(variance_1, var,
check_less_precise=2)

# The underestimated variance.
np.testing.assert_almost_equal(variance_0, (n - 1.0) / n * var,
decimal=2)
tm.assert_almost_equal(variance_0, (n - 1.0) / n * var,
check_less_precise=2)

# The overestimated variance.
np.testing.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var,
decimal=2)
tm.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var,
check_less_precise=2)

def test_ground_truth(self):
# Test against values that were precomputed with Numpy.
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2301,8 +2301,8 @@ def test_update_raise(self):
[[1.5, np.nan, 3.], [1.5, np.nan, 3.], [1.5, np.nan, 3.],
[1.5, np.nan, 3.]]])

np.testing.assert_raises(Exception, pan.update, *(pan, ),
**{'raise_conflict': True})
self.assertRaises(Exception, pan.update, *(pan, ),
**{'raise_conflict': True})

def test_all_any(self):
self.assertTrue((self.panel.all(axis=0).values == nanall(
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,18 @@ def test_less_precise(self):

self.assertRaises(AssertionError, assert_series_equal, s1, s2)
self._assert_equal(s1, s2, check_less_precise=True)
for i in range(4):
self._assert_equal(s1, s2, check_less_precise=i)
self.assertRaises(AssertionError, assert_series_equal, s1, s2, 10)

s1 = Series([0.12345], dtype='float32')
s2 = Series([0.12346], dtype='float32')

self.assertRaises(AssertionError, assert_series_equal, s1, s2)
self._assert_equal(s1, s2, check_less_precise=True)
for i in range(4):
self._assert_equal(s1, s2, check_less_precise=i)
self.assertRaises(AssertionError, assert_series_equal, s1, s2, 10)

# even less than less precise
s1 = Series([0.1235], dtype='float32')
Expand Down
51 changes: 40 additions & 11 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,40 @@ def assertNotAlmostEquals(self, *args, **kwargs):


def assert_almost_equal(left, right, check_exact=False,
check_dtype='equiv', **kwargs):
check_dtype='equiv', check_less_precise=False,
**kwargs):
"""Check that left and right Index are equal.

Parameters
----------
left : object
right : object
check_exact : bool, default True
Whether to compare number exactly.
check_dtype: bool, default True
check dtype if both a and b are the same type
check_less_precise : bool or int, default False
Specify comparison precision. Only used when check_exact is False.
5 digits (False) or 3 digits (True) after decimal points are compared.
If int, then specify the digits to compare
"""
if isinstance(left, pd.Index):
return assert_index_equal(left, right, check_exact=check_exact,
exact=check_dtype, **kwargs)
exact=check_dtype,
check_less_precise=check_less_precise,
**kwargs)

elif isinstance(left, pd.Series):
return assert_series_equal(left, right, check_exact=check_exact,
check_dtype=check_dtype, **kwargs)
check_dtype=check_dtype,
check_less_precise=check_less_precise,
**kwargs)

elif isinstance(left, pd.DataFrame):
return assert_frame_equal(left, right, check_exact=check_exact,
check_dtype=check_dtype, **kwargs)
check_dtype=check_dtype,
check_less_precise=check_less_precise,
**kwargs)

else:
# other sequences
Expand All @@ -142,8 +164,11 @@ def assert_almost_equal(left, right, check_exact=False,
else:
obj = 'Input'
assert_class_equal(left, right, obj=obj)
return _testing.assert_almost_equal(left, right,
check_dtype=check_dtype, **kwargs)
return _testing.assert_almost_equal(
left, right,
check_dtype=check_dtype,
check_less_precise=check_less_precise,
**kwargs)


def assert_dict_equal(left, right, compare_keys=True):
Expand Down Expand Up @@ -690,9 +715,10 @@ def assert_index_equal(left, right, exact='equiv', check_names=True,
Int64Index as well
check_names : bool, default True
Whether to check the names attribute.
check_less_precise : bool, default False
check_less_precise : bool or int, default False
Specify comparison precision. Only used when check_exact is False.
5 digits (False) or 3 digits (True) after decimal points are compared.
If int, then specify the digits to compare
check_exact : bool, default True
Whether to compare number exactly.
check_categorical : bool, default True
Expand Down Expand Up @@ -1040,9 +1066,10 @@ def assert_series_equal(left, right, check_dtype=True,
are identical.
check_series_type : bool, default False
Whether to check the Series class is identical.
check_less_precise : bool, default False
check_less_precise : bool or int, default False
Specify comparison precision. Only used when check_exact is False.
5 digits (False) or 3 digits (True) after decimal points are compared.
If int, then specify the digits to compare
check_exact : bool, default False
Whether to compare number exactly.
check_names : bool, default True
Expand Down Expand Up @@ -1106,7 +1133,7 @@ def assert_series_equal(left, right, check_dtype=True,
check_dtype=check_dtype)
else:
_testing.assert_almost_equal(left.get_values(), right.get_values(),
check_less_precise,
check_less_precise=check_less_precise,
check_dtype=check_dtype,
obj='{0}'.format(obj))

Expand Down Expand Up @@ -1150,9 +1177,10 @@ def assert_frame_equal(left, right, check_dtype=True,
are identical.
check_frame_type : bool, default False
Whether to check the DataFrame class is identical.
check_less_precise : bool, default False
check_less_precise : bool or it, default False
Specify comparison precision. Only used when check_exact is False.
5 digits (False) or 3 digits (True) after decimal points are compared.
If int, then specify the digits to compare
check_names : bool, default True
Whether to check the Index names attribute.
by_blocks : bool, default False
Expand Down Expand Up @@ -1259,9 +1287,10 @@ def assert_panelnd_equal(left, right,
Whether to check the Panel dtype is identical.
check_panel_type : bool, default False
Whether to check the Panel class is identical.
check_less_precise : bool, default False
check_less_precise : bool or int, default False
Specify comparison precision. Only used when check_exact is False.
5 digits (False) or 3 digits (True) after decimal points are compared.
If int, then specify the digits to compare
assert_func : function for comparing data
check_names : bool, default True
Whether to check the Index names attribute.
Expand Down