Skip to content

Commit 42d6ee7

Browse files
jbrockmendeljreback
authored andcommitted
have Timestamp return NotImplemented (#28157)
1 parent f8a924b commit 42d6ee7

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

pandas/_libs/tslibs/c_timestamp.pyx

+2-9
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,8 @@ cdef class _Timestamp(datetime):
269269
return self + neg_other
270270

271271
typ = getattr(other, '_typ', None)
272-
273-
# a Timestamp-DatetimeIndex -> yields a negative TimedeltaIndex
274-
if typ in ('datetimeindex', 'datetimearray'):
275-
# timezone comparison is performed in DatetimeIndex._sub_datelike
276-
return -other.__sub__(self)
277-
278-
# a Timestamp-TimedeltaIndex -> yields a negative TimedeltaIndex
279-
elif typ in ('timedeltaindex', 'timedeltaarray'):
280-
return (-other).__add__(self)
272+
if typ is not None:
273+
return NotImplemented
281274

282275
elif other is NaT:
283276
return NaT

pandas/core/arrays/datetimelike.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ def __sub__(self, other):
13001300
return result
13011301

13021302
def __rsub__(self, other):
1303-
if is_datetime64_any_dtype(other) and is_timedelta64_dtype(self):
1303+
if is_datetime64_any_dtype(other) and is_timedelta64_dtype(self.dtype):
13041304
# ndarray[datetime64] cannot be subtracted from self, so
13051305
# we need to wrap in DatetimeArray/Index and flip the operation
13061306
if not isinstance(other, DatetimeLikeArrayMixin):
@@ -1310,9 +1310,9 @@ def __rsub__(self, other):
13101310
other = DatetimeArray(other)
13111311
return other - self
13121312
elif (
1313-
is_datetime64_any_dtype(self)
1313+
is_datetime64_any_dtype(self.dtype)
13141314
and hasattr(other, "dtype")
1315-
and not is_datetime64_any_dtype(other)
1315+
and not is_datetime64_any_dtype(other.dtype)
13161316
):
13171317
# GH#19959 datetime - datetime is well-defined as timedelta,
13181318
# but any other type - datetime is not well-defined.
@@ -1321,13 +1321,21 @@ def __rsub__(self, other):
13211321
cls=type(self).__name__, typ=type(other).__name__
13221322
)
13231323
)
1324-
elif is_period_dtype(self) and is_timedelta64_dtype(other):
1324+
elif is_period_dtype(self.dtype) and is_timedelta64_dtype(other):
13251325
# TODO: Can we simplify/generalize these cases at all?
13261326
raise TypeError(
13271327
"cannot subtract {cls} from {dtype}".format(
13281328
cls=type(self).__name__, dtype=other.dtype
13291329
)
13301330
)
1331+
elif is_timedelta64_dtype(self.dtype):
1332+
if lib.is_integer(other) or is_integer_dtype(other):
1333+
# need to subtract before negating, since that flips freq
1334+
# -self flips self.freq, messing up results
1335+
return -(self - other)
1336+
1337+
return (-self) + other
1338+
13311339
return -(self - other)
13321340

13331341
# FIXME: DTA/TDA/PA inplace methods should actually be inplace, GH#24115

0 commit comments

Comments
 (0)