Skip to content
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
Contributor 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
Contributor 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
Contributor 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 .

Copy link
Member

Choose a reason for hiding this comment

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

@Aniketsy yes, please add it as an additional test, and keep the original one you added (because we want to cover the case of the bug report #61935, where both left and right are CategoricalIndex)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jorisvandenbossche Should I keep these as two separate tests for clarity, or merge them into one parametrized test?


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