Skip to content

ENH: raise_assert_detail shows the difference between the columns #48390

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 3 commits into from
Sep 12, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enhancement2
Other enhancements
^^^^^^^^^^^^^^^^^^
- :meth:`Series.add_suffix`, :meth:`DataFrame.add_suffix`, :meth:`Series.add_prefix` and :meth:`DataFrame.add_prefix` support an ``axis`` argument. If ``axis`` is set, the default behaviour of which axis to consider can be overwritten (:issue:`47819`)
- :func:`assert_frame_equal` now shows the first element where the DataFrames differ, analogously to ``pytest``'s output (:issue:`47910`)
-

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ cpdef assert_almost_equal(a, b,
Py_ssize_t i, na, nb
double fa, fb
bint is_unequal = False, a_is_ndarray, b_is_ndarray
str first_diff = ''

if lobj is None:
lobj = a
Expand Down Expand Up @@ -159,12 +160,14 @@ cpdef assert_almost_equal(a, b,
except AssertionError:
is_unequal = True
diff += 1
if not first_diff:
first_diff = f"At positional index {i}, first diff: {a[i]} != {b[i]}"

if is_unequal:
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, index_values=index_values)
raise_assert_detail(obj, msg, lobj, robj, first_diff=first_diff, index_values=index_values)

return True

Expand Down
7 changes: 6 additions & 1 deletion pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ def assert_timedelta_array_equal(
assert_attr_equal("freq", left, right, obj=obj)


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

msg = f"""{obj} are different
Expand Down Expand Up @@ -674,6 +676,9 @@ def raise_assert_detail(obj, message, left, right, diff=None, index_values=None)
if diff is not None:
msg += f"\n[diff]: {diff}"

if first_diff is not None:
msg += f"\n{first_diff}"

raise AssertionError(msg)


Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def test_frame_equal_index_mismatch(check_like, obj_fixture):

{obj_fixture}\\.index values are different \\(33\\.33333 %\\)
\\[left\\]: Index\\(\\['a', 'b', 'c'\\], dtype='object'\\)
\\[right\\]: Index\\(\\['a', 'b', 'd'\\], dtype='object'\\)"""
\\[right\\]: Index\\(\\['a', 'b', 'd'\\], dtype='object'\\)
At positional index 2, first diff: c != d"""

df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "c"])
df2 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "d"])
Expand Down