Skip to content

ENH: TimedeltaArray add/sub with non-nano #48261

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 2 commits into from
Aug 29, 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
11 changes: 10 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ def _add_datetimelike_scalar(self, other) -> DatetimeArray:

i8 = self.asi8
result = checked_add_with_arr(i8, other.value, arr_mask=self._isnan)

dtype = tz_to_dtype(tz=other.tz, unit=self._unit)
res_values = result.view(f"M8[{self._unit}]")
return DatetimeArray._simple_new(res_values, dtype=dtype, freq=self.freq)
Expand Down Expand Up @@ -1289,13 +1290,21 @@ def _add_timedelta_arraylike(

other = ensure_wrapped_if_datetimelike(other)
other = cast("TimedeltaArray", other)
self = cast("DatetimeArray | TimedeltaArray", self)

if self._reso != other._reso:
raise NotImplementedError(
f"Addition of {type(self).__name__} with TimedeltaArray with "
"mis-matched resolutions is not yet supported."
)

self_i8 = self.asi8
other_i8 = other.asi8
new_values = checked_add_with_arr(
self_i8, other_i8, arr_mask=self._isnan, b_mask=other._isnan
)
return type(self)(new_values, dtype=self.dtype)
res_values = new_values.view(self._ndarray.dtype)
return type(self)._simple_new(res_values, dtype=self.dtype)

@final
def _add_nat(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,10 @@ def __rdivmod__(self, other):
return res1, res2

def __neg__(self) -> TimedeltaArray:
freq = None
if self.freq is not None:
return type(self)(-self._ndarray, freq=-self.freq)
return type(self)(-self._ndarray)
freq = -self.freq
return type(self)._simple_new(-self._ndarray, dtype=self.dtype, freq=freq)

def __pos__(self) -> TimedeltaArray:
return type(self)(self._ndarray.copy(), freq=self.freq)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/arrays/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ def test_div_td_array(self, tda):
expected = tda._ndarray / other
tm.assert_numpy_array_equal(result, expected)

def test_add_timedeltaarraylike(self, tda):
# TODO(2.0): just do `tda_nano = tda.astype("m8[ns]")`
tda_nano = TimedeltaArray(tda._ndarray.astype("m8[ns]"))

msg = "mis-matched resolutions is not yet supported"
with pytest.raises(NotImplementedError, match=msg):
tda_nano + tda
with pytest.raises(NotImplementedError, match=msg):
tda + tda_nano
with pytest.raises(NotImplementedError, match=msg):
tda - tda_nano
with pytest.raises(NotImplementedError, match=msg):
tda_nano - tda

result = tda_nano + tda_nano
expected = tda_nano * 2
tm.assert_extension_array_equal(result, expected)


class TestTimedeltaArray:
@pytest.mark.parametrize("dtype", [int, np.int32, np.int64, "uint32", "uint64"])
Expand Down