Skip to content

Commit 5e2197b

Browse files
committed
improvements
1 parent e6988c1 commit 5e2197b

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

pandas/_testing/asserters.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,13 @@ def _check_types(left, right, obj="Index") -> None:
317317

318318
# Skip exact dtype checking when `check_categorical` is False
319319
if is_categorical_dtype(left.dtype) and is_categorical_dtype(right.dtype):
320-
if not check_categorical:
320+
if check_categorical:
321321
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
326-
327-
# allow string-like to have different inferred_types
328-
if left.inferred_type in ("string"):
329-
assert right.inferred_type in ("string")
330-
else:
331323
assert_attr_equal("inferred_type", left, right, obj=obj)
324+
return
332325

326+
assert_attr_equal("inferred_type", left, right, obj=obj)
333327
assert_attr_equal("dtype", left, right, obj=obj)
334328

335329
def _get_ilevel_values(index, level):
@@ -457,7 +451,7 @@ def repr_class(x):
457451
return
458452

459453
if exact == "equiv":
460-
# accept equivalence of all NumericIndex (sub-)classes
454+
# accept equivalence of NumericIndex (sub-)classes
461455
if isinstance(left, NumericIndex) and isinstance(right, NumericIndex):
462456
return
463457

pandas/tests/util/test_assert_index_equal.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,30 @@ def test_index_equal_length_mismatch(check_exact):
5858
tm.assert_index_equal(idx1, idx2, check_exact=check_exact)
5959

6060

61-
def test_index_equal_class_mismatch(check_exact):
62-
msg = """Index are different
61+
@pytest.mark.parametrize("exact", [False, "equiv"])
62+
def test_index_equal_class(exact):
63+
idx1 = Index([0, 1, 2])
64+
idx2 = RangeIndex(3)
65+
66+
tm.assert_index_equal(idx1, idx2, exact=exact)
67+
68+
69+
@pytest.mark.parametrize(
70+
"idx_values, msg_str",
71+
[
72+
[[1, 2, 3.0], "Float64Index\\(\\[1\\.0, 2\\.0, 3\\.0\\], dtype='float64'\\)"],
73+
[range(3), "RangeIndex\\(start=0, stop=3, step=1\\)"],
74+
],
75+
)
76+
def test_index_equal_class_mismatch(check_exact, idx_values, msg_str):
77+
msg = f"""Index are different
6378
6479
Index classes are different
6580
\\[left\\]: Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
66-
\\[right\\]: Float64Index\\(\\[1\\.0, 2\\.0, 3\\.0\\], dtype='float64'\\)"""
81+
\\[right\\]: {msg_str}"""
6782

6883
idx1 = Index([1, 2, 3])
69-
idx2 = Index([1, 2, 3.0])
84+
idx2 = Index(idx_values)
7085

7186
with pytest.raises(AssertionError, match=msg):
7287
tm.assert_index_equal(idx1, idx2, exact=True, check_exact=check_exact)

0 commit comments

Comments
 (0)