Skip to content

Commit

Permalink
Backport PR #54952 on branch 2.1.x (REGR: Arrow backed objects not pr…
Browse files Browse the repository at this point in the history
…opagating exceptions) (#55209)

Backport PR #54952: REGR: Arrow backed objects not propagating exceptions

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Sep 20, 2023
1 parent 1ae73af commit 7acebe4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Fixed regressions
- Fixed regression in :meth:`Series.drop_duplicates` for PyArrow strings (:issue:`54904`)
- Fixed regression in :meth:`Series.interpolate` raising when ``fill_value`` was given (:issue:`54920`)
- Fixed regression in :meth:`Series.value_counts` raising for numeric data if ``bins`` was specified (:issue:`54857`)
- Fixed regression in comparison operations for PyArrow backed columns not propagating exceptions correctly (:issue:`54944`)
- Fixed regression when comparing a :class:`Series` with ``datetime64`` dtype with ``None`` (:issue:`54870`)

.. ---------------------------------------------------------------------------
Expand Down
16 changes: 9 additions & 7 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,20 +627,22 @@ def __setstate__(self, state) -> None:

def _cmp_method(self, other, op):
pc_func = ARROW_CMP_FUNCS[op.__name__]
try:
if isinstance(other, (ArrowExtensionArray, np.ndarray, list, BaseMaskedArray)):
result = pc_func(self._pa_array, self._box_pa(other))
except (pa.lib.ArrowNotImplementedError, pa.lib.ArrowInvalid):
if is_scalar(other):
elif is_scalar(other):
try:
result = pc_func(self._pa_array, self._box_pa(other))
except (pa.lib.ArrowNotImplementedError, pa.lib.ArrowInvalid):
mask = isna(self) | isna(other)
valid = ~mask
result = np.zeros(len(self), dtype="bool")
result[valid] = op(np.array(self)[valid], other)
result = pa.array(result, type=pa.bool_())
result = pc.if_else(valid, result, None)
else:
raise NotImplementedError(
f"{op.__name__} not implemented for {type(other)}"
)
else:
raise NotImplementedError(
f"{op.__name__} not implemented for {type(other)}"
)
return ArrowExtensionArray(result)

def _evaluate_op_method(self, other, op, arrow_funcs):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,14 @@ def test_duration_fillna_numpy(pa_type):
tm.assert_series_equal(result, expected)


def test_comparison_not_propagating_arrow_error():
# GH#54944
a = pd.Series([1 << 63], dtype="uint64[pyarrow]")
b = pd.Series([None], dtype="int64[pyarrow]")
with pytest.raises(pa.lib.ArrowInvalid, match="Integer value"):
a < b


def test_factorize_chunked_dictionary():
# GH 54844
pa_array = pa.chunked_array(
Expand Down

0 comments on commit 7acebe4

Please sign in to comment.