Skip to content

Commit b99e6d2

Browse files
committed
BUG: assert_index_equal CategoricalIndex incomparable categories gives AssertionError
1 parent 9012972 commit b99e6d2

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
@@ -936,7 +936,8 @@ Categorical
936936
- Bug in :meth:`Categorical.astype` where ``copy=False`` would still trigger a copy of the codes (:issue:`62000`)
937937
- Bug in :meth:`DataFrame.pivot` and :meth:`DataFrame.set_index` raising an ``ArrowNotImplementedError`` for columns with pyarrow dictionary dtype (:issue:`53051`)
938938
- 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`)
939-
-
939+
- Bug in :func:`testing.assert_index_equal` raising ``TypeError`` instead of ``AssertionError`` for incomparable ``CategoricalIndex`` when ``check_categorical=True`` and ``exact=False`` (:issue:`61935`)
940+
-
940941

941942
Datetimelike
942943
^^^^^^^^^^^^

pandas/_testing/asserters.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import pandas as pd
3939
from pandas import (
4040
Categorical,
41-
CategoricalIndex,
4241
DataFrame,
4342
DatetimeIndex,
4443
Index,
@@ -326,15 +325,16 @@ def _check_types(left, right, obj: str = "Index") -> None:
326325
# skip exact index checking when `check_categorical` is False
327326
elif check_exact and check_categorical:
328327
if not left.equals(right):
328+
# _values compare can raise TypeError for non-comparable categoricals (GH#61935)
329329
try:
330330
mismatch = left._values != right._values
331331
except TypeError:
332-
if isinstance(left, CategoricalIndex) and isinstance(
333-
right, CategoricalIndex
334-
):
335-
mismatch = left.codes != right.codes
336-
else:
337-
mismatch = left.values != right.values
332+
raise_assert_detail(
333+
obj,
334+
"types are not comparable (non-matching categorical categories)",
335+
left,
336+
right,
337+
)
338338

339339
if not isinstance(mismatch, np.ndarray):
340340
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)