Skip to content

TST: fix assert_categorical_equal message #13080

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 pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ def __setstate__(self, state):

# Provide compatibility with pre-0.15.0 Categoricals.
if '_codes' not in state and 'labels' in state:
state['_codes'] = state.pop('labels')
state['_codes'] = state.pop('labels').astype(np.int8)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor fix to make dtype compat. Needs whatsnew?

if '_categories' not in state and '_levels' in state:
state['_categories'] = self._validate_categories(state.pop(
'_levels'))
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ def test_strftime(self):
expected = np.array(['2015/03/01', '2015/03/02', '2015/03/03',
'2015/03/04', '2015/03/05'], dtype=np.object_)
# dtype may be S10 or U10 depending on python version
print(result)
print(expected)
self.assert_numpy_array_equal(result, expected, check_dtype=False)

period_index = period_range('20150301', periods=5)
Expand Down
72 changes: 55 additions & 17 deletions pandas/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def test_assert_almost_equal_dicts(self):
self._assert_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 2})

self._assert_not_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 3})
self._assert_not_almost_equal_both(
{'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3}
)
self._assert_not_almost_equal_both({'a': 1, 'b': 2},
{'a': 1, 'b': 2, 'c': 3})
self._assert_not_almost_equal_both({'a': 1}, 1)
self._assert_not_almost_equal_both({'a': 1}, 'abc')
self._assert_not_almost_equal_both({'a': 1}, [1, ])
Expand Down Expand Up @@ -215,11 +214,11 @@ def test_numpy_array_equal_message(self):
\\[right\\]: \\[1\\.0, nan, 3\\.0\\]"""

with assertRaisesRegexp(AssertionError, expected):
assert_numpy_array_equal(
np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))
assert_numpy_array_equal(np.array([np.nan, 2, 3]),
np.array([1, np.nan, 3]))
with assertRaisesRegexp(AssertionError, expected):
assert_almost_equal(
np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))
assert_almost_equal(np.array([np.nan, 2, 3]),
np.array([1, np.nan, 3]))

expected = """numpy array are different

Expand Down Expand Up @@ -339,8 +338,8 @@ def test_index_equal_message(self):
labels=\\[\\[0, 0, 1, 1\\], \\[0, 1, 2, 3\\]\\]\\)"""

idx1 = pd.Index([1, 2, 3])
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 3), ('B', 4
)])
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
('B', 3), ('B', 4)])
with assertRaisesRegexp(AssertionError, expected):
assert_index_equal(idx1, idx2, exact=False)

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

idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2), ('B', 3), ('B', 4
)])
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 3), ('B', 4
)])
idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2),
('B', 3), ('B', 4)])
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
('B', 3), ('B', 4)])
with assertRaisesRegexp(AssertionError, expected):
assert_index_equal(idx1, idx2)
with assertRaisesRegexp(AssertionError, expected):
Expand Down Expand Up @@ -434,10 +433,10 @@ def test_index_equal_message(self):
\\[left\\]: Int64Index\\(\\[2, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""

idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2), ('B', 3), ('B', 4
)])
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 3), ('B', 4
)])
idx1 = pd.MultiIndex.from_tuples([('A', 2), ('A', 2),
('B', 3), ('B', 4)])
idx2 = pd.MultiIndex.from_tuples([('A', 1), ('A', 2),
('B', 3), ('B', 4)])
with assertRaisesRegexp(AssertionError, expected):
assert_index_equal(idx1, idx2)
with assertRaisesRegexp(AssertionError, expected):
Expand Down Expand Up @@ -674,6 +673,45 @@ def test_notisinstance(self):
tm.assertNotIsInstance(pd.Series([1]), pd.Series)


class TestAssertCategoricalEqual(unittest.TestCase):
_multiprocess_can_split_ = True

def test_categorical_equal_message(self):

expected = """Categorical\\.categories are different

Categorical\\.categories values are different \\(25\\.0 %\\)
\\[left\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: Int64Index\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""

a = pd.Categorical([1, 2, 3, 4])
b = pd.Categorical([1, 2, 3, 5])
with assertRaisesRegexp(AssertionError, expected):
tm.assert_categorical_equal(a, b)

expected = """Categorical\\.codes are different

Categorical\\.codes values are different \\(50\\.0 %\\)
\\[left\\]: \\[0, 1, 3, 2\\]
\\[right\\]: \\[0, 1, 2, 3\\]"""

a = pd.Categorical([1, 2, 4, 3], categories=[1, 2, 3, 4])
b = pd.Categorical([1, 2, 3, 4], categories=[1, 2, 3, 4])
with assertRaisesRegexp(AssertionError, expected):
tm.assert_categorical_equal(a, b)

expected = """Categorical are different

Attribute "ordered" are different
\\[left\\]: False
\\[right\\]: True"""

a = pd.Categorical([1, 2, 3, 4], ordered=False)
b = pd.Categorical([1, 2, 3, 4], ordered=True)
with assertRaisesRegexp(AssertionError, expected):
tm.assert_categorical_equal(a, b)


class TestRNGContext(unittest.TestCase):

def test_RNGContext(self):
Expand Down
19 changes: 9 additions & 10 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,18 +903,17 @@ def assertNotIsInstance(obj, cls, msg=''):
raise AssertionError(err_msg.format(msg, cls))


def assert_categorical_equal(res, exp):
assertIsInstance(res, pd.Categorical, '[Categorical] ')
assertIsInstance(exp, pd.Categorical, '[Categorical] ')
def assert_categorical_equal(left, right, check_dtype=True,
obj='Categorical'):
assertIsInstance(left, pd.Categorical, '[Categorical] ')
assertIsInstance(right, pd.Categorical, '[Categorical] ')

assert_index_equal(res.categories, exp.categories)
assert_index_equal(left.categories, right.categories,
obj='{0}.categories'.format(obj))
assert_numpy_array_equal(left.codes, right.codes, check_dtype=check_dtype,
obj='{0}.codes'.format(obj))

if not array_equivalent(res.codes, exp.codes):
raise AssertionError(
'codes not equivalent: {0} vs {1}.'.format(res.codes, exp.codes))

if res.ordered != exp.ordered:
raise AssertionError("ordered not the same")
assert_attr_equal('ordered', left, right, obj=obj)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, why didn't this work before?

Copy link
Member Author

@sinhrks sinhrks May 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is for better error reporting purpose (The validation is the same).

As described in #13076, the check itself is OK but it is not called from Series/Index check, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh I c, ok then.



def raise_assert_detail(obj, message, left, right):
Expand Down