Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,12 +829,17 @@ def _cmp_method(self, other, op) -> ArrowExtensionArray:
pc_func = ARROW_CMP_FUNCS[op.__name__]
if isinstance(other, (ExtensionArray, np.ndarray, list)):
try:
result = pc_func(self._pa_array, self._box_pa(other))
except pa.ArrowNotImplementedError:
# TODO: could this be wrong if other is object dtype?
# in which case we need to operate pointwise?
result = ops.invalid_comparison(self, other, op)
result = pa.array(result, type=pa.bool_())
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:
result = ops.invalid_comparison(self, other, op)
result = pa.array(result, type=pa.bool_())
elif is_scalar(other):
try:
result = pc_func(self._pa_array, self._box_pa(other))
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