Skip to content

CLN: have Timestamp return NotImplemented #28157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,8 @@ cdef class _Timestamp(datetime):
return self + neg_other

typ = getattr(other, '_typ', None)

# a Timestamp-DatetimeIndex -> yields a negative TimedeltaIndex
if typ in ('datetimeindex', 'datetimearray'):
# timezone comparison is performed in DatetimeIndex._sub_datelike
return -other.__sub__(self)

# a Timestamp-TimedeltaIndex -> yields a negative TimedeltaIndex
elif typ in ('timedeltaindex', 'timedeltaarray'):
return (-other).__add__(self)
if typ is not None:
return NotImplemented

elif other is NaT:
return NaT
Expand Down
16 changes: 12 additions & 4 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ def __sub__(self, other):
return result

def __rsub__(self, other):
if is_datetime64_any_dtype(other) and is_timedelta64_dtype(self):
if is_datetime64_any_dtype(other) and is_timedelta64_dtype(self.dtype):
# ndarray[datetime64] cannot be subtracted from self, so
# we need to wrap in DatetimeArray/Index and flip the operation
if not isinstance(other, DatetimeLikeArrayMixin):
Expand All @@ -1310,9 +1310,9 @@ def __rsub__(self, other):
other = DatetimeArray(other)
return other - self
elif (
is_datetime64_any_dtype(self)
is_datetime64_any_dtype(self.dtype)
and hasattr(other, "dtype")
and not is_datetime64_any_dtype(other)
and not is_datetime64_any_dtype(other.dtype)
):
# GH#19959 datetime - datetime is well-defined as timedelta,
# but any other type - datetime is not well-defined.
Expand All @@ -1321,13 +1321,21 @@ def __rsub__(self, other):
cls=type(self).__name__, typ=type(other).__name__
)
)
elif is_period_dtype(self) and is_timedelta64_dtype(other):
elif is_period_dtype(self.dtype) and is_timedelta64_dtype(other):
# TODO: Can we simplify/generalize these cases at all?
raise TypeError(
"cannot subtract {cls} from {dtype}".format(
cls=type(self).__name__, dtype=other.dtype
)
)
elif is_timedelta64_dtype(self.dtype):
if lib.is_integer(other) or is_integer_dtype(other):
# need to subtract before negating, since that flips freq
# -self flips self.freq, messing up results
return -(self - other)

return (-self) + other

return -(self - other)

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