From c3677068c7e9c24efaa5309b828aeb1083891824 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 4 Aug 2023 11:01:09 -0700 Subject: [PATCH 1/2] REF: de-duplicate test_add_series_with_extension_array --- pandas/tests/extension/base/ops.py | 11 +++++++++++ pandas/tests/extension/json/test_json.py | 5 ----- pandas/tests/extension/test_arrow.py | 23 ++++------------------ pandas/tests/extension/test_categorical.py | 5 ----- pandas/tests/extension/test_datetime.py | 7 ------- pandas/tests/extension/test_period.py | 10 ---------- 6 files changed, 15 insertions(+), 46 deletions(-) diff --git a/pandas/tests/extension/base/ops.py b/pandas/tests/extension/base/ops.py index aafb1900a4236..183a219f7b13c 100644 --- a/pandas/tests/extension/base/ops.py +++ b/pandas/tests/extension/base/ops.py @@ -161,7 +161,18 @@ def test_divmod_series_array(self, data, data_for_twos): self._check_divmod_op(other, ops.rdivmod, ser) def test_add_series_with_extension_array(self, data): + # Check adding an ExtensionArray to a Series of the same dtype matches + # the behavior of adding the arrays directly and then wrapping in a + # Series. + ser = pd.Series(data) + + exc = self._get_expected_exception("__add__", ser, data) + if exc is not None: + with pytest.raises(exc): + ser + data + return + result = ser + data expected = pd.Series(data + data) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/extension/json/test_json.py b/pandas/tests/extension/json/test_json.py index 0c9abd45a51a5..3e80421cec340 100644 --- a/pandas/tests/extension/json/test_json.py +++ b/pandas/tests/extension/json/test_json.py @@ -312,11 +312,6 @@ def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request): request.node.add_marker(mark) super().test_arith_frame_with_scalar(data, all_arithmetic_operators) - def test_add_series_with_extension_array(self, data): - ser = pd.Series(data) - with pytest.raises(TypeError, match="unsupported"): - ser + data - @pytest.mark.xfail(reason="not implemented") def test_divmod_series_array(self): # GH 23287 diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 2438626cf0347..68f8c3af1eb6f 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -994,6 +994,9 @@ def _get_expected_exception( or pa.types.is_integer(pa_dtype) or pa.types.is_decimal(pa_dtype) ): + # TODO: in many of these cases, e.g. non-duration temporal, + # these will *never* be allowed. Would it make more sense to + # re-raise as TypeError, more consistent with non-pyarrow cases? exc = pa.ArrowNotImplementedError else: exc = None @@ -1129,25 +1132,7 @@ def test_arith_series_with_array(self, data, all_arithmetic_operators, request): def test_add_series_with_extension_array(self, data, request): pa_dtype = data.dtype.pyarrow_dtype - if pa.types.is_temporal(pa_dtype) and not pa.types.is_duration(pa_dtype): - # i.e. timestamp, date, time, but not timedelta; these *should* - # raise when trying to add - ser = pd.Series(data) - if pa_version_under7p0: - msg = "Function add_checked has no kernel matching input types" - else: - msg = "Function 'add_checked' has no kernel matching input types" - with pytest.raises(NotImplementedError, match=msg): - # TODO: this is a pa.lib.ArrowNotImplementedError, might - # be better to reraise a TypeError; more consistent with - # non-pyarrow cases - ser + data - - return - - if (pa_version_under8p0 and pa.types.is_duration(pa_dtype)) or ( - pa.types.is_boolean(pa_dtype) - ): + if pa_version_under8p0 and pa.types.is_duration(pa_dtype): request.node.add_marker( pytest.mark.xfail( raises=NotImplementedError, diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py index 5de1debb21d93..806766f7eb478 100644 --- a/pandas/tests/extension/test_categorical.py +++ b/pandas/tests/extension/test_categorical.py @@ -258,11 +258,6 @@ def test_arith_series_with_scalar(self, data, all_arithmetic_operators, request) ) super().test_arith_series_with_scalar(data, op_name) - def test_add_series_with_extension_array(self, data): - ser = pd.Series(data) - with pytest.raises(TypeError, match="cannot perform|unsupported operand"): - ser + data - def test_divmod_series_array(self): # GH 23287 # skipping because it is not implemented diff --git a/pandas/tests/extension/test_datetime.py b/pandas/tests/extension/test_datetime.py index ab21f768e6521..8ea6c5fde2c1c 100644 --- a/pandas/tests/extension/test_datetime.py +++ b/pandas/tests/extension/test_datetime.py @@ -135,13 +135,6 @@ def _get_expected_exception(self, op_name, obj, other): return None return super()._get_expected_exception(op_name, obj, other) - def test_add_series_with_extension_array(self, data): - # Datetime + Datetime not implemented - ser = pd.Series(data) - msg = "cannot add DatetimeArray and DatetimeArray" - with pytest.raises(TypeError, match=msg): - ser + data - def test_divmod_series_array(self): # GH 23287 # skipping because it is not implemented diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 7b6bc98ee8c05..afba2f0db4b18 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -121,16 +121,6 @@ def _get_expected_exception(self, op_name, obj, other): return None return super()._get_expected_exception(op_name, obj, other) - def test_add_series_with_extension_array(self, data): - # we don't implement + for Period - s = pd.Series(data) - msg = ( - r"unsupported operand type\(s\) for \+: " - r"\'PeriodArray\' and \'PeriodArray\'" - ) - with pytest.raises(TypeError, match=msg): - s + data - def test_direct_arith_with_ndframe_returns_not_implemented( self, data, frame_or_series ): From 031b56c347dddb2aacb7111082afdd715761340e Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 4 Aug 2023 12:50:30 -0700 Subject: [PATCH 2/2] remove xfail --- pandas/tests/extension/test_arrow.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 68f8c3af1eb6f..88f7d8ab30431 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -1132,14 +1132,7 @@ def test_arith_series_with_array(self, data, all_arithmetic_operators, request): def test_add_series_with_extension_array(self, data, request): pa_dtype = data.dtype.pyarrow_dtype - if pa_version_under8p0 and pa.types.is_duration(pa_dtype): - request.node.add_marker( - pytest.mark.xfail( - raises=NotImplementedError, - reason=f"add_checked not implemented for {pa_dtype}", - ) - ) - elif pa_dtype.equals("int8"): + if pa_dtype.equals("int8"): request.node.add_marker( pytest.mark.xfail( raises=pa.ArrowInvalid,