From 4d23e02212a705c1163a6085eb82f7380d44ce6d Mon Sep 17 00:00:00 2001 From: StevenSchaerer <53116297+stevenschaerer@users.noreply.github.com> Date: Sun, 5 Jun 2022 22:48:23 +0200 Subject: [PATCH 1/4] Add check_like functionality to assert_series_equal --- pandas/_testing/asserters.py | 13 +++++++++ pandas/tests/util/test_assert_series_equal.py | 29 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index d04dc76c01f7e..b1f7564da53d5 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -876,6 +876,7 @@ def assert_series_equal( obj="Series", *, check_index=True, + check_like=False, ): """ Check that left and right Series are equal. @@ -941,6 +942,11 @@ def assert_series_equal( Whether to check index equivalence. If False, then compare only values. .. versionadded:: 1.3.0 + check_like : bool, default False + If True, ignore the order of the index. Must be False if check_index is False. + Note: same labels must be with the same data. + + .. versionadded:: 1.4.3 Examples -------- @@ -951,6 +957,9 @@ def assert_series_equal( """ __tracebackhide__ = True + if not check_index and check_like: + raise ValueError("check_like must be False if check_index is False") + if check_less_precise is not no_default: warnings.warn( "The 'check_less_precise' keyword in testing.assert_*_equal " @@ -985,11 +994,15 @@ def assert_series_equal( check_names=check_names, check_exact=check_exact, check_categorical=check_categorical, + check_order=not check_like, rtol=rtol, atol=atol, obj=f"{obj}.index", ) + if check_like: + left, right = left.reindex_like(right), right + if check_freq and isinstance(left.index, (DatetimeIndex, TimedeltaIndex)): lidx = left.index ridx = right.index diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index dcf1fe291f179..d24ffa8baf2a0 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -158,6 +158,35 @@ def test_series_equal_index_dtype(s1, s2, msg, check_index_type): tm.assert_series_equal(s1, s2, **kwargs) +@pytest.mark.parametrize("check_like", [True, False]) +def test_series_equal_order_mismatch(check_like): + s1 = Series([1, 2, 3], index=["a", "b", "c"]) + s2 = Series([3, 2, 1], index=["c", "b", "a"]) + + if not check_like: # Do not ignore row-column orderings. + with pytest.raises(AssertionError, match="Series.index are different"): + tm.assert_series_equal(s1, s2, check_like=check_like) + else: + _assert_series_equal_both(s1, s2, check_like=check_like) + + +@pytest.mark.parametrize("check_index", [True, False]) +def test_series_equal_index_mismatch(check_index): + s1 = Series([1, 2, 3], index=["a", "b", "c"]) + s2 = Series([1, 2, 3], index=["c", "b", "a"]) + + if check_index: # Do not ignore row-column orderings. + with pytest.raises(AssertionError, match="Series.index are different"): + tm.assert_series_equal(s1, s2, check_index=check_index) + else: + _assert_series_equal_both(s1, s2, check_index=check_index) + + +def test_series_invalid_param_combination(): + with pytest.raises(ValueError, match="check_like must be False if check_index is False"): + tm.assert_series_equal(pd.Series(), pd.Series(), check_index=False, check_like=True) + + def test_series_equal_length_mismatch(rtol): msg = """Series are different From 90c23bb8c40725287f271ded69a1f8e335ae9a9f Mon Sep 17 00:00:00 2001 From: StevenSchaerer <53116297+stevenschaerer@users.noreply.github.com> Date: Sun, 5 Jun 2022 23:02:29 +0200 Subject: [PATCH 2/4] fix code formatting issues --- pandas/tests/util/test_assert_series_equal.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index d24ffa8baf2a0..a9223cb2aa3bb 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -183,8 +183,10 @@ def test_series_equal_index_mismatch(check_index): def test_series_invalid_param_combination(): - with pytest.raises(ValueError, match="check_like must be False if check_index is False"): - tm.assert_series_equal(pd.Series(), pd.Series(), check_index=False, check_like=True) + with pytest.raises( + ValueError, match="check_like must be False if check_index is False" + ): + tm.assert_series_equal(Series(), Series(), check_index=False, check_like=True) def test_series_equal_length_mismatch(rtol): From 1be17eddcc4eedc5a68c07d57e2d6cff648d04d0 Mon Sep 17 00:00:00 2001 From: StevenSchaerer <53116297+stevenschaerer@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:22:36 +0200 Subject: [PATCH 3/4] versionadded and whatsnew --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/_testing/asserters.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 6bf6fd65f5633..720d18f8a7fe3 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -152,6 +152,7 @@ Other enhancements - Added ``numeric_only`` argument to :meth:`Resampler.sum`, :meth:`Resampler.prod`, :meth:`Resampler.min`, :meth:`Resampler.max`, :meth:`Resampler.first`, and :meth:`Resampler.last` (:issue:`46442`) - ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`) - :class:`DataError`, :class:`SpecificationError`, :class:`SettingWithCopyError`, and :class:`SettingWithCopyWarning` are now exposed in ``pandas.errors`` (:issue:`27656`) +- Added ``check_like`` argument to :func:`testing.assert_series_equal` (:issue:`47247`) .. --------------------------------------------------------------------------- .. _whatsnew_150.notable_bug_fixes: diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index b1f7564da53d5..5507da041e01d 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -946,7 +946,7 @@ def assert_series_equal( If True, ignore the order of the index. Must be False if check_index is False. Note: same labels must be with the same data. - .. versionadded:: 1.4.3 + .. versionadded:: 1.5.0 Examples -------- From f18c4bad30e6f883ae02c3a0a9ccc5c6f72a5852 Mon Sep 17 00:00:00 2001 From: StevenSchaerer <53116297+stevenschaerer@users.noreply.github.com> Date: Mon, 6 Jun 2022 21:24:52 +0200 Subject: [PATCH 4/4] fix test comment --- pandas/tests/util/test_assert_series_equal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/util/test_assert_series_equal.py b/pandas/tests/util/test_assert_series_equal.py index a9223cb2aa3bb..2209bed67325c 100644 --- a/pandas/tests/util/test_assert_series_equal.py +++ b/pandas/tests/util/test_assert_series_equal.py @@ -163,7 +163,7 @@ def test_series_equal_order_mismatch(check_like): s1 = Series([1, 2, 3], index=["a", "b", "c"]) s2 = Series([3, 2, 1], index=["c", "b", "a"]) - if not check_like: # Do not ignore row-column orderings. + if not check_like: # Do not ignore index ordering. with pytest.raises(AssertionError, match="Series.index are different"): tm.assert_series_equal(s1, s2, check_like=check_like) else: @@ -175,7 +175,7 @@ def test_series_equal_index_mismatch(check_index): s1 = Series([1, 2, 3], index=["a", "b", "c"]) s2 = Series([1, 2, 3], index=["c", "b", "a"]) - if check_index: # Do not ignore row-column orderings. + if check_index: # Do not ignore index. with pytest.raises(AssertionError, match="Series.index are different"): tm.assert_series_equal(s1, s2, check_index=check_index) else: