Skip to content

Commit 012166c

Browse files
authored
DOC: definition of a scalar in rich comparison methods (#62191)
1 parent 5eaf80b commit 012166c

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

pandas/core/ops/docstrings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,10 @@ def make_flex_doc(op_name: str, typ: str) -> str:
435435

436436
Parameters
437437
----------
438-
other : Series or scalar value
439-
The second operand in this operation.
438+
other : object
439+
When a Series is provided, will align on indexes. For all other types,
440+
will behave the same as ``==`` but with possibly different results due
441+
to the other arguments.
440442
level : int or name
441443
Broadcast across a level, matching Index values on the
442444
passed MultiIndex level.

pandas/core/series.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,8 +6092,10 @@ def eq(
60926092

60936093
Parameters
60946094
----------
6095-
other : Series or scalar value
6096-
The second operand in this operation.
6095+
other : object
6096+
When a Series is provided, will align on indexes. For all other types,
6097+
will behave the same as ``==`` but with possibly different results due
6098+
to the other arguments.
60976099
level : int or name
60986100
Broadcast across a level, matching Index values on the
60996101
passed MultiIndex level.
@@ -6161,8 +6163,10 @@ def le(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series:
61616163

61626164
Parameters
61636165
----------
6164-
other : Series or scalar value
6165-
The second operand in this operation.
6166+
other : object
6167+
When a Series is provided, will align on indexes. For all other types,
6168+
will behave the same as ``==`` but with possibly different results due
6169+
to the other arguments.
61666170
level : int or name
61676171
Broadcast across a level, matching Index values on the
61686172
passed MultiIndex level.
@@ -6233,8 +6237,10 @@ def ge(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series:
62336237

62346238
Parameters
62356239
----------
6236-
other : Series or scalar value
6237-
The second operand in this operation.
6240+
other : object
6241+
When a Series is provided, will align on indexes. For all other types,
6242+
will behave the same as ``==`` but with possibly different results due
6243+
to the other arguments.
62386244
level : int or name
62396245
Broadcast across a level, matching Index values on the
62406246
passed MultiIndex level.

pandas/tests/series/methods/test_compare.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from enum import (
2+
Enum,
3+
auto,
4+
)
5+
16
import numpy as np
27
import pytest
38

@@ -138,3 +143,61 @@ def test_compare_datetime64_and_string():
138143
tm.assert_series_equal(result_eq1, expected_eq)
139144
tm.assert_series_equal(result_eq2, expected_eq)
140145
tm.assert_series_equal(result_neq, expected_neq)
146+
147+
148+
def test_eq_objects():
149+
# GH#62191 Test eq with Enum and List elements
150+
151+
class Thing(Enum):
152+
FIRST = auto()
153+
SECOND = auto()
154+
155+
left = pd.Series([Thing.FIRST, Thing.SECOND])
156+
py_l = [Thing.FIRST, Thing.SECOND]
157+
158+
result = left.eq(Thing.FIRST)
159+
expected = pd.Series([True, False])
160+
tm.assert_series_equal(result, expected)
161+
162+
result = left.eq(py_l)
163+
expected = pd.Series([True, True])
164+
tm.assert_series_equal(result, expected)
165+
166+
result = left.eq(np.asarray(py_l))
167+
expected = pd.Series([True, True])
168+
tm.assert_series_equal(result, expected)
169+
170+
result = left.eq(pd.Series(py_l))
171+
expected = pd.Series([True, True])
172+
tm.assert_series_equal(result, expected)
173+
174+
result = pd.Series([[1, 2], [3, 4]]).eq([1, 2])
175+
expected = pd.Series([True, False])
176+
with pytest.raises(AssertionError):
177+
tm.assert_series_equal(result, expected)
178+
expected = pd.Series([False, False])
179+
tm.assert_series_equal(result, expected)
180+
181+
182+
def test_eq_with_index():
183+
# GH#62191 Test eq with non-trivial indices
184+
left = pd.Series([1, 2], index=[1, 0])
185+
py_l = [1, 2]
186+
187+
# assuming Python list has the same index as the Series
188+
result = left.eq(py_l)
189+
expected = pd.Series([True, True], index=[1, 0])
190+
tm.assert_series_equal(result, expected)
191+
192+
# assuming np.ndarray has the same index as the Series
193+
result = left.eq(np.asarray(py_l))
194+
expected = pd.Series([True, True], index=[1, 0])
195+
tm.assert_series_equal(result, expected)
196+
197+
result = left.eq(pd.Series(py_l))
198+
expected = pd.Series([False, False])
199+
tm.assert_series_equal(result, expected)
200+
201+
result = left.eq(pd.Series([2, 1]))
202+
expected = pd.Series([True, True])
203+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)