Skip to content

Commit 81172ce

Browse files
committed
TST: fix assert_categorical_equal message
1 parent 43989fd commit 81172ce

File tree

4 files changed

+65
-30
lines changed

4 files changed

+65
-30
lines changed

pandas/core/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ def __setstate__(self, state):
985985

986986
# Provide compatibility with pre-0.15.0 Categoricals.
987987
if '_codes' not in state and 'labels' in state:
988-
state['_codes'] = state.pop('labels')
988+
state['_codes'] = state.pop('labels').astype(np.int8)
989989
if '_categories' not in state and '_levels' in state:
990990
state['_categories'] = self._validate_categories(state.pop(
991991
'_levels'))

pandas/tests/series/test_datetime_values.py

-2
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,6 @@ def test_strftime(self):
320320
expected = np.array(['2015/03/01', '2015/03/02', '2015/03/03',
321321
'2015/03/04', '2015/03/05'], dtype=np.object_)
322322
# dtype may be S10 or U10 depending on python version
323-
print(result)
324-
print(expected)
325323
self.assert_numpy_array_equal(result, expected, check_dtype=False)
326324

327325
period_index = period_range('20150301', periods=5)

pandas/tests/test_testing.py

+55-17
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ def test_assert_almost_equal_dicts(self):
6565
self._assert_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
6666

6767
self._assert_not_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 3})
68-
self._assert_not_almost_equal_both(
69-
{'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3}
70-
)
68+
self._assert_not_almost_equal_both({'a': 1, 'b': 2},
69+
{'a': 1, 'b': 2, 'c': 3})
7170
self._assert_not_almost_equal_both({'a': 1}, 1)
7271
self._assert_not_almost_equal_both({'a': 1}, 'abc')
7372
self._assert_not_almost_equal_both({'a': 1}, [1, ])
@@ -215,11 +214,11 @@ def test_numpy_array_equal_message(self):
215214
\\[right\\]: \\[1\\.0, nan, 3\\.0\\]"""
216215

217216
with assertRaisesRegexp(AssertionError, expected):
218-
assert_numpy_array_equal(
219-
np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))
217+
assert_numpy_array_equal(np.array([np.nan, 2, 3]),
218+
np.array([1, np.nan, 3]))
220219
with assertRaisesRegexp(AssertionError, expected):
221-
assert_almost_equal(
222-
np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))
220+
assert_almost_equal(np.array([np.nan, 2, 3]),
221+
np.array([1, np.nan, 3]))
223222

224223
expected = """numpy array are different
225224
@@ -339,8 +338,8 @@ def test_index_equal_message(self):
339338
labels=\\[\\[0, 0, 1, 1\\], \\[0, 1, 2, 3\\]\\]\\)"""
340339

341340
idx1 = pd.Index([1, 2, 3])
342-
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 3), ('B', 4
343-
)])
341+
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
342+
('B', 3), ('B', 4)])
344343
with assertRaisesRegexp(AssertionError, expected):
345344
assert_index_equal(idx1, idx2, exact=False)
346345

@@ -350,10 +349,10 @@ def test_index_equal_message(self):
350349
\\[left\\]: Int64Index\\(\\[2, 2, 3, 4\\], dtype='int64'\\)
351350
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
352351

353-
idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2), ('B', 3), ('B', 4
354-
)])
355-
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 3), ('B', 4
356-
)])
352+
idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2),
353+
('B', 3), ('B', 4)])
354+
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
355+
('B', 3), ('B', 4)])
357356
with assertRaisesRegexp(AssertionError, expected):
358357
assert_index_equal(idx1, idx2)
359358
with assertRaisesRegexp(AssertionError, expected):
@@ -434,10 +433,10 @@ def test_index_equal_message(self):
434433
\\[left\\]: Int64Index\\(\\[2, 2, 3, 4\\], dtype='int64'\\)
435434
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
436435

437-
idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2), ('B', 3), ('B', 4
438-
)])
439-
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 3), ('B', 4
440-
)])
436+
idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2),
437+
('B', 3), ('B', 4)])
438+
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
439+
('B', 3), ('B', 4)])
441440
with assertRaisesRegexp(AssertionError, expected):
442441
assert_index_equal(idx1, idx2)
443442
with assertRaisesRegexp(AssertionError, expected):
@@ -674,6 +673,45 @@ def test_notisinstance(self):
674673
tm.assertNotIsInstance(pd.Series([1]), pd.Series)
675674

676675

676+
class TestAssertCategoricalEqual(unittest.TestCase):
677+
_multiprocess_can_split_ = True
678+
679+
def test_categorical_equal_message(self):
680+
681+
expected = """Categorical\\.categories are different
682+
683+
Categorical\\.categories values are different \\(25\\.0 %\\)
684+
\\[left\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
685+
\\[right\\]: Int64Index\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""
686+
687+
a = pd.Categorical([1, 2, 3, 4])
688+
b = pd.Categorical([1, 2, 3, 5])
689+
with assertRaisesRegexp(AssertionError, expected):
690+
tm.assert_categorical_equal(a, b)
691+
692+
expected = """Categorical\\.codes are different
693+
694+
Categorical\\.codes values are different \\(50\\.0 %\\)
695+
\\[left\\]: \\[0, 1, 3, 2\\]
696+
\\[right\\]: \\[0, 1, 2, 3\\]"""
697+
698+
a = pd.Categorical([1, 2, 4, 3], categories=[1, 2, 3, 4])
699+
b = pd.Categorical([1, 2, 3, 4], categories=[1, 2, 3, 4])
700+
with assertRaisesRegexp(AssertionError, expected):
701+
tm.assert_categorical_equal(a, b)
702+
703+
expected = """Categorical are different
704+
705+
Attribute "ordered" are different
706+
\\[left\\]: False
707+
\\[right\\]: True"""
708+
709+
a = pd.Categorical([1, 2, 3, 4], ordered=False)
710+
b = pd.Categorical([1, 2, 3, 4], ordered=True)
711+
with assertRaisesRegexp(AssertionError, expected):
712+
tm.assert_categorical_equal(a, b)
713+
714+
677715
class TestRNGContext(unittest.TestCase):
678716

679717
def test_RNGContext(self):

pandas/util/testing.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -903,18 +903,17 @@ def assertNotIsInstance(obj, cls, msg=''):
903903
raise AssertionError(err_msg.format(msg, cls))
904904

905905

906-
def assert_categorical_equal(res, exp):
907-
assertIsInstance(res, pd.Categorical, '[Categorical] ')
908-
assertIsInstance(exp, pd.Categorical, '[Categorical] ')
906+
def assert_categorical_equal(left, right, check_dtype=True,
907+
obj='Categorical'):
908+
assertIsInstance(left, pd.Categorical, '[Categorical] ')
909+
assertIsInstance(right, pd.Categorical, '[Categorical] ')
909910

910-
assert_index_equal(res.categories, exp.categories)
911+
assert_index_equal(left.categories, right.categories,
912+
obj='{0}.categories'.format(obj))
913+
assert_numpy_array_equal(left.codes, right.codes, check_dtype=check_dtype,
914+
obj='{0}.codes'.format(obj))
911915

912-
if not array_equivalent(res.codes, exp.codes):
913-
raise AssertionError(
914-
'codes not equivalent: {0} vs {1}.'.format(res.codes, exp.codes))
915-
916-
if res.ordered != exp.ordered:
917-
raise AssertionError("ordered not the same")
916+
assert_attr_equal('ordered', left, right, obj=obj)
918917

919918

920919
def raise_assert_detail(obj, message, left, right):

0 commit comments

Comments
 (0)