Skip to content

BUG: Fix TypeError in assert_index_equal when comparing CategoricalIndex with check_categorical=True and exact=False #61941

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
12 changes: 11 additions & 1 deletion pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,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
try:
mismatch = left._values != right._values
except TypeError:
if hasattr(left, "_internal_get_values") and hasattr(
right, "_internal_get_values"
Copy link
Member

Choose a reason for hiding this comment

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

Can you use isinstance checks to call _internal_get_values if the object is a CategoricalIndex?

Copy link
Author

Choose a reason for hiding this comment

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

Thankyou ! for correcting me . I have updated that

Copy link
Author

Choose a reason for hiding this comment

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

There is one check fail due to:
CategoricalIndex does not have an _internal_get_values() method.
So should i use .value or something else . Please suggest

):
mismatch = (
left._internal_get_values() != right._internal_get_values()
)
else:
mismatch = left.values != right.values

if not isinstance(mismatch, np.ndarray):
mismatch = cast("ExtensionArray", mismatch).fillna(True)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/util/test_assert_index_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,12 @@ 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_mismatch_categories():
# GH#61941
ci1 = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c"], ordered=False)
ci2 = CategoricalIndex(["a", "x", "c"], categories=["a", "b", "c"], ordered=False)
Copy link
Member

Choose a reason for hiding this comment

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

Can you test when 1 index is CategoricalIndex and the other is just an Index?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I had meant an additional test in addition to the one you had

Copy link
Author

Choose a reason for hiding this comment

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

Please , let me know if , I need to add an additional test .


with pytest.raises(AssertionError, match="Index are different"):
tm.assert_index_equal(ci1, ci2, check_exact=False, check_categorical=True)
Loading