From 74f50edd2ca41fce3d9f9c9c764c733d7ba53164 Mon Sep 17 00:00:00 2001 From: tp Date: Sat, 12 Jun 2021 00:32:25 +0100 Subject: [PATCH 1/2] CLN: assert_index_equal & assert_class_equal --- pandas/_testing/asserters.py | 36 ++++++++++---------- pandas/tests/util/test_assert_index_equal.py | 23 ++++++++++--- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 1942e07d1b562..3f7e48c5a3398 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -316,16 +316,15 @@ def _check_types(left, right, obj="Index") -> None: assert_class_equal(left, right, exact=exact, obj=obj) # Skip exact dtype checking when `check_categorical` is False - if check_categorical: - assert_attr_equal("dtype", left, right, obj=obj) - if is_categorical_dtype(left.dtype) and is_categorical_dtype(right.dtype): + if is_categorical_dtype(left.dtype) and is_categorical_dtype(right.dtype): + if check_categorical: + assert_attr_equal("dtype", left, right, obj=obj) assert_index_equal(left.categories, right.categories, exact=exact) - - # allow string-like to have different inferred_types - if left.inferred_type in ("string"): - assert right.inferred_type in ("string") - else: assert_attr_equal("inferred_type", left, right, obj=obj) + return + + assert_attr_equal("inferred_type", left, right, obj=obj) + assert_attr_equal("dtype", left, right, obj=obj) def _get_ilevel_values(index, level): # accept level number only @@ -437,6 +436,8 @@ def assert_class_equal(left, right, exact: bool | str = True, obj="Input"): """ Checks classes are equal. """ + from pandas.core.indexes.numeric import NumericIndex + __tracebackhide__ = True def repr_class(x): @@ -446,17 +447,16 @@ def repr_class(x): return type(x).__name__ + if type(left) == type(right): + return + if exact == "equiv": - if type(left) != type(right): - # allow equivalence of Int64Index/RangeIndex - types = {type(left).__name__, type(right).__name__} - if len(types - {"Int64Index", "RangeIndex"}): - msg = f"{obj} classes are not equivalent" - raise_assert_detail(obj, msg, repr_class(left), repr_class(right)) - elif exact: - if type(left) != type(right): - msg = f"{obj} classes are different" - raise_assert_detail(obj, msg, repr_class(left), repr_class(right)) + # accept equivalence of NumericIndex (sub-)classes + if isinstance(left, NumericIndex) and isinstance(right, NumericIndex): + return + + msg = f"{obj} classes are different" + raise_assert_detail(obj, msg, repr_class(left), repr_class(right)) def assert_attr_equal(attr: str, left, right, obj: str = "Attributes"): diff --git a/pandas/tests/util/test_assert_index_equal.py b/pandas/tests/util/test_assert_index_equal.py index 1778b6fb9d832..8211b52fed650 100644 --- a/pandas/tests/util/test_assert_index_equal.py +++ b/pandas/tests/util/test_assert_index_equal.py @@ -58,15 +58,30 @@ def test_index_equal_length_mismatch(check_exact): tm.assert_index_equal(idx1, idx2, check_exact=check_exact) -def test_index_equal_class_mismatch(check_exact): - msg = """Index are different +@pytest.mark.parametrize("exact", [False, "equiv"]) +def test_index_equal_class(exact): + idx1 = Index([0, 1, 2]) + idx2 = RangeIndex(3) + + tm.assert_index_equal(idx1, idx2, exact=exact) + + +@pytest.mark.parametrize( + "idx_values, msg_str", + [ + [[1, 2, 3.0], "Float64Index\\(\\[1\\.0, 2\\.0, 3\\.0\\], dtype='float64'\\)"], + [range(3), "RangeIndex\\(start=0, stop=3, step=1\\)"], + ], +) +def test_index_equal_class_mismatch(check_exact, idx_values, msg_str): + msg = f"""Index are different Index classes are different \\[left\\]: Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\) -\\[right\\]: Float64Index\\(\\[1\\.0, 2\\.0, 3\\.0\\], dtype='float64'\\)""" +\\[right\\]: {msg_str}""" idx1 = Index([1, 2, 3]) - idx2 = Index([1, 2, 3.0]) + idx2 = Index(idx_values) with pytest.raises(AssertionError, match=msg): tm.assert_index_equal(idx1, idx2, exact=True, check_exact=check_exact) From c34ae66bada72aaa2c7d845e9116ae848ce8c70b Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 13 Jun 2021 10:58:59 +0100 Subject: [PATCH 2/2] move inferred_type check --- pandas/_testing/asserters.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 3f7e48c5a3398..a29767153b021 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -314,16 +314,15 @@ def _check_types(left, right, obj="Index") -> None: return assert_class_equal(left, right, exact=exact, obj=obj) + assert_attr_equal("inferred_type", left, right, obj=obj) # Skip exact dtype checking when `check_categorical` is False if is_categorical_dtype(left.dtype) and is_categorical_dtype(right.dtype): if check_categorical: assert_attr_equal("dtype", left, right, obj=obj) assert_index_equal(left.categories, right.categories, exact=exact) - assert_attr_equal("inferred_type", left, right, obj=obj) return - assert_attr_equal("inferred_type", left, right, obj=obj) assert_attr_equal("dtype", left, right, obj=obj) def _get_ilevel_values(index, level):