Skip to content

Commit 7653af9

Browse files
committed
TST: more strict testing in lint.sh
1 parent c0850ea commit 7653af9

File tree

9 files changed

+90
-45
lines changed

9 files changed

+90
-45
lines changed

ci/lint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if [ "$LINT" ]; then
2020
echo "Linting DONE"
2121

2222
echo "Check for invalid testing"
23-
grep -r --include '*.py' --exclude nosetester.py --exclude testing.py 'numpy.testing' pandas
23+
grep -r -E --include '*.py' --exclude nosetester.py --exclude testing.py '(numpy|np)\.testing' pandas
2424
if [ $? = "0" ]; then
2525
RET=1
2626
fi

pandas/io/tests/json/test_pandas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_frame_non_unique_index(self):
100100
orient='split'))
101101
unser = read_json(df.to_json(orient='records'), orient='records')
102102
self.assert_index_equal(df.columns, unser.columns)
103-
np.testing.assert_equal(df.values, unser.values)
103+
tm.assert_almost_equal(df.values, unser.values)
104104
unser = read_json(df.to_json(orient='values'), orient='values')
105105
tm.assert_numpy_array_equal(df.values, unser.values)
106106

pandas/io/tests/test_packers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -671,14 +671,14 @@ def _test_small_strings_no_warn(self, compress):
671671
with tm.assert_produces_warning(None):
672672
empty_unpacked = self.encode_decode(empty, compress=compress)
673673

674-
np.testing.assert_array_equal(empty_unpacked, empty)
674+
tm.assert_numpy_array_equal(empty_unpacked, empty)
675675
self.assertTrue(empty_unpacked.flags.writeable)
676676

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

