Skip to content

Fix obj arguments in assertions #59460

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

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 8 additions & 5 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def assert_index_equal(
check_order: bool = True,
rtol: float = 1.0e-5,
atol: float = 1.0e-8,
obj: str = "Index",
obj: str | None = None,
) -> None:
"""
Check that left and right Index are equal.
Expand Down Expand Up @@ -217,7 +217,7 @@ def assert_index_equal(
Relative tolerance. Only used when check_exact is False.
atol : float, default 1e-8
Absolute tolerance. Only used when check_exact is False.
obj : str, default 'Index'
obj : str, default 'Index' or 'MultiIndex'
Specify object name being compared, internally used to show appropriate
assertion message.

Expand All @@ -235,6 +235,9 @@ def assert_index_equal(
"""
__tracebackhide__ = True

if obj is None:
obj = "MultiIndex" if isinstance(left, MultiIndex) else "Index"

def _check_types(left, right, obj: str = "Index") -> None:
if not exact:
return
Expand Down Expand Up @@ -283,7 +286,7 @@ def _check_types(left, right, obj: str = "Index") -> None:
right = cast(MultiIndex, right)

for level in range(left.nlevels):
lobj = f"MultiIndex level [{level}]"
lobj = f"{obj} level [{level}]"
try:
# try comparison on levels/codes to avoid densifying MultiIndex
assert_index_equal(
Expand Down Expand Up @@ -314,7 +317,7 @@ def _check_types(left, right, obj: str = "Index") -> None:
obj=lobj,
)
# get_level_values may change dtype
_check_types(left.levels[level], right.levels[level], obj=obj)
_check_types(left.levels[level], right.levels[level], obj=lobj)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't manage to write a test for this (type seems to be already checked just above), which, given its comment, smells very corner-case-y. Probably it's better to stop the head-banging, unless you have ideas on a test case, of course


# skip exact index checking when `check_categorical` is False
elif check_exact and check_categorical:
Expand Down Expand Up @@ -527,7 +530,7 @@ def assert_interval_array_equal(
kwargs["check_freq"] = False

assert_equal(left._left, right._left, obj=f"{obj}.left", **kwargs)
assert_equal(left._right, right._right, obj=f"{obj}.left", **kwargs)
assert_equal(left._right, right._right, obj=f"{obj}.right", **kwargs)

assert_attr_equal("closed", left, right, obj=obj)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_frame_equal_shape_mismatch(df1, df2, frame_or_series):
DataFrame.from_records(
{"a": [1.0, 2.0], "b": [2.1, 1.5], "c": ["l1", "l2"]}, index=["a", "b"]
),
"MultiIndex level \\[0\\] are different",
"DataFrame\\.index level \\[0\\] are different",
),
],
)
Expand Down
21 changes: 20 additions & 1 deletion pandas/tests/util/test_assert_interval_array_equal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import pytest

from pandas import interval_range
from pandas import (
Interval,
interval_range,
)
import pandas._testing as tm
from pandas.arrays import IntervalArray


@pytest.mark.parametrize(
Expand Down Expand Up @@ -79,3 +83,18 @@ def test_interval_array_equal_start_mismatch():

with pytest.raises(AssertionError, match=msg):
tm.assert_interval_array_equal(arr1, arr2)


def test_interval_array_equal_end_mismatch_only():
arr1 = IntervalArray([Interval(0, 1), Interval(0, 5)])
arr2 = IntervalArray([Interval(0, 1), Interval(0, 6)])

msg = """\
IntervalArray.right are different

IntervalArray.right values are different \\(50.0 %\\)
\\[left\\]: \\[1, 5\\]
\\[right\\]: \\[1, 6\\]"""

with pytest.raises(AssertionError, match=msg):
tm.assert_interval_array_equal(arr1, arr2)
2 changes: 1 addition & 1 deletion pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_less_precise(data1, data2, any_float_dtype, decimals):
DataFrame.from_records(
{"a": [1.0, 2.0], "b": [2.1, 1.5], "c": ["l1", "l2"]}, index=["a", "b"]
).c,
"MultiIndex level \\[0\\] are different",
"Series\\.index level \\[0\\] are different",
),
],
)
Expand Down