Skip to content

Commit e31dfe1

Browse files
BUG: Enable Series.equals to compare iterables to non-iterables
1 parent 8c7efd1 commit e31dfe1

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Timezones
246246
Numeric
247247
^^^^^^^
248248
- Bug in :func:`to_numeric` where float precision was incorrect (:issue:`31364`)
249+
- Bug in :meth: `Series.equals` where a ``ValueError`` was raised when iterables were compared to non-iterables (:issue:`35267`)
249250
-
250251

251252
Conversion

pandas/_libs/lib.pyx

+7
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,13 @@ def array_equivalent_object(left: object[:], right: object[:]) -> bool:
581581
return False
582582
elif (x is C_NA) ^ (y is C_NA):
583583
return False
584+
# Only compare scalars to scalars and arrays to arrays
585+
elif cnp.PyArray_IsAnyScalar(x) != cnp.PyArray_IsAnyScalar(y):
586+
return False
587+
# Check if arrays have the same type
588+
elif not (cnp.PyArray_IsPythonScalar(x) or cnp.PyArray_IsPythonScalar(y)) \
589+
and not (isinstance(x, type(y)) or isinstance(y, type(x))):
590+
return False
584591
elif not (PyObject_RichCompareBool(x, y, Py_EQ) or
585592
(x is None or is_nan(x)) and (y is None or is_nan(y))):
586593
return False

pandas/tests/series/methods/test_equals.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ def test_equals_list_array():
3131
s2 = s1.copy()
3232
assert s1.equals(s2)
3333

34-
# TODO: Series equals should also work between single value and list
35-
# s1[1] = 9
36-
# assert not s1.equals(s2)
34+
s1[1] = 9
35+
assert not s1.equals(s2)
3736

3837

3938
def test_equals_false_negative():

0 commit comments

Comments
 (0)