From c91e60387fe6101eebbf284065ecc08ff76bf8a3 Mon Sep 17 00:00:00 2001 From: MeeseeksMachine <39504233+meeseeksmachine@users.noreply.github.com> Date: Fri, 30 Sep 2022 11:34:49 +0200 Subject: [PATCH] Backport PR #48833 on branch 1.5.x (BUG: ArrowExtensionArray compared to invalid object not raising) (#48878) Backport PR #48833: BUG: ArrowExtensionArray compared to invalid object not raising Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v1.5.1.rst | 2 +- pandas/core/arrays/arrow/array.py | 2 +- pandas/tests/extension/test_arrow.py | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index e5a7f6529e383..e9e5e5062b4e6 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -94,7 +94,7 @@ Bug fixes - Bug in :func:`assert_index_equal` for extension arrays with non matching ``NA`` raising ``ValueError`` (:issue:`48608`) - Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`) - Bug in :meth:`DataFrame.sort_values` emitting unnecessary ``FutureWarning`` when called on :class:`DataFrame` with boolean sparse columns (:issue:`48784`) -- +- Bug in :class:`.arrays.ArrowExtensionArray` with a comparison operator to an invalid object would not raise a ``NotImplementedError`` (:issue:`48833`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/arrays/arrow/array.py b/pandas/core/arrays/arrow/array.py index cfae5b4cae681..c3538e6aa7b32 100644 --- a/pandas/core/arrays/arrow/array.py +++ b/pandas/core/arrays/arrow/array.py @@ -390,7 +390,7 @@ def _cmp_method(self, other, op): result[valid] = op(np.array(self)[valid], other) return BooleanArray(result, mask) else: - return NotImplementedError( + raise NotImplementedError( f"{op.__name__} not implemented for {type(other)}" ) diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 62e9503286311..dae7d2f01d5e2 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -1620,6 +1620,13 @@ def test_compare_array(self, data, comparison_op, na_value, request): with pytest.raises(type(exc)): ser.combine(other, comparison_op) + def test_invalid_other_comp(self, data, comparison_op): + # GH 48833 + with pytest.raises( + NotImplementedError, match=".* not implemented for " + ): + comparison_op(data, object()) + def test_arrowdtype_construct_from_string_type_with_unsupported_parameters(): with pytest.raises(NotImplementedError, match="Passing pyarrow type"):