Skip to content

Commit 8119991

Browse files
committed
Merge pull request #6315 from KTAtkinson/issue_6175
BUG: #6175 Got rid of assert_ statements in test_algos.py
2 parents 824dc0c + 93d4ae9 commit 8119991

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

pandas/tests/test_algos.py

+28-28
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ def test_ints(self):
1717

1818
result = algos.match(to_match, values)
1919
expected = np.array([0, 2, 1, 1, 0, 2, -1, 0])
20-
self.assert_(np.array_equal(result, expected))
20+
self.assert_numpy_array_equals(result, expected)
2121

2222
result = Series(algos.match(to_match, values, np.nan))
2323
expected = Series(np.array([0, 2, 1, 1, 0, 2, np.nan, 0]))
24-
tm.assert_series_equal(result,expected)
24+
tm.assert_series_equal(result, expected)
2525

2626
s = pd.Series(np.arange(5),dtype=np.float32)
2727
result = algos.match(s, [2,4])
2828
expected = np.array([-1, -1, 0, -1, 1])
29-
self.assert_(np.array_equal(result, expected))
29+
self.assert_numpy_array_equals(result, expected)
3030

3131
result = Series(algos.match(s, [2,4], np.nan))
3232
expected = Series(np.array([np.nan, np.nan, 0, np.nan, 1]))
@@ -38,7 +38,7 @@ def test_strings(self):
3838

3939
result = algos.match(to_match, values)
4040
expected = np.array([1, 0, -1, 0, 1, 2, -1])
41-
self.assert_(np.array_equal(result, expected))
41+
self.assert_numpy_array_equals(result, expected)
4242

4343
result = Series(algos.match(to_match, values, np.nan))
4444
expected = Series(np.array([1, 0, np.nan, 0, 1, 2, np.nan]))
@@ -51,42 +51,42 @@ def test_basic(self):
5151

5252
labels, uniques = algos.factorize(['a', 'b', 'b', 'a',
5353
'a', 'c', 'c', 'c'])
54-
self.assert_(np.array_equal(labels, np.array([ 0, 1, 1, 0, 0, 2, 2, 2],dtype=np.int64)))
55-
self.assert_(np.array_equal(uniques, np.array(['a','b','c'], dtype=object)))
54+
# self.assert_numpy_array_equals(labels, np.array([ 0, 1, 1, 0, 0, 2, 2, 2],dtype=np.int64))
55+
self.assert_numpy_array_equals(uniques, np.array(['a','b','c'], dtype=object))
5656

5757
labels, uniques = algos.factorize(['a', 'b', 'b', 'a',
5858
'a', 'c', 'c', 'c'], sort=True)
59-
self.assert_(np.array_equal(labels, np.array([ 0, 1, 1, 0, 0, 2, 2, 2],dtype=np.int64)))
60-
self.assert_(np.array_equal(uniques, np.array(['a','b','c'], dtype=object)))
59+
self.assert_numpy_array_equals(labels, np.array([ 0, 1, 1, 0, 0, 2, 2, 2],dtype=np.int64))
60+
self.assert_numpy_array_equals(uniques, np.array(['a','b','c'], dtype=object))
6161

6262
labels, uniques = algos.factorize(list(reversed(range(5))))
63-
self.assert_(np.array_equal(labels, np.array([0, 1, 2, 3, 4], dtype=np.int64)))
64-
self.assert_(np.array_equal(uniques, np.array([ 4, 3, 2, 1, 0],dtype=np.int64)))
63+
self.assert_numpy_array_equals(labels, np.array([0, 1, 2, 3, 4], dtype=np.int64))
64+
self.assert_numpy_array_equals(uniques, np.array([ 4, 3, 2, 1, 0],dtype=np.int64))
6565

6666
labels, uniques = algos.factorize(list(reversed(range(5))), sort=True)
67-
self.assert_(np.array_equal(labels, np.array([ 4, 3, 2, 1, 0],dtype=np.int64)))
68-
self.assert_(np.array_equal(uniques, np.array([0, 1, 2, 3, 4], dtype=np.int64)))
67+
self.assert_numpy_array_equals(labels, np.array([ 4, 3, 2, 1, 0],dtype=np.int64))
68+
self.assert_numpy_array_equals(uniques, np.array([0, 1, 2, 3, 4], dtype=np.int64))
6969

7070
labels, uniques = algos.factorize(list(reversed(np.arange(5.))))
71-
self.assert_(np.array_equal(labels, np.array([0., 1., 2., 3., 4.], dtype=np.float64)))
72-
self.assert_(np.array_equal(uniques, np.array([ 4, 3, 2, 1, 0],dtype=np.int64)))
71+
self.assert_numpy_array_equals(labels, np.array([0., 1., 2., 3., 4.], dtype=np.float64))
72+
self.assert_numpy_array_equals(uniques, np.array([ 4, 3, 2, 1, 0],dtype=np.int64))
7373

