Skip to content
6 changes: 4 additions & 2 deletions pandas/core/ops/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ def make_flex_doc(op_name: str, typ: str) -> str:

Parameters
----------
other : Series or scalar value
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.
Expand Down
18 changes: 12 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6072,8 +6072,10 @@ def eq(

Parameters
----------
other : Series or scalar value
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.
Expand Down Expand Up @@ -6141,8 +6143,10 @@ def le(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series:

Parameters
----------
other : Series or scalar value
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.
Expand Down Expand Up @@ -6213,8 +6217,10 @@ def ge(self, other, level=None, fill_value=None, axis: Axis = 0) -> Series:

Parameters
----------
other : Series or scalar value
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.
Expand Down
49 changes: 49 additions & 0 deletions pandas/tests/series/methods/test_compare.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from enum import (
Enum,
auto,
)

import numpy as np
import pytest

Expand Down Expand Up @@ -138,3 +143,47 @@ 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():
"""Test eq with Enum and List elements"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a comment; our tests (mostly) do not utilize docstrings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a reference to the PR as a comment here:

# GH#62191

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you setup the asserts in the following pattern:

expected = ...
result = ...
tm.assert_series_equal(result, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of checking that the result does not equal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was discussed that eq does not work properly when the elements of a Series is a list or other array-like objects. I wanted to verify this point, and show that the result is not as one would expected.

If this is not necessary, I can remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with leaving something akin to this operation, but you should assert the result that pandas gives back. It's better to check two things are equal than not equal (as it's a stronger test).



def test_eq_with_index():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same requests with this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean removing the whole test test_eq_with_index?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the requests in the test above also to be done in this test as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rhshadrach , from the context I am not too sure which lines to be remove. Could you be more specific and quote the line numbers? Thanks.

I have checked

    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(result_non_scalar, expected_would_be)

which seems to have analogy with the lines I removed earlier. That seems to be a new test to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The request that was made above which hasn't been applied here is to make all lines tm.assert_series_equal(result, expected) (#62191 (comment))

"""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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not belong here and is already tested for elsewhere; can you remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


tm.assert_series_equal(left.eq(pd.Series([2, 1])), pd.Series([True, True]))
Loading