Skip to content

ENH: Timestamp +- timedeltalike scalar support non-nano #47313

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 3 commits into from
Jun 13, 2022
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
30 changes: 19 additions & 11 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,6 @@ cdef class _Timestamp(ABCTimestamp):
cdef:
int64_t nanos = 0

if isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:
raise NotImplementedError(self._reso)

if is_any_td_scalar(other):
if is_timedelta64_object(other):
other_reso = get_datetime64_unit(other)
Expand All @@ -390,20 +387,31 @@ cdef class _Timestamp(ABCTimestamp):
# TODO: no tests get here
other = ensure_td64ns(other)

# TODO: what to do with mismatched resos?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO I would raise and only allow this for matching resos

# TODO: disallow round_ok
nanos = delta_to_nanoseconds(
other, reso=self._reso, round_ok=True
)
try:
result = type(self)(self.value + nanos, tz=self.tzinfo)
new_value = self.value + nanos
except OverflowError:
# Use Python ints
# Hit in test_tdi_add_overflow
result = type(self)(int(self.value) + int(nanos), tz=self.tzinfo)
new_value = int(self.value) + int(nanos)

try:
result = type(self)._from_value_and_reso(new_value, reso=self._reso, tz=self.tzinfo)
except OverflowError as err:
# TODO: don't hard-code nanosecond here
raise OutOfBoundsDatetime(f"Out of bounds nanosecond timestamp: {new_value}") from err

if result is not NaT:
result._set_freq(self._freq) # avoid warning in constructor
return result

elif isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:
raise NotImplementedError(self._reso)

elif is_integer_object(other):
raise integer_op_not_supported(self)

Expand Down Expand Up @@ -431,13 +439,16 @@ cdef class _Timestamp(ABCTimestamp):
return NotImplemented

def __sub__(self, other):
if isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:
raise NotImplementedError(self._reso)
if other is NaT:
return NaT

if is_any_td_scalar(other) or is_integer_object(other):
elif is_any_td_scalar(other) or is_integer_object(other):
neg_other = -other
return self + neg_other

elif isinstance(self, _Timestamp) and self._reso != NPY_FR_ns:
raise NotImplementedError(self._reso)

elif is_array(other):
if other.dtype.kind in ['i', 'u']:
raise integer_op_not_supported(self)
Expand All @@ -450,9 +461,6 @@ cdef class _Timestamp(ABCTimestamp):
)
return NotImplemented

if other is NaT:
return NaT

# coerce if necessary if we are a Timestamp-like
if (PyDateTime_Check(self)
and (PyDateTime_Check(other) or is_datetime64_object(other))):
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,29 @@ def test_to_period(self, dt64, ts):
alt = Timestamp(dt64)
assert ts.to_period("D") == alt.to_period("D")

@pytest.mark.parametrize(
"td", [timedelta(days=4), Timedelta(days=4), np.timedelta64(4, "D")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the future Timedelta with a non-nano reso will be tested separately to solidify ts._reso/td._reso semantics?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats the idea, yes

)
def test_addsub_timedeltalike_non_nano(self, dt64, ts, td):

result = ts - td
expected = Timestamp(dt64) - td
assert isinstance(result, Timestamp)
assert result._reso == ts._reso
assert result == expected

result = ts + td
expected = Timestamp(dt64) + td
assert isinstance(result, Timestamp)
assert result._reso == ts._reso
assert result == expected

result = td + ts
expected = td + Timestamp(dt64)
assert isinstance(result, Timestamp)
assert result._reso == ts._reso
assert result == expected


class TestAsUnit:
def test_as_unit(self):
Expand Down