Skip to content

Commit 042ac78

Browse files
authored
ERR: improve exception message from timedelta64-datetime64 (#61876)
1 parent 88cb152 commit 042ac78

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,8 @@ def __rsub__(self, other):
14861486
# GH#19959 datetime - datetime is well-defined as timedelta,
14871487
# but any other type - datetime is not well-defined.
14881488
raise TypeError(
1489-
f"cannot subtract {type(self).__name__} from {type(other).__name__}"
1489+
f"cannot subtract {type(self).__name__} from "
1490+
f"{type(other).__name__}[{other.dtype}]"
14901491
)
14911492
elif isinstance(self.dtype, PeriodDtype) and lib.is_np_dtype(other_dtype, "m"):
14921493
# TODO: Can we simplify/generalize these cases at all?
@@ -1495,8 +1496,14 @@ def __rsub__(self, other):
14951496
self = cast("TimedeltaArray", self)
14961497
return (-self) + other
14971498

1499+
flipped = self - other
1500+
if flipped.dtype.kind == "M":
1501+
# GH#59571 give a more helpful exception message
1502+
raise TypeError(
1503+
f"cannot subtract {type(self).__name__} from {type(other).__name__}"
1504+
)
14981505
# We get here with e.g. datetime objects
1499-
return -(self - other)
1506+
return -flipped
15001507

15011508
def __iadd__(self, other) -> Self:
15021509
result = self + other

pandas/tests/arithmetic/test_datetime64.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,12 @@ def test_dt64arr_add_sub_td64ndarray(self, tz_naive_fixture, box_with_array):
955955

956956
result = dtarr - tdarr
957957
tm.assert_equal(result, expected)
958-
msg = "cannot subtract|(bad|unsupported) operand type for unary"
958+
msg = "|".join(
959+
[
960+
"cannot subtract DatetimeArray from ndarray",
961+
"cannot subtract a datelike from a TimedeltaArray",
962+
]
963+
)
959964
with pytest.raises(TypeError, match=msg):
960965
tdarr - dtarr
961966

@@ -1272,7 +1277,7 @@ def test_dt64arr_series_sub_tick_DateOffset(self, box_with_array):
12721277

12731278
result2 = -pd.offsets.Second(5) + ser
12741279
tm.assert_equal(result2, expected)
1275-
msg = "(bad|unsupported) operand type for unary"
1280+
msg = "cannot subtract DatetimeArray from Second"
12761281
with pytest.raises(TypeError, match=msg):
12771282
pd.offsets.Second(5) - ser
12781283

@@ -1317,9 +1322,7 @@ def test_dti_add_tick_tzaware(self, tz_aware_fixture, box_with_array):
13171322
roundtrip = offset - scalar
13181323
tm.assert_equal(roundtrip, dates)
13191324

1320-
msg = "|".join(
1321-
["bad operand type for unary -", "cannot subtract DatetimeArray"]
1322-
)
1325+
msg = "cannot subtract DatetimeArray from"
13231326
with pytest.raises(TypeError, match=msg):
13241327
scalar - dates
13251328

@@ -1378,7 +1381,7 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array, unit):
13781381
expected = DatetimeIndex([x - off for x in vec_items]).as_unit(exp_unit)
13791382
expected = tm.box_expected(expected, box_with_array)
13801383
tm.assert_equal(expected, vec - off)
1381-
msg = "(bad|unsupported) operand type for unary"
1384+
msg = "cannot subtract DatetimeArray from"
13821385
with pytest.raises(TypeError, match=msg):
13831386
off - vec
13841387

@@ -1494,7 +1497,7 @@ def test_dt64arr_add_sub_DateOffsets(
14941497
expected = DatetimeIndex([offset + x for x in vec_items]).as_unit(unit)
14951498
expected = tm.box_expected(expected, box_with_array)
14961499
tm.assert_equal(expected, offset + vec)
1497-
msg = "(bad|unsupported) operand type for unary"
1500+
msg = "cannot subtract DatetimeArray from"
14981501
with pytest.raises(TypeError, match=msg):
14991502
offset - vec
15001503

@@ -1983,7 +1986,7 @@ def test_operators_datetimelike_with_timezones(self):
19831986
result = dt1 - td1[0]
19841987
exp = (dt1.dt.tz_localize(None) - td1[0]).dt.tz_localize(tz)
19851988
tm.assert_series_equal(result, exp)
1986-
msg = "(bad|unsupported) operand type for unary"
1989+
msg = "cannot subtract DatetimeArray from"
19871990
with pytest.raises(TypeError, match=msg):
19881991
td1[0] - dt1
19891992

pandas/tests/arithmetic/test_timedelta64.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def test_subtraction_ops(self):
320320
with pytest.raises(TypeError, match=msg):
321321
td - dt
322322

323-
msg = "(bad|unsupported) operand type for unary"
323+
msg = "cannot subtract DatetimeArray from Timedelta"
324324
with pytest.raises(TypeError, match=msg):
325325
td - dti
326326

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy