Skip to content

Commit e6988c1

Browse files
committed
more precise checks
1 parent cc624ed commit e6988c1

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

pandas/_testing/asserters.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,22 @@ def _check_types(left, right, obj="Index") -> None:
316316
assert_class_equal(left, right, exact=exact, obj=obj)
317317

318318
# Skip exact dtype checking when `check_categorical` is False
319-
if check_categorical:
320-
assert_attr_equal("dtype", left, right, obj=obj)
321-
if is_categorical_dtype(left.dtype) and is_categorical_dtype(right.dtype):
319+
if is_categorical_dtype(left.dtype) and is_categorical_dtype(right.dtype):
320+
if not check_categorical:
321+
assert_attr_equal("dtype", left, right, obj=obj)
322322
assert_index_equal(left.categories, right.categories, exact=exact)
323+
else:
324+
assert_attr_equal("inferred_type", left, right, obj=obj)
325+
return
323326

324327
# allow string-like to have different inferred_types
325328
if left.inferred_type in ("string"):
326329
assert right.inferred_type in ("string")
327330
else:
328331
assert_attr_equal("inferred_type", left, right, obj=obj)
329332

333+
assert_attr_equal("dtype", left, right, obj=obj)
334+
330335
def _get_ilevel_values(index, level):
331336
# accept level number only
332337
unique = index.levels[level]
@@ -437,6 +442,8 @@ def assert_class_equal(left, right, exact: bool | str = True, obj="Input"):
437442
"""
438443
Checks classes are equal.
439444
"""
445+
from pandas.core.indexes.numeric import NumericIndex
446+
440447
__tracebackhide__ = True
441448

442449
def repr_class(x):
@@ -450,14 +457,12 @@ def repr_class(x):
450457
return
451458

452459
if exact == "equiv":
453-
# allow equivalence of Int64Index/RangeIndex
454-
types = {type(left).__name__, type(right).__name__}
455-
if len(types - {"Int64Index", "RangeIndex"}):
456-
msg = f"{obj} classes are not equivalent"
457-
raise_assert_detail(obj, msg, repr_class(left), repr_class(right))
458-
elif exact:
459-
msg = f"{obj} classes are different"
460-
raise_assert_detail(obj, msg, repr_class(left), repr_class(right))
460+
# accept equivalence of all NumericIndex (sub-)classes
461+
if isinstance(left, NumericIndex) and isinstance(right, NumericIndex):
462+
return
463+
464+
msg = f"{obj} classes are different"
465+
raise_assert_detail(obj, msg, repr_class(left), repr_class(right))
461466

462467

463468
def assert_attr_equal(attr: str, left, right, obj: str = "Attributes"):

0 commit comments

Comments
 (0)