From f60bf958da887b232834e89af31a0630021b801d Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Mon, 25 Aug 2025 22:47:13 +0200 Subject: [PATCH 1/6] doc: definition of a scalar --- pandas/core/ops/docstrings.py | 2 +- pandas/core/series.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/ops/docstrings.py b/pandas/core/ops/docstrings.py index 5ce0a2da86f31..6ff7883506741 100644 --- a/pandas/core/ops/docstrings.py +++ b/pandas/core/ops/docstrings.py @@ -435,7 +435,7 @@ def make_flex_doc(op_name: str, typ: str) -> str: Parameters ---------- -other : Series or scalar value +other : Series or scalar value (that can be the element of a Series) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the diff --git a/pandas/core/series.py b/pandas/core/series.py index 63c9963fb7eac..aecd1ab94fcfd 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -6072,7 +6072,7 @@ def eq( Parameters ---------- - other : Series or scalar value + other : Series or scalar value (that can be the element of a Series) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the @@ -6141,7 +6141,7 @@ def le(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series: Parameters ---------- - other : Series or scalar value + other : Series or scalar value (that can be the element of a Series) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the @@ -6213,7 +6213,7 @@ def ge(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series: Parameters ---------- - other : Series or scalar value + other : Series or scalar value (that can be the element of a Series) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the From 288f06ee843b14b9583204d02060c4e901d2e67f Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 26 Aug 2025 15:23:16 +0200 Subject: [PATCH 2/6] fix(comment): https://github.com/pandas-dev/pandas/pull/62191#discussion_r2299424241 --- pandas/core/ops/docstrings.py | 2 +- pandas/core/series.py | 6 ++--- pandas/tests/series/methods/test_compare.py | 27 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/pandas/core/ops/docstrings.py b/pandas/core/ops/docstrings.py index 6ff7883506741..d06ddda0a4dfc 100644 --- a/pandas/core/ops/docstrings.py +++ b/pandas/core/ops/docstrings.py @@ -435,7 +435,7 @@ def make_flex_doc(op_name: str, typ: str) -> str: Parameters ---------- -other : Series or scalar value (that can be the element of a Series) +other : Array-like or scalar value (non-array-like element of the former) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the diff --git a/pandas/core/series.py b/pandas/core/series.py index aecd1ab94fcfd..1bf99aba20831 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -6072,7 +6072,7 @@ def eq( Parameters ---------- - other : Series or scalar value (that can be the element of a Series) + other : Array-like or scalar value (non-array-like element of the former) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the @@ -6141,7 +6141,7 @@ def le(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series: Parameters ---------- - other : Series or scalar value (that can be the element of a Series) + other : Array-like or scalar value (non-array-like element of the former) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the @@ -6213,7 +6213,7 @@ def ge(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series: Parameters ---------- - other : Series or scalar value (that can be the element of a Series) + other : Array-like or scalar value (non-array-like element of the former) The second operand in this operation. level : int or name Broadcast across a level, matching Index values on the diff --git a/pandas/tests/series/methods/test_compare.py b/pandas/tests/series/methods/test_compare.py index 2a57d5139b62c..b72f468ea5746 100644 --- a/pandas/tests/series/methods/test_compare.py +++ b/pandas/tests/series/methods/test_compare.py @@ -1,3 +1,8 @@ +from enum import ( + Enum, + auto, +) + import numpy as np import pytest @@ -138,3 +143,25 @@ def test_compare_datetime64_and_string(): tm.assert_series_equal(result_eq1, expected_eq) tm.assert_series_equal(result_eq2, expected_eq) tm.assert_series_equal(result_neq, expected_neq) + + +def test_eq_objects(): + class Thing(Enum): + FIRST = auto() + SECOND = auto() + + left = pd.Series([Thing.FIRST, Thing.SECOND]) + tm.assert_series_equal(left.eq(Thing.FIRST), left == Thing.FIRST) + + py_l = [Thing.FIRST, Thing.SECOND] + tm.assert_series_equal(left.eq(py_l), left == py_l) + + np_a = np.asarray(py_l) + tm.assert_series_equal(left.eq(np_a), left == np_a) + + pd_s = pd.Series(py_l) + tm.assert_series_equal(left.eq(pd_s), left == pd_s) + + left_non_scalar = pd.Series([[1, 2], [3, 4]]) + with pytest.raises(AssertionError): + tm.assert_series_equal(left_non_scalar.eq([1, 2]), pd.Series([True, False])) From 470d093a556beb5c7bd88654110ea5236c9f9279 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Wed, 27 Aug 2025 15:39:21 +0200 Subject: [PATCH 3/6] fix(comment): https://github.com/pandas-dev/pandas/issues/62063#issuecomment-3226016092 --- pandas/core/ops/docstrings.py | 6 ++++-- pandas/core/series.py | 18 ++++++++++++------ pandas/tests/series/methods/test_compare.py | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pandas/core/ops/docstrings.py b/pandas/core/ops/docstrings.py index d06ddda0a4dfc..d97880f133306 100644 --- a/pandas/core/ops/docstrings.py +++ b/pandas/core/ops/docstrings.py @@ -435,8 +435,10 @@ def make_flex_doc(op_name: str, typ: str) -> str: Parameters ---------- -other : Array-like or scalar value (non-array-like element of the former) - The second operand in this operation. +other : object + When a Series is provided, will align on indexes. For all other types, + will behave the same as ``==`` but with possibly different results due + to the other arguments. level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level. diff --git a/pandas/core/series.py b/pandas/core/series.py index 1bf99aba20831..6b3bebceb9004 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -6072,8 +6072,10 @@ def eq( Parameters ---------- - other : Array-like or scalar value (non-array-like element of the former) - The second operand in this operation. + other : object + When a Series is provided, will align on indexes. For all other types, + will behave the same as ``==`` but with possibly different results due + to the other arguments. level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level. @@ -6141,8 +6143,10 @@ def le(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series: Parameters ---------- - other : Array-like or scalar value (non-array-like element of the former) - The second operand in this operation. + other : object + When a Series is provided, will align on indexes. For all other types, + will behave the same as ``==`` but with possibly different results due + to the other arguments. level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level. @@ -6213,8 +6217,10 @@ def ge(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series: Parameters ---------- - other : Array-like or scalar value (non-array-like element of the former) - The second operand in this operation. + other : object + When a Series is provided, will align on indexes. For all other types, + will behave the same as ``==`` but with possibly different results due + to the other arguments. level : int or name Broadcast across a level, matching Index values on the passed MultiIndex level. diff --git a/pandas/tests/series/methods/test_compare.py b/pandas/tests/series/methods/test_compare.py index b72f468ea5746..8d90eec801272 100644 --- a/pandas/tests/series/methods/test_compare.py +++ b/pandas/tests/series/methods/test_compare.py @@ -146,6 +146,7 @@ def test_compare_datetime64_and_string(): def test_eq_objects(): + """Test eq with Enum and List elements""" class Thing(Enum): FIRST = auto() SECOND = auto() @@ -165,3 +166,23 @@ class Thing(Enum): left_non_scalar = pd.Series([[1, 2], [3, 4]]) with pytest.raises(AssertionError): tm.assert_series_equal(left_non_scalar.eq([1, 2]), pd.Series([True, False])) + + +def test_eq_with_index(): + """Test eq with non-trivial indices""" + left = pd.Series([1, 2], index=[1, 0]) + + py_l = [1, 2] + tm.assert_series_equal(left.eq(py_l), left == py_l) + + np_a = np.asarray(py_l) + tm.assert_series_equal(left.eq(np_a), left == np_a) + + pd_s = pd.Series(py_l) + tm.assert_series_equal(left.eq(pd_s), pd.Series([False, False])) + + match = r"Can only compare identically-labeled Series objects" + with pytest.raises(ValueError, match=match): + _ = left == pd_s + + tm.assert_series_equal(left.eq(pd.Series([2, 1])), pd.Series([True, True])) From 9f262151316efe53722ef60ca41edcfe969107b8 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Wed, 27 Aug 2025 20:24:37 +0200 Subject: [PATCH 4/6] fix: ruff --- pandas/tests/series/methods/test_compare.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/methods/test_compare.py b/pandas/tests/series/methods/test_compare.py index 8d90eec801272..fdc717d9fe74c 100644 --- a/pandas/tests/series/methods/test_compare.py +++ b/pandas/tests/series/methods/test_compare.py @@ -147,6 +147,7 @@ def test_compare_datetime64_and_string(): def test_eq_objects(): """Test eq with Enum and List elements""" + class Thing(Enum): FIRST = auto() SECOND = auto() From 36bfdef7e475441fafe2b338d1032538179fb17f Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Sun, 7 Sep 2025 16:59:04 +0200 Subject: [PATCH 5/6] fix: comments --- pandas/tests/series/methods/test_compare.py | 29 +++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/pandas/tests/series/methods/test_compare.py b/pandas/tests/series/methods/test_compare.py index fdc717d9fe74c..e62e07c9b0eec 100644 --- a/pandas/tests/series/methods/test_compare.py +++ b/pandas/tests/series/methods/test_compare.py @@ -146,31 +146,42 @@ def test_compare_datetime64_and_string(): def test_eq_objects(): - """Test eq with Enum and List elements""" + # GH#62191 Test eq with Enum and List elements class Thing(Enum): FIRST = auto() SECOND = auto() left = pd.Series([Thing.FIRST, Thing.SECOND]) - tm.assert_series_equal(left.eq(Thing.FIRST), left == Thing.FIRST) + + result_scalar = left.eq(Thing.FIRST) + expected_scalar = left == Thing.FIRST + tm.assert_series_equal(result_scalar, expected_scalar) py_l = [Thing.FIRST, Thing.SECOND] - tm.assert_series_equal(left.eq(py_l), left == py_l) + result_py_l = left.eq(py_l) + expected_py_l = left == py_l + tm.assert_series_equal(result_py_l, expected_py_l) np_a = np.asarray(py_l) - tm.assert_series_equal(left.eq(np_a), left == np_a) + result_np_a = left.eq(np_a) + expected_np_a = left == np_a + tm.assert_series_equal(result_np_a, expected_np_a) pd_s = pd.Series(py_l) - tm.assert_series_equal(left.eq(pd_s), left == pd_s) + result_pd_s = left.eq(pd_s) + expected_pd_s = left == pd_s + tm.assert_series_equal(result_pd_s, expected_pd_s) left_non_scalar = pd.Series([[1, 2], [3, 4]]) + result_non_scalar = left_non_scalar.eq([1, 2]) + expected_would_be = pd.Series([True, False]) with pytest.raises(AssertionError): - tm.assert_series_equal(left_non_scalar.eq([1, 2]), pd.Series([True, False])) + tm.assert_series_equal(result_non_scalar, expected_would_be) def test_eq_with_index(): - """Test eq with non-trivial indices""" + # GH#62191 Test eq with non-trivial indices left = pd.Series([1, 2], index=[1, 0]) py_l = [1, 2] @@ -182,8 +193,4 @@ def test_eq_with_index(): pd_s = pd.Series(py_l) tm.assert_series_equal(left.eq(pd_s), pd.Series([False, False])) - match = r"Can only compare identically-labeled Series objects" - with pytest.raises(ValueError, match=match): - _ = left == pd_s - tm.assert_series_equal(left.eq(pd.Series([2, 1])), pd.Series([True, True])) From 2b186fd3380e07a3d1a99ea71eb24688f4180aca Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Fri, 3 Oct 2025 11:13:53 +0200 Subject: [PATCH 6/6] fix: https://github.com/pandas-dev/pandas/pull/62191#discussion_r2389252086 --- pandas/tests/series/methods/test_compare.py | 65 ++++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/pandas/tests/series/methods/test_compare.py b/pandas/tests/series/methods/test_compare.py index e62e07c9b0eec..2eae0e778f156 100644 --- a/pandas/tests/series/methods/test_compare.py +++ b/pandas/tests/series/methods/test_compare.py @@ -153,44 +153,51 @@ class Thing(Enum): SECOND = auto() left = pd.Series([Thing.FIRST, Thing.SECOND]) + py_l = [Thing.FIRST, Thing.SECOND] - result_scalar = left.eq(Thing.FIRST) - expected_scalar = left == Thing.FIRST - tm.assert_series_equal(result_scalar, expected_scalar) + result = left.eq(Thing.FIRST) + expected = pd.Series([True, False]) + tm.assert_series_equal(result, expected) - py_l = [Thing.FIRST, Thing.SECOND] - result_py_l = left.eq(py_l) - expected_py_l = left == py_l - tm.assert_series_equal(result_py_l, expected_py_l) - - np_a = np.asarray(py_l) - result_np_a = left.eq(np_a) - expected_np_a = left == np_a - tm.assert_series_equal(result_np_a, expected_np_a) - - pd_s = pd.Series(py_l) - result_pd_s = left.eq(pd_s) - expected_pd_s = left == pd_s - tm.assert_series_equal(result_pd_s, expected_pd_s) - - left_non_scalar = pd.Series([[1, 2], [3, 4]]) - result_non_scalar = left_non_scalar.eq([1, 2]) - expected_would_be = pd.Series([True, False]) + result = left.eq(py_l) + expected = pd.Series([True, True]) + tm.assert_series_equal(result, expected) + + result = left.eq(np.asarray(py_l)) + expected = pd.Series([True, True]) + tm.assert_series_equal(result, expected) + + result = left.eq(pd.Series(py_l)) + expected = pd.Series([True, True]) + tm.assert_series_equal(result, expected) + + result = pd.Series([[1, 2], [3, 4]]).eq([1, 2]) + expected = pd.Series([True, False]) with pytest.raises(AssertionError): - tm.assert_series_equal(result_non_scalar, expected_would_be) + tm.assert_series_equal(result, expected) + expected = pd.Series([False, False]) + tm.assert_series_equal(result, expected) def test_eq_with_index(): # GH#62191 Test eq with non-trivial indices left = pd.Series([1, 2], index=[1, 0]) - py_l = [1, 2] - tm.assert_series_equal(left.eq(py_l), left == py_l) - np_a = np.asarray(py_l) - tm.assert_series_equal(left.eq(np_a), left == np_a) + # assuming Python list has the same index as the Series + result = left.eq(py_l) + expected = pd.Series([True, True], index=[1, 0]) + tm.assert_series_equal(result, expected) + + # assuming np.ndarray has the same index as the Series + result = left.eq(np.asarray(py_l)) + expected = pd.Series([True, True], index=[1, 0]) + tm.assert_series_equal(result, expected) - pd_s = pd.Series(py_l) - tm.assert_series_equal(left.eq(pd_s), pd.Series([False, False])) + result = left.eq(pd.Series(py_l)) + expected = pd.Series([False, False]) + tm.assert_series_equal(result, expected) - tm.assert_series_equal(left.eq(pd.Series([2, 1])), pd.Series([True, True])) + result = left.eq(pd.Series([2, 1])) + expected = pd.Series([True, True]) + tm.assert_series_equal(result, expected)