Skip to content

ENH: Added index to testing assert message when series values differ #31435

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 7 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 8 additions & 2 deletions pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ cpdef assert_dict_equal(a, b, bint compare_keys=True):
cpdef assert_almost_equal(a, b,
check_less_precise=False,
bint check_dtype=True,
obj=None, lobj=None, robj=None):
obj=None, lobj=None, robj=None, index=None):
"""
Check that left and right objects are almost equal.

Expand All @@ -89,6 +89,12 @@ cpdef assert_almost_equal(a, b,
robj : str, default None
Specify right object name being compared, internally used to show
appropriate assertion message
index : str, default None
Specify shared index of objects being compared, internally used to
show appropriate assertion message

.. versionadded:: 1.1.0

"""
cdef:
int decimal
Expand Down Expand Up @@ -171,7 +177,7 @@ cpdef assert_almost_equal(a, b,
from pandas._testing import raise_assert_detail
msg = (f"{obj} values are different "
f"({np.round(diff * 100.0 / na, 5)} %)")
raise_assert_detail(obj, msg, lobj, robj)
raise_assert_detail(obj, msg, lobj, robj, index=index)

return True

Expand Down
25 changes: 19 additions & 6 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,16 @@ def assert_timedelta_array_equal(left, right, obj="TimedeltaArray"):
assert_attr_equal("freq", left, right, obj=obj)


def raise_assert_detail(obj, message, left, right, diff=None):
def raise_assert_detail(obj, message, left, right, diff=None, index=None):
__tracebackhide__ = True

msg = f"""{obj} are different

{message}"""

if isinstance(index, np.ndarray):
msg += f"\n[index]: {pprint_thing(index)}"

if isinstance(left, np.ndarray):
left = pprint_thing(left)
elif is_categorical_dtype(left):
Expand All @@ -901,9 +908,7 @@ def raise_assert_detail(obj, message, left, right, diff=None):
elif is_categorical_dtype(right):
right = repr(right)

msg = f"""{obj} are different

{message}
msg += f"""
[left]: {left}
[right]: {right}"""

Expand All @@ -921,6 +926,7 @@ def assert_numpy_array_equal(
err_msg=None,
check_same=None,
obj="numpy array",
index=None,
):
"""
Check that 'np.ndarray' is equivalent.
Expand All @@ -940,6 +946,8 @@ def assert_numpy_array_equal(
obj : str, default 'numpy array'
Specify object name being compared, internally used to show appropriate
assertion message.
index : numpy.ndarray, default None
optional index (shared by both left and right), used in output.
"""
__tracebackhide__ = True

Expand Down Expand Up @@ -977,7 +985,7 @@ def _raise(left, right, err_msg):

diff = diff * 100.0 / left.size
msg = f"{obj} values are different ({np.round(diff, 5)} %)"
raise_assert_detail(obj, msg, left, right)
raise_assert_detail(obj, msg, left, right, index=index)

raise AssertionError(err_msg)

Expand Down Expand Up @@ -1142,7 +1150,11 @@ def assert_series_equal(
raise AssertionError("check_exact may only be used with numeric Series")

assert_numpy_array_equal(
left._values, right._values, check_dtype=check_dtype, obj=str(obj)
left._values,
right._values,
check_dtype=check_dtype,
obj=str(obj),
index=left.index._internal_get_values(),
)
elif check_datetimelike_compat and (
needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype)
Expand Down Expand Up @@ -1181,6 +1193,7 @@ def assert_series_equal(
check_less_precise=check_less_precise,
check_dtype=check_dtype,
obj=str(obj),
index=left.index._internal_get_values(),
)

# metadata comparison
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
msg = f"""{obj}\\.iloc\\[:, 1\\] \\(column name="B"\\) are different

{obj}\\.iloc\\[:, 1\\] \\(column name="B"\\) values are different \\(33\\.33333 %\\)
\\[index\\]: \\[0, 1, 2\\]
\\[left\\]: \\[4, 5, 6\\]
\\[right\\]: \\[4, 5, 7\\]"""

Expand All @@ -196,6 +197,7 @@ def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
"""{obj}\\.iloc\\[:, 1\\] \\(column name="E"\\) are different

{obj}\\.iloc\\[:, 1\\] \\(column name="E"\\) values are different \\(33\\.33333 %\\)
\\[index\\]: \\[0, 1, 2\\]
\\[left\\]: \\[é, è, ë\\]
\\[right\\]: \\[é, è, e̊\\]""",
),
Expand All @@ -205,6 +207,7 @@ def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
"""{obj}\\.iloc\\[:, 0\\] \\(column name="A"\\) are different

{obj}\\.iloc\\[:, 0\\] \\(column name="A"\\) values are different \\(100\\.0 %\\)
\\[index\\]: \\[0, 1, 2\\]
\\[left\\]: \\[á, à, ä\\]
\\[right\\]: \\[a, a, a\\]""",
),
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/util/test_assert_series_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def test_series_equal_values_mismatch(check_less_precise):
msg = """Series are different

Series values are different \\(33\\.33333 %\\)
\\[index\\]: \\[0, 1, 2\\]
\\[left\\]: \\[1, 2, 3\\]
\\[right\\]: \\[1, 2, 4\\]"""

Expand Down