Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.3.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Bug fixes
- Fix regression in ``~Series.str.contains``, ``~Series.str.match`` and ``~Series.str.fullmatch``
with a compiled regex and custom flags (:issue:`62240`)
- Fix :meth:`Series.str.match` and :meth:`Series.str.fullmatch` not matching patterns with groups correctly for the Arrow-backed string dtype (:issue:`61072`)

- Fix comparing a :class:`StringDtype` Series with mixed objects raising an error (:issue:`60228`)

Improvements and fixes for Copy-on-Write
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
31 changes: 18 additions & 13 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,22 +883,27 @@ def _cmp_method(self, other, op) -> ArrowExtensionArray:
ltype = self._pa_array.type

if isinstance(other, (ExtensionArray, np.ndarray, list)):
boxed = self._box_pa(other)
rtype = boxed.type
if (pa.types.is_timestamp(ltype) and pa.types.is_date(rtype)) or (
pa.types.is_timestamp(rtype) and pa.types.is_date(ltype)
):
# GH#62157 match non-pyarrow behavior
result = ops.invalid_comparison(self, other, op)
result = pa.array(result, type=pa.bool_())
try:
boxed = self._box_pa(other)
except pa.lib.ArrowInvalid:
# e.g. GH#60228 [1, "b"] we have to operate pointwise
res_values = [op(x, y) for x, y in zip(self, other)]
result = pa.array(res_values, type=pa.bool_(), from_pandas=True)
else:
try:
result = pc_func(self._pa_array, boxed)
except pa.ArrowNotImplementedError:
# TODO: could this be wrong if other is object dtype?
# in which case we need to operate pointwise?
rtype = boxed.type
if (pa.types.is_timestamp(ltype) and pa.types.is_date(rtype)) or (
pa.types.is_timestamp(rtype) and pa.types.is_date(ltype)
):
# GH#62157 match non-pyarrow behavior
result = ops.invalid_comparison(self, other, op)
result = pa.array(result, type=pa.bool_())
else:
try:
result = pc_func(self._pa_array, boxed)
except pa.ArrowNotImplementedError:
result = ops.invalid_comparison(self, other, op)
result = pa.array(result, type=pa.bool_())

elif is_scalar(other):
if (isinstance(other, datetime) and pa.types.is_date(ltype)) or (
type(other) is date and pa.types.is_timestamp(ltype)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/extension/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,19 @@ def test_searchsorted_with_na_raises(data_for_sorting, as_series):
)
with pytest.raises(ValueError, match=msg):
arr.searchsorted(b)


def test_mixed_object_comparison(dtype):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe move this to pandas/tests/arrays/string_/test_string.py? (in general this file only contains the generic extension tests, not string-specific ones)

Copy link
Member

Choose a reason for hiding this comment

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

(although it is not actually that string specific, generally all of our dtypes should do this comparison to mixed object dtype? So we could also make this a base extension test. But let's do that later, that might involve more fixes in other (test) EAs which doesn't need to be backported)

Copy link
Member Author

Choose a reason for hiding this comment

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

i started a branch this morning that adds a tests.arithmetic.test_string file. if this is merged, i'll move this test in that branch too

# GH#60228
ser = pd.Series(["a", "b"], dtype=dtype)

mixed = pd.Series([1, "b"], dtype=object)

result = ser == mixed
expected = pd.Series([False, True], dtype=bool)
if dtype.storage == "python" and dtype.na_value is pd.NA:
expected = expected.astype("boolean")
elif dtype.storage == "pyarrow" and dtype.na_value is pd.NA:
expected = expected.astype("bool[pyarrow]")

tm.assert_series_equal(result, expected)
Loading