diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 31f38ef0dca7b..98b91bf4a152c 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -933,6 +933,7 @@ Bug fixes Categorical ^^^^^^^^^^^ - Bug in :func:`Series.apply` where ``nan`` was ignored for :class:`CategoricalDtype` (:issue:`59938`) +- Bug in :func:`testing.assert_index_equal` raising ``TypeError`` instead of ``AssertionError`` for incomparable ``CategoricalIndex`` when ``check_categorical=True`` and ``exact=False`` (:issue:`61935`) - Bug in :meth:`Categorical.astype` where ``copy=False`` would still trigger a copy of the codes (:issue:`62000`) - Bug in :meth:`DataFrame.pivot` and :meth:`DataFrame.set_index` raising an ``ArrowNotImplementedError`` for columns with pyarrow dictionary dtype (:issue:`53051`) - Bug in :meth:`Series.convert_dtypes` with ``dtype_backend="pyarrow"`` where empty :class:`CategoricalDtype` :class:`Series` raised an error or got converted to ``null[pyarrow]`` (:issue:`59934`) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 2e5f8c372f7ef..c8f3bb6bd77d2 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -325,7 +325,17 @@ def _check_types(left, right, obj: str = "Index") -> None: # skip exact index checking when `check_categorical` is False elif check_exact and check_categorical: if not left.equals(right): - mismatch = left._values != right._values + # _values compare can raise TypeError (non-comparable + # categoricals (GH#61935) + try: + mismatch = left._values != right._values + except TypeError: + raise_assert_detail( + obj, + "types are not comparable (non-matching categorical categories)", + left, + right, + ) if not isinstance(mismatch, np.ndarray): mismatch = cast("ExtensionArray", mismatch).fillna(True) diff --git a/pandas/tests/util/test_assert_index_equal.py b/pandas/tests/util/test_assert_index_equal.py index ab52d6c8e9f39..8baabe97a3219 100644 --- a/pandas/tests/util/test_assert_index_equal.py +++ b/pandas/tests/util/test_assert_index_equal.py @@ -317,3 +317,11 @@ def test_assert_multi_index_dtype_check_categorical(check_categorical): tm.assert_index_equal(idx1, idx2, check_categorical=check_categorical) else: tm.assert_index_equal(idx1, idx2, check_categorical=check_categorical) + + +def test_assert_index_equal_categorical_incomparable_categories(): + # GH#61935 + left = Index([1, 2, 3], name="a", dtype="category") + right = Index([1, 2, 6], name="a", dtype="category") + with pytest.raises(AssertionError, match="types are not comparable"): + tm.assert_index_equal(left, right, check_categorical=True, exact=False)