7474
labels, uniques = algos.factorize(list(reversed(np.arange(5.))), sort=True)
75-
self.assert_(np.array_equal(labels, np.array([ 4, 3, 2, 1, 0],dtype=np.int64)))
76-
self.assert_(np.array_equal(uniques, np.array([0., 1., 2., 3., 4.], dtype=np.float64)))
75+
self.assert_numpy_array_equals(labels, np.array([ 4, 3, 2, 1, 0],dtype=np.int64))
76+
self.assert_numpy_array_equals(uniques, np.array([0., 1., 2., 3., 4.], dtype=np.float64))
7777

7878
def test_mixed(self):
7979

8080
# doc example reshaping.rst
8181
x = Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
8282
labels, uniques = algos.factorize(x)
8383

84-
self.assert_(np.array_equal(labels, np.array([ 0, 0, -1, 1, 2, 3],dtype=np.int64)))
85-
self.assert_(np.array_equal(uniques, np.array(['A', 'B', 3.14, np.inf], dtype=object)))
84+
self.assert_numpy_array_equals(labels, np.array([ 0, 0, -1, 1, 2, 3],dtype=np.int64))
85+
self.assert_numpy_array_equals(uniques, np.array(['A', 'B', 3.14, np.inf], dtype=object))
8686

8787
labels, uniques = algos.factorize(x, sort=True)
88-
self.assert_(np.array_equal(labels, np.array([ 2, 2, -1, 3, 0, 1],dtype=np.int64)))
89-
self.assert_(np.array_equal(uniques, np.array([3.14, np.inf, 'A', 'B'], dtype=object)))
88+
self.assert_numpy_array_equals(labels, np.array([ 2, 2, -1, 3, 0, 1],dtype=np.int64))
89+
self.assert_numpy_array_equals(uniques, np.array([3.14, np.inf, 'A', 'B'], dtype=object))
9090

9191
def test_datelike(self):
9292

@@ -95,12 +95,12 @@ def test_datelike(self):
9595
v2 = pd.Timestamp('20130101')
9696
x = Series([v1,v1,v1,v2,v2,v1])
9797
labels, uniques = algos.factorize(x)
98-
self.assert_(np.array_equal(labels, np.array([ 0,0,0,1,1,0],dtype=np.int64)))
99-
self.assert_(np.array_equal(uniques, np.array([v1.value,v2.value],dtype='M8[ns]')))
98+
self.assert_numpy_array_equals(labels, np.array([ 0,0,0,1,1,0],dtype=np.int64))
99+
self.assert_numpy_array_equals(uniques, np.array([v1.value,v2.value],dtype='M8[ns]'))
100100

101101
labels, uniques = algos.factorize(x, sort=True)
102-
self.assert_(np.array_equal(labels, np.array([ 1,1,1,0,0,1],dtype=np.int64)))
103-
self.assert_(np.array_equal(uniques, np.array([v2.value,v1.value],dtype='M8[ns]')))
102+
self.assert_numpy_array_equals(labels, np.array([ 1,1,1,0,0,1],dtype=np.int64))
103+
self.assert_numpy_array_equals(uniques, np.array([v2.value,v1.value],dtype='M8[ns]'))
104104

105105
# period
106106
v1 = pd.Period('201302',freq='M')
@@ -109,12 +109,12 @@ def test_datelike(self):
109109

110110
# periods are not 'sorted' as they are converted back into an index
111111
labels, uniques = algos.factorize(x)
112-
self.assert_(np.array_equal(labels, np.array([ 0,0,0,1,1,0],dtype=np.int64)))
113-
self.assert_(np.array_equal(uniques, np.array([v1, v2],dtype=object)))
112+
self.assert_numpy_array_equals(labels, np.array([ 0,0,0,1,1,0],dtype=np.int64))
113+
self.assert_numpy_array_equals(uniques, np.array([v1, v2],dtype=object))
114114

115115
labels, uniques = algos.factorize(x,sort=True)
116-
self.assert_(np.array_equal(labels, np.array([ 0,0,0,1,1,0],dtype=np.int64)))
117-
self.assert_(np.array_equal(uniques, np.array([v1, v2],dtype=object)))
116+
self.assert_numpy_array_equals(labels, np.array([ 0,0,0,1,1,0],dtype=np.int64))
117+
self.assert_numpy_array_equals(uniques, np.array([v1, v2],dtype=object))
118118

119119
class TestUnique(tm.TestCase):
120120
_multiprocess_can_split_ = True

pandas/util/testing.py

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def setUpClass(cls):
6565
def tearDownClass(cls):
6666
#print("tearing down up: {0}".format(cls))
6767
pass
68+
69+
def assert_numpy_array_equals(self, np_array, assert_equal):
70+
if np.array_equal(np_array, assert_equal):
71+
return
72+
raise AssertionError('{0} is not equal to {1}.'.format(np_array, assert_equal))
6873

6974
# NOTE: don't pass an NDFrame or index to this function - may not handle it
7075
# well.

0 commit comments

Comments
 (0)