diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index c6b6367e347ba..a1458e7429fa8 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1495,7 +1495,16 @@ def __rsub__(self, other): return (-self) + other # We get here with e.g. datetime objects - return -(self - other) + from pandas.core.arrays import DatetimeArray + + datetime_result = self - other + try: + return -(datetime_result) + except TypeError as e: + raise TypeError( + "Unsupported operand type(s) for -: " + f"'{type(self).__name__}' and '{type(other).__name__}'" + ) from e def __iadd__(self, other) -> Self: result = self + other diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 26dfcf088e74b..7faf136d15bec 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -12,6 +12,7 @@ starmap, ) import operator +import re import numpy as np import pytest @@ -1273,8 +1274,8 @@ def test_dt64arr_series_sub_tick_DateOffset(self, box_with_array): result2 = -pd.offsets.Second(5) + ser tm.assert_equal(result2, expected) - msg = "(bad|unsupported) operand type for unary" - with pytest.raises(TypeError, match=msg): + msg = "Unsupported operand type(s) for -: 'DatetimeArray' and 'Second'" + with pytest.raises(TypeError, match=re.escape(msg)): pd.offsets.Second(5) - ser @pytest.mark.parametrize( @@ -1318,8 +1319,10 @@ def test_dti_add_tick_tzaware(self, tz_aware_fixture, box_with_array): roundtrip = offset - scalar tm.assert_equal(roundtrip, dates) - msg = "|".join( - ["bad operand type for unary -", "cannot subtract DatetimeArray"] + msg = ( + r"Unsupported operand type\(s\) " + "for -: 'DatetimeArray' and '.*'|" + r"cannot subtract DatetimeArray from .*" ) with pytest.raises(TypeError, match=msg): scalar - dates @@ -1379,7 +1382,7 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array, unit): expected = DatetimeIndex([x - off for x in vec_items]).as_unit(exp_unit) expected = tm.box_expected(expected, box_with_array) tm.assert_equal(expected, vec - off) - msg = "(bad|unsupported) operand type for unary" + msg = r"Unsupported operand type\(s\) " "for -: 'DatetimeArray' and '.*'" with pytest.raises(TypeError, match=msg): off - vec @@ -1495,7 +1498,7 @@ def test_dt64arr_add_sub_DateOffsets( expected = DatetimeIndex([offset + x for x in vec_items]).as_unit(unit) expected = tm.box_expected(expected, box_with_array) tm.assert_equal(expected, offset + vec) - msg = "(bad|unsupported) operand type for unary" + msg = r"Unsupported operand type\(s\) for -: 'DatetimeArray' and '.*'" with pytest.raises(TypeError, match=msg): offset - vec @@ -1984,7 +1987,7 @@ def test_operators_datetimelike_with_timezones(self): result = dt1 - td1[0] exp = (dt1.dt.tz_localize(None) - td1[0]).dt.tz_localize(tz) tm.assert_series_equal(result, exp) - msg = "(bad|unsupported) operand type for unary" + msg = r"Unsupported operand type\(s\) " "for -: 'DatetimeArray' and 'Timedelta'" with pytest.raises(TypeError, match=msg): td1[0] - dt1 diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 87e085fb22878..7c647eb67d381 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -320,7 +320,7 @@ def test_subtraction_ops(self): with pytest.raises(TypeError, match=msg): td - dt - msg = "(bad|unsupported) operand type for unary" + msg = r"Unsupported operand type\(s\) " "for -: 'DatetimeArray' and 'Timedelta'" with pytest.raises(TypeError, match=msg): td - dti