Skip to content

Commit 02ffdfb

Browse files
committed
BUG: assert_index_equal CategoricalIndex incomparable categories gives AssertionError
1 parent f36b1fb commit 02ffdfb

File tree

3 files changed

+15
-30
lines changed

3 files changed

+15
-30
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ Categorical
689689
- Bug in :func:`Series.apply` where ``nan`` was ignored for :class:`CategoricalDtype` (:issue:`59938`)
690690
- Bug in :meth:`DataFrame.pivot` and :meth:`DataFrame.set_index` raising an ``ArrowNotImplementedError`` for columns with pyarrow dictionary dtype (:issue:`53051`)
691691
- 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`)
692-
-
692+
- Bug in :func:`testing.assert_index_equal` raising ``TypeError`` instead of ``AssertionError`` for incomparable ``CategoricalIndex`` when ``check_categorical=True`` and ``exact=False`` (:issue:`61935`)
693+
-
693694

694695
Datetimelike
695696
^^^^^^^^^^^^

pandas/_testing/asserters.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import pandas as pd
3636
from pandas import (
3737
Categorical,
38-
CategoricalIndex,
3938
DataFrame,
4039
DatetimeIndex,
4140
Index,
@@ -323,15 +322,16 @@ def _check_types(left, right, obj: str = "Index") -> None:
323322
# skip exact index checking when `check_categorical` is False
324323
elif check_exact and check_categorical:
325324
if not left.equals(right):
325+
# _values compare can raise TypeError for non-comparable categoricals (GH#61935)
326326
try:
327327
mismatch = left._values != right._values
328328
except TypeError:
329-
if isinstance(left, CategoricalIndex) and isinstance(
330-
right, CategoricalIndex
331-
):
332-
mismatch = left.codes != right.codes
333-
else:
334-
mismatch = left.values != right.values
329+
raise_assert_detail(
330+
obj,
331+
"types are not comparable (non-matching categorical categories)",
332+
left,
333+
right,
334+
)
335335

336336
if not isinstance(mismatch, np.ndarray):
337337
mismatch = cast("ExtensionArray", mismatch).fillna(True)

pandas/tests/util/test_assert_index_equal.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -319,25 +319,9 @@ def test_assert_multi_index_dtype_check_categorical(check_categorical):
319319
tm.assert_index_equal(idx1, idx2, check_categorical=check_categorical)
320320

321321

322-
def test_assert_index_equal_categorical_mismatch_categories():
323-
# GH#61941 - CategoricalIndex vs Index
324-
ci = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c"], ordered=False)
325-
idx = Index(["a", "b", "c"])
326-
327-
with pytest.raises(AssertionError, match="Index are different"):
328-
tm.assert_index_equal(
329-
ci,
330-
idx,
331-
)
332-
333-
334-
def test_assert_categorical_index_equal_mismatch_categories():
335-
# GH#61941 - both left and right are CategoricalIndex
336-
ci1 = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c"], ordered=False)
337-
ci2 = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "d"], ordered=False)
338-
339-
with pytest.raises(AssertionError, match="Index are different"):
340-
tm.assert_index_equal(
341-
ci1,
342-
ci2,
343-
)
322+
def test_assert_index_equal_categorical_incomparable_categories():
323+
# GH#61935
324+
left = Index([1, 2, 3], name="a", dtype="category")
325+
right = Index([1, 2, 6], name="a", dtype="category")
326+
with pytest.raises(AssertionError, match="types are not comparable"):
327+
tm.assert_index_equal(left, right, check_categorical=True, exact=False)

0 commit comments

Comments
 (0)