681-
np.testing.assert_array_equal(char_unpacked, char)
681+
tm.assert_numpy_array_equal(char_unpacked, char)
682682
self.assertTrue(char_unpacked.flags.writeable)
683683
# if this test fails I am sorry because the interpreter is now in a
684684
# bad state where b'a' points to 98 == ord(b'b').
@@ -688,7 +688,7 @@ def _test_small_strings_no_warn(self, compress):
688688
# always be the same (unless we were able to mutate the shared
689689
# character singleton in which case ord(b'a') == ord(b'b').
690690
self.assertEqual(ord(b'a'), ord(u'a'))
691-
np.testing.assert_array_equal(
691+
tm.assert_numpy_array_equal(
692692
char_unpacked,
693693
np.array([ord(b'b')], dtype='uint8'),
694694
)

pandas/src/testing.pyx

+13-7
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,20 @@ cpdef assert_dict_equal(a, b, bint compare_keys=True):
5555

5656
return True
5757

58-
cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,
58+
cpdef assert_almost_equal(a, b,
59+
check_less_precise=False,
60+
bint check_dtype=True,
5961
obj=None, lobj=None, robj=None):
6062
"""Check that left and right objects are almost equal.
6163
6264
Parameters
6365
----------
6466
a : object
6567
b : object
66-
check_less_precise : bool, default False
68+
check_less_precise : bool or int, default False
6769
Specify comparison precision.
6870
5 digits (False) or 3 digits (True) after decimal points are compared.
71+
If an integer, then this will be the number of decimal points to compare
6972
check_dtype: bool, default True
7073
check dtype if both a and b are np.ndarray
7174
obj : str, default None
@@ -91,6 +94,8 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,
9194
if robj is None:
9295
robj = b
9396

97+
assert isinstance(check_less_precise, (int, bool))
98+
9499
if isinstance(a, dict) or isinstance(b, dict):
95100
return assert_dict_equal(a, b)
96101

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

146151
for i in xrange(len(a)):
147152
try:
148-
assert_almost_equal(a[i], b[i], check_less_precise)
153+
assert_almost_equal(a[i], b[i], check_less_precise=check_less_precise)
149154
except AssertionError:
150155
is_unequal = True
151156
diff += 1
@@ -173,11 +178,12 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False, check_dtype=True,
173178
# inf comparison
174179
return True
175180

176-
decimal = 5
177-
178-
# deal with differing dtypes
179-
if check_less_precise:
181+
if check_less_precise is True:
180182
decimal = 3
183+
elif check_less_precise is False:
184+
decimal = 5
185+
else:
186+
decimal = check_less_precise
181187

182188
fa, fb = a, b
183189

pandas/tests/test_algos.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def test_group_var_generic_1d(self):
585585
expected_counts = counts + 3
586586

587587
self.algo(out, counts, values, labels)
588-
np.testing.assert_allclose(out, expected_out, self.rtol)
588+
self.assertTrue(np.allclose(out, expected_out, self.rtol))
589589
tm.assert_numpy_array_equal(counts, expected_counts)
590590

591591
def test_group_var_generic_1d_flat_labels(self):
@@ -601,7 +601,7 @@ def test_group_var_generic_1d_flat_labels(self):
601601

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

604-
np.testing.assert_allclose(out, expected_out, self.rtol)
604+
self.assertTrue(np.allclose(out, expected_out, self.rtol))
605605
tm.assert_numpy_array_equal(counts, expected_counts)
606606

607607
def test_group_var_generic_2d_all_finite(self):
@@ -616,7 +616,7 @@ def test_group_var_generic_2d_all_finite(self):
616616
expected_counts = counts + 2
617617

618618
self.algo(out, counts, values, labels)
619-
np.testing.assert_allclose(out, expected_out, self.rtol)
619+
self.assertTrue(np.allclose(out, expected_out, self.rtol))
620620
tm.assert_numpy_array_equal(counts, expected_counts)
621621

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

637637
self.algo(out, counts, values, labels)
638-
np.testing.assert_allclose(out, expected_out, self.rtol)
638+
tm.assert_almost_equal(out, expected_out, check_less_precise=6)
639639
tm.assert_numpy_array_equal(counts, expected_counts)
640640

641641
def test_group_var_constant(self):

pandas/tests/test_nanops.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -799,30 +799,31 @@ def setUp(self):
799799
def test_nanvar_all_finite(self):
800800
samples = self.samples
801801
actual_variance = nanops.nanvar(samples)
802-
np.testing.assert_almost_equal(actual_variance, self.variance,
803-
decimal=2)
802+
tm.assert_almost_equal(actual_variance, self.variance,
803+
check_less_precise=2)
804804

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

809809
actual_variance = nanops.nanvar(samples, skipna=True)
810-
np.testing.assert_almost_equal(actual_variance, self.variance,
811-
decimal=2)
810+
tm.assert_almost_equal(actual_variance, self.variance,
811+
check_less_precise=2)
812812

813813
actual_variance = nanops.nanvar(samples, skipna=False)
814-
np.testing.assert_almost_equal(actual_variance, np.nan, decimal=2)
814+
tm.assert_almost_equal(actual_variance, np.nan, check_less_precise=2)
815815

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

820820
actual_std = nanops.nanstd(samples, skipna=True)
821-
np.testing.assert_almost_equal(actual_std, self.variance ** 0.5,
822-
decimal=2)
821+
tm.assert_almost_equal(actual_std, self.variance ** 0.5,
822+
check_less_precise=2)
823823

824824
actual_std = nanops.nanvar(samples, skipna=False)
825-
np.testing.assert_almost_equal(actual_std, np.nan, decimal=2)
825+
tm.assert_almost_equal(actual_std, np.nan,
826+
check_less_precise=2)
826827

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

833834
actual_variance = nanops.nanvar(samples, axis=1)
834-
np.testing.assert_array_almost_equal(actual_variance, np.array(
835-
[self.variance, 1.0 / 12]), decimal=2)
835+
tm.assert_almost_equal(actual_variance, np.array(
836+
[self.variance, 1.0 / 12]), check_less_precise=2)
836837

837838
def test_nanvar_ddof(self):
838839
n = 5
@@ -845,13 +846,16 @@ def test_nanvar_ddof(self):
845846

846847
# The unbiased estimate.
847848
var = 1.0 / 12
848-
np.testing.assert_almost_equal(variance_1, var, decimal=2)
849+
tm.assert_almost_equal(variance_1, var,
850+
check_less_precise=2)
851+
849852
# The underestimated variance.
850-
np.testing.assert_almost_equal(variance_0, (n - 1.0) / n * var,
851-
decimal=2)
853+
tm.assert_almost_equal(variance_0, (n - 1.0) / n * var,
854+
check_less_precise=2)
855+
852856
# The overestimated variance.
853-
np.testing.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var,
854-
decimal=2)
857+
tm.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var,
858+
check_less_precise=2)
855859

856860
def test_ground_truth(self):
857861
# Test against values that were precomputed with Numpy.

pandas/tests/test_panel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2301,8 +2301,8 @@ def test_update_raise(self):
23012301
[[1.5, np.nan, 3.], [1.5, np.nan, 3.], [1.5, np.nan, 3.],
23022302
[1.5, np.nan, 3.]]])
23032303

2304-
np.testing.assert_raises(Exception, pan.update, *(pan, ),
2305-
**{'raise_conflict': True})
2304+
self.assertRaises(Exception, pan.update, *(pan, ),
2305+
**{'raise_conflict': True})
23062306

23072307
def test_all_any(self):
23082308
self.assertTrue((self.panel.all(axis=0).values == nanall(

pandas/tests/test_testing.py

+6
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,18 @@ def test_less_precise(self):
519519

520520
self.assertRaises(AssertionError, assert_series_equal, s1, s2)
521521
self._assert_equal(s1, s2, check_less_precise=True)
522+
for i in range(4):
523+
self._assert_equal(s1, s2, check_less_precise=i)
524+
self.assertRaises(AssertionError, assert_series_equal, s1, s2, 10)
522525

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

526529
self.assertRaises(AssertionError, assert_series_equal, s1, s2)
527530
self._assert_equal(s1, s2, check_less_precise=True)
531+
for i in range(4):
532+
self._assert_equal(s1, s2, check_less_precise=i)
533+
self.assertRaises(AssertionError, assert_series_equal, s1, s2, 10)
528534

529535
# even less than less precise
530536
s1 = Series([0.1235], dtype='float32')

pandas/util/testing.py

+40-11
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,40 @@ def assertNotAlmostEquals(self, *args, **kwargs):
116116

117117

118118
def assert_almost_equal(left, right, check_exact=False,
119-
check_dtype='equiv', **kwargs):
119+
check_dtype='equiv', check_less_precise=False,
120+
**kwargs):
121+
"""Check that left and right Index are equal.
122+
123+
Parameters
124+
----------
125+
left : object
126+
right : object
127+
check_exact : bool, default True
128+
Whether to compare number exactly.
129+
check_dtype: bool, default True
130+
check dtype if both a and b are the same type
131+
check_less_precise : bool or int, default False
132+
Specify comparison precision. Only used when check_exact is False.
133+
5 digits (False) or 3 digits (True) after decimal points are compared.
134+
If int, then specify the digits to compare
135+
"""
120136
if isinstance(left, pd.Index):
121137
return assert_index_equal(left, right, check_exact=check_exact,
122-
exact=check_dtype, **kwargs)
138+
exact=check_dtype,
139+
check_less_precise=check_less_precise,
140+
**kwargs)
123141

124142
elif isinstance(left, pd.Series):
125143
return assert_series_equal(left, right, check_exact=check_exact,
126-
check_dtype=check_dtype, **kwargs)
144+
check_dtype=check_dtype,
145+
check_less_precise=check_less_precise,
146+
**kwargs)
127147

128148
elif isinstance(left, pd.DataFrame):
129149
return assert_frame_equal(left, right, check_exact=check_exact,
130-
check_dtype=check_dtype, **kwargs)
150+
check_dtype=check_dtype,
151+
check_less_precise=check_less_precise,
152+
**kwargs)
131153

132154
else:
133155
# other sequences
@@ -142,8 +164,11 @@ def assert_almost_equal(left, right, check_exact=False,
142164
else:
143165
obj = 'Input'
144166
assert_class_equal(left, right, obj=obj)
145-
return _testing.assert_almost_equal(left, right,
146-
check_dtype=check_dtype, **kwargs)
167+
return _testing.assert_almost_equal(
168+
left, right,
169+
check_dtype=check_dtype,
170+
check_less_precise=check_less_precise,
171+
**kwargs)
147172

148173

149174
def assert_dict_equal(left, right, compare_keys=True):
@@ -690,9 +715,10 @@ def assert_index_equal(left, right, exact='equiv', check_names=True,
690715
Int64Index as well
691716
check_names : bool, default True
692717
Whether to check the names attribute.
693-
check_less_precise : bool, default False
718+
check_less_precise : bool or int, default False
694719
Specify comparison precision. Only used when check_exact is False.
695720
5 digits (False) or 3 digits (True) after decimal points are compared.
721+
If int, then specify the digits to compare
696722
check_exact : bool, default True
697723
Whether to compare number exactly.
698724
check_categorical : bool, default True
@@ -1040,9 +1066,10 @@ def assert_series_equal(left, right, check_dtype=True,
10401066
are identical.
10411067
check_series_type : bool, default False
10421068
Whether to check the Series class is identical.
1043-
check_less_precise : bool, default False
1069+
check_less_precise : bool or int, default False
10441070
Specify comparison precision. Only used when check_exact is False.
10451071
5 digits (False) or 3 digits (True) after decimal points are compared.
1072+
If int, then specify the digits to compare
10461073
check_exact : bool, default False
10471074
Whether to compare number exactly.
10481075
check_names : bool, default True
@@ -1106,7 +1133,7 @@ def assert_series_equal(left, right, check_dtype=True,
11061133
check_dtype=check_dtype)
11071134
else:
11081135
_testing.assert_almost_equal(left.get_values(), right.get_values(),
1109-
check_less_precise,
1136+
check_less_precise=check_less_precise,
11101137
check_dtype=check_dtype,
11111138
obj='{0}'.format(obj))
11121139

@@ -1150,9 +1177,10 @@ def assert_frame_equal(left, right, check_dtype=True,
11501177
are identical.
11511178
check_frame_type : bool, default False
11521179
Whether to check the DataFrame class is identical.
1153-
check_less_precise : bool, default False
1180+
check_less_precise : bool or it, default False
11541181
Specify comparison precision. Only used when check_exact is False.
11551182
5 digits (False) or 3 digits (True) after decimal points are compared.
1183+
If int, then specify the digits to compare
11561184
check_names : bool, default True
11571185
Whether to check the Index names attribute.
11581186
by_blocks : bool, default False
@@ -1259,9 +1287,10 @@ def assert_panelnd_equal(left, right,
12591287
Whether to check the Panel dtype is identical.
12601288
check_panel_type : bool, default False
12611289
Whether to check the Panel class is identical.
1262-
check_less_precise : bool, default False
1290+
check_less_precise : bool or int, default False
12631291
Specify comparison precision. Only used when check_exact is False.
12641292
5 digits (False) or 3 digits (True) after decimal points are compared.
1293+
If int, then specify the digits to compare
12651294
assert_func : function for comparing data
12661295
check_names : bool, default True
12671296
Whether to check the Index names attribute.

0 commit comments

Comments
 (0)