From 548d48fed9cb8b4f616531de15adbc9b3f6de592 Mon Sep 17 00:00:00 2001 From: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com> Date: Wed, 29 Jan 2020 23:20:48 +0000 Subject: [PATCH 1/7] Added index to assert message when values differ --- pandas/_libs/testing.pyx | 7 ++++-- pandas/_testing.py | 23 +++++++++++++++---- pandas/tests/util/test_assert_frame_equal.py | 3 +++ pandas/tests/util/test_assert_series_equal.py | 2 ++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index 0e57b563d4d25..df875cc15af32 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -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. @@ -89,6 +89,9 @@ 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 """ cdef: int decimal @@ -171,7 +174,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 diff --git a/pandas/_testing.py b/pandas/_testing.py index dff15c66750ac..8054ef515fcd6 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -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): @@ -901,9 +908,10 @@ def raise_assert_detail(obj, message, left, right, diff=None): elif is_categorical_dtype(right): right = repr(right) - msg = f"""{obj} are different + if isinstance(index, np.ndarray): + index = pprint_thing(index) -{message} + msg += f""" [left]: {left} [right]: {right}""" @@ -921,6 +929,7 @@ def assert_numpy_array_equal( err_msg=None, check_same=None, obj="numpy array", + index=None, ): """ Check that 'np.ndarray' is equivalent. @@ -940,6 +949,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 + optional index (shared by both left and right), used in output. """ __tracebackhide__ = True @@ -977,7 +988,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) @@ -1142,7 +1153,8 @@ 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) @@ -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 diff --git a/pandas/tests/util/test_assert_frame_equal.py b/pandas/tests/util/test_assert_frame_equal.py index 3090343ba2fd9..b6b0ca53d1fdb 100644 --- a/pandas/tests/util/test_assert_frame_equal.py +++ b/pandas/tests/util/test_assert_frame_equal.py @@ -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\\]""" @@ -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̊\\]""", ), @@ -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\\]""", ), diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index eaf0824f52927..5b48021c47b25 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -162,12 +162,14 @@ def test_series_equal_length_mismatch(check_less_precise): with pytest.raises(AssertionError, match=msg): tm.assert_series_equal(s1, s2, check_less_precise=check_less_precise) + print("No Exception") 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\\]""" From c2da1d0b1c5a0ad7b74ca081190ca3328e2d568e Mon Sep 17 00:00:00 2001 From: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com> Date: Thu, 30 Jan 2020 22:14:53 +0000 Subject: [PATCH 2/7] Added default value doc string --- pandas/_testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 8054ef515fcd6..5da13f3a24a4a 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -949,7 +949,7 @@ 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 + index : numpy.ndarray, default None optional index (shared by both left and right), used in output. """ __tracebackhide__ = True From b1d8fb80c4b9dfa6b26d68b10eaefc7326510738 Mon Sep 17 00:00:00 2001 From: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com> Date: Fri, 31 Jan 2020 22:14:25 +0000 Subject: [PATCH 3/7] removed unnecessary output --- pandas/tests/util/test_assert_series_equal.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index 5b48021c47b25..a77c57fb5411c 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -162,7 +162,6 @@ def test_series_equal_length_mismatch(check_less_precise): with pytest.raises(AssertionError, match=msg): tm.assert_series_equal(s1, s2, check_less_precise=check_less_precise) - print("No Exception") def test_series_equal_values_mismatch(check_less_precise): From 114f88e18a04e31f164c4f23368a99a797e7fa62 Mon Sep 17 00:00:00 2001 From: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com> Date: Sun, 15 Mar 2020 00:56:04 +0000 Subject: [PATCH 4/7] Added versionadded tag and removed unused code --- pandas/_libs/testing.pyx | 3 +++ pandas/_testing.py | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index df875cc15af32..f1e8d6c5611d3 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -92,6 +92,9 @@ cpdef assert_almost_equal(a, b, 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 diff --git a/pandas/_testing.py b/pandas/_testing.py index 5da13f3a24a4a..04633b827dae1 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -908,9 +908,6 @@ def raise_assert_detail(obj, message, left, right, diff=None, index=None): elif is_categorical_dtype(right): right = repr(right) - if isinstance(index, np.ndarray): - index = pprint_thing(index) - msg += f""" [left]: {left} [right]: {right}""" From 44ba94a795952f9a5b9938867e77aa3c2a0399e3 Mon Sep 17 00:00:00 2001 From: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com> Date: Sun, 15 Mar 2020 01:35:55 +0000 Subject: [PATCH 5/7] Fix Pep 8 issue --- pandas/_testing.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 04633b827dae1..a4b29529b6a64 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -1150,8 +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), - index = left.index._internal_get_values() + 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) From 60ad1cba3abb87779aa0b90c3e806f9e0701bbf5 Mon Sep 17 00:00:00 2001 From: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com> Date: Tue, 17 Mar 2020 21:36:42 +0000 Subject: [PATCH 6/7] Avoid use of _internal_get_values --- pandas/_testing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index a4b29529b6a64..fb37cb57033b6 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -1154,7 +1154,7 @@ def assert_series_equal( right._values, check_dtype=check_dtype, obj=str(obj), - index=left.index._internal_get_values(), + index=np.asarray(left.index), ) elif check_datetimelike_compat and ( needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype) @@ -1193,7 +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(), + index=np.asarray(left.index), ) # metadata comparison From 97b4b7db10a2a7849e853bce2f41019a97eecee0 Mon Sep 17 00:00:00 2001 From: Anthony Milbourne <18662115+amilbourne@users.noreply.github.com> Date: Sat, 21 Mar 2020 22:01:33 +0000 Subject: [PATCH 7/7] Renamed index parameters to index_values and fixed pydoc --- pandas/_libs/testing.pyx | 10 +++++----- pandas/_testing.py | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index f1e8d6c5611d3..c6b8c3e876390 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -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, index=None): + obj=None, lobj=None, robj=None, index_values=None): """ Check that left and right objects are almost equal. @@ -89,9 +89,9 @@ 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 + index_values : ndarray, default None + Specify shared index values of objects being compared, internally used + to show appropriate assertion message .. versionadded:: 1.1.0 @@ -177,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, index=index) + raise_assert_detail(obj, msg, lobj, robj, index_values=index_values) return True diff --git a/pandas/_testing.py b/pandas/_testing.py index fb37cb57033b6..db5d141cc0051 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -888,15 +888,15 @@ 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, index=None): +def raise_assert_detail(obj, message, left, right, diff=None, index_values=None): __tracebackhide__ = True msg = f"""{obj} are different {message}""" - if isinstance(index, np.ndarray): - msg += f"\n[index]: {pprint_thing(index)}" + if isinstance(index_values, np.ndarray): + msg += f"\n[index]: {pprint_thing(index_values)}" if isinstance(left, np.ndarray): left = pprint_thing(left) @@ -926,7 +926,7 @@ def assert_numpy_array_equal( err_msg=None, check_same=None, obj="numpy array", - index=None, + index_values=None, ): """ Check that 'np.ndarray' is equivalent. @@ -946,7 +946,7 @@ 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 + index_values : numpy.ndarray, default None optional index (shared by both left and right), used in output. """ __tracebackhide__ = True @@ -985,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, index=index) + raise_assert_detail(obj, msg, left, right, index_values=index_values) raise AssertionError(err_msg) @@ -1154,7 +1154,7 @@ def assert_series_equal( right._values, check_dtype=check_dtype, obj=str(obj), - index=np.asarray(left.index), + index_values=np.asarray(left.index), ) elif check_datetimelike_compat and ( needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype) @@ -1193,7 +1193,7 @@ def assert_series_equal( check_less_precise=check_less_precise, check_dtype=check_dtype, obj=str(obj), - index=np.asarray(left.index), + index_values=np.asarray(left.index), ) # metadata comparison