Skip to content

Commit 5b20b59

Browse files
committed
catch TypeError on DateTimeArrays specifically; Improve error message
-
1 parent 1aba9a2 commit 5b20b59

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,12 +1498,14 @@ def __rsub__(self, other):
14981498
from pandas.core.arrays import DatetimeArray
14991499

15001500
datetime_result = self - other
1501-
if isinstance(datetime_result, DatetimeArray):
1502-
raise TypeError(
1503-
"TypeError: unsupported operand type(s) for -: "
1504-
f"'{type(self).__name__}' and '{type(other).__name__}'"
1505-
)
1506-
return -(datetime_result)
1501+
try:
1502+
return -(datetime_result)
1503+
except TypeError as e:
1504+
if isinstance(datetime_result, DatetimeArray):
1505+
raise TypeError(
1506+
"Unsupported operand type(s) for -: "
1507+
f"'{type(self).__name__}' and '{type(other).__name__}'"
1508+
) from e
15071509

15081510
def __iadd__(self, other) -> Self:
15091511
result = self + other

0 commit comments

Comments
 (0)