Repository: pandas-dev/pandas
Pull request: ENH: Add check_like functionality to assert_series_equal
- [x] closes #47247
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.


Problem statement:
ENH: check_like for assert_series_equal
#### Is your feature request related to a problem?

I wish `assert_series_equal` would contain the same `check_like` functionality as `assert_frame_equal`.

#### Describe the solution you'd like

The solution should be the same as `assert_frame_equal`.

#### API breaking implications

The change would be fully backward compatible (default value `False`).

#### Describe alternatives you've considered

I currently use `assert_index_equal` with `check_order=False` and use `assert_series_equal` with the reindexed Series.

#### Additional context

```python
# the following should not raise an AssertionError:
assert_series_equal(
    pd.Series([1, 2, 3], index=["a", "b", "c"]),
    pd.Series([3, 2, 1], index=["c", "b", "a"]),
    check_like=True,
)
```

Note: If check_index is False then check_like must be False.

I'll create a PR in a bit.

Hints from issue discussion:

Commit message: fix code formatting issues

Patch to review:
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..a9223cb2aa3bb 100644
--- a/pandas/tests/util/test_assert_series_equal.py
+++ b/pandas/tests/util/test_assert_series_equal.py
@@ -158,6 +158,37 @@ 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(Series(), Series(), check_index=False, check_like=True)
+
+
 def test_series_equal_length_mismatch(rtol):
     msg = """Series are different
 


Code review comments:
diff_hunk: @@ -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
path: pandas/_testing/asserters.py
text: @user1:
Please change to 1.5.0

@user1:
Also please add an entry to `whatsnew/1.5.0.rst`