Skip to content

Commit a46d841

Browse files
jbrockmendelSeeminSyed
authored andcommitted
CLN: remove DatetimeLikeArray._add_delta (pandas-dev#32799)
1 parent aa99c68 commit a46d841

File tree

4 files changed

+38
-99
lines changed

4 files changed

+38
-99
lines changed

pandas/core/arrays/datetimelike.py

+27-36
Original file line numberDiff line numberDiff line change
@@ -1129,56 +1129,46 @@ def _sub_period(self, other):
11291129
def _add_offset(self, offset):
11301130
raise AbstractMethodError(self)
11311131

1132-
def _add_delta(self, other):
1132+
def _add_timedeltalike_scalar(self, other):
11331133
"""
1134-
Add a timedelta-like, Tick or TimedeltaIndex-like object
1135-
to self, yielding an int64 numpy array
1136-
1137-
Parameters
1138-
----------
1139-
delta : {timedelta, np.timedelta64, Tick,
1140-
TimedeltaIndex, ndarray[timedelta64]}
1134+
Add a delta of a timedeltalike
11411135
11421136
Returns
11431137
-------
1144-
result : ndarray[int64]
1145-
1146-
Notes
1147-
-----
1148-
The result's name is set outside of _add_delta by the calling
1149-
method (__add__ or __sub__), if necessary (i.e. for Indexes).
1150-
"""
1151-
if isinstance(other, (Tick, timedelta, np.timedelta64)):
1152-
new_values = self._add_timedeltalike_scalar(other)
1153-
elif is_timedelta64_dtype(other):
1154-
# ndarray[timedelta64] or TimedeltaArray/index
1155-
new_values = self._add_delta_tdi(other)
1156-
1157-
return new_values
1158-
1159-
def _add_timedeltalike_scalar(self, other):
1160-
"""
1161-
Add a delta of a timedeltalike
1162-
return the i8 result view
1138+
Same type as self
11631139
"""
11641140
if isna(other):
11651141
# i.e np.timedelta64("NaT"), not recognized by delta_to_nanoseconds
11661142
new_values = np.empty(self.shape, dtype="i8")
11671143
new_values[:] = iNaT
1168-
return new_values
1144+
return type(self)(new_values, dtype=self.dtype)
11691145

11701146
inc = delta_to_nanoseconds(other)
11711147
new_values = checked_add_with_arr(self.asi8, inc, arr_mask=self._isnan).view(
11721148
"i8"
11731149
)
11741150
new_values = self._maybe_mask_results(new_values)
1175-
return new_values.view("i8")
11761151

1177-
def _add_delta_tdi(self, other):
1152+
new_freq = None
1153+
if isinstance(self.freq, Tick) or is_period_dtype(self.dtype):
1154+
# adding a scalar preserves freq
1155+
new_freq = self.freq
1156+
1157+
if new_freq is not None:
1158+
# fastpath that doesnt require inference
1159+
return type(self)(new_values, dtype=self.dtype, freq=new_freq)
1160+
return type(self)._from_sequence(new_values, dtype=self.dtype, freq="infer")
1161+
1162+
def _add_timedelta_arraylike(self, other):
11781163
"""
11791164
Add a delta of a TimedeltaIndex
1180-
return the i8 result view
1165+
1166+
Returns
1167+
-------
1168+
Same type as self
11811169
"""
1170+
# overriden by PeriodArray
1171+
11821172
if len(self) != len(other):
11831173
raise ValueError("cannot add indices of unequal length")
11841174

@@ -1196,7 +1186,8 @@ def _add_delta_tdi(self, other):
11961186
if self._hasnans or other._hasnans:
11971187
mask = (self._isnan) | (other._isnan)
11981188
new_values[mask] = iNaT
1199-
return new_values.view("i8")
1189+
1190+
return type(self)._from_sequence(new_values, dtype=self.dtype, freq="infer")
12001191

12011192
def _add_nat(self):
12021193
"""
@@ -1338,7 +1329,7 @@ def __add__(self, other):
13381329
if other is NaT:
13391330
result = self._add_nat()
13401331
elif isinstance(other, (Tick, timedelta, np.timedelta64)):
1341-
result = self._add_delta(other)
1332+
result = self._add_timedeltalike_scalar(other)
13421333
elif isinstance(other, DateOffset):
13431334
# specifically _not_ a Tick
13441335
result = self._add_offset(other)
@@ -1354,7 +1345,7 @@ def __add__(self, other):
13541345
# array-like others
13551346
elif is_timedelta64_dtype(other):
13561347
# TimedeltaIndex, ndarray[timedelta64]
1357-
result = self._add_delta(other)
1348+
result = self._add_timedelta_arraylike(other)
13581349
elif is_object_dtype(other):
13591350
# e.g. Array/Index of DateOffset objects
13601351
result = self._addsub_object_array(other, operator.add)
@@ -1390,7 +1381,7 @@ def __sub__(self, other):
13901381
if other is NaT:
13911382
result = self._sub_nat()
13921383
elif isinstance(other, (Tick, timedelta, np.timedelta64)):
1393-
result = self._add_delta(-other)
1384+
result = self._add_timedeltalike_scalar(-other)
13941385
elif isinstance(other, DateOffset):
13951386
# specifically _not_ a Tick
13961387
result = self._add_offset(-other)
@@ -1409,7 +1400,7 @@ def __sub__(self, other):
14091400
# array-like others
14101401
elif is_timedelta64_dtype(other):
14111402
# TimedeltaIndex, ndarray[timedelta64]
1412-
result = self._add_delta(-other)
1403+
result = self._add_timedelta_arraylike(-other)
14131404
elif is_object_dtype(other):
14141405
# e.g. Array/Index of DateOffset objects
14151406
result = self._addsub_object_array(other, operator.sub)

pandas/core/arrays/datetimes.py

-17
Original file line numberDiff line numberDiff line change
@@ -718,23 +718,6 @@ def _sub_datetimelike_scalar(self, other):
718718
result = self._maybe_mask_results(result)
719719
return result.view("timedelta64[ns]")
720720

721-
def _add_delta(self, delta):
722-
"""
723-
Add a timedelta-like, Tick, or TimedeltaIndex-like object
724-
to self, yielding a new DatetimeArray
725-
726-
Parameters
727-
----------
728-
other : {timedelta, np.timedelta64, Tick,
729-
TimedeltaIndex, ndarray[timedelta64]}
730-
731-
Returns
732-
-------
733-
result : DatetimeArray
734-
"""
735-
new_values = super()._add_delta(delta)
736-
return type(self)._from_sequence(new_values, tz=self.tz, freq="infer")
737-
738721
# -----------------------------------------------------------------
739722
# Timezone Conversion and Localization Methods
740723

pandas/core/arrays/period.py

+11-29
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,11 @@ def _add_timedeltalike_scalar(self, other):
647647
648648
Returns
649649
-------
650-
result : ndarray[int64]
650+
PeriodArray
651651
"""
652-
assert isinstance(self.freq, Tick) # checked by calling function
653-
assert isinstance(other, (timedelta, np.timedelta64, Tick))
652+
if not isinstance(self.freq, Tick):
653+
# We cannot add timedelta-like to non-tick PeriodArray
654+
raise raise_on_incompatible(self, other)
654655

655656
if notna(other):
656657
# special handling for np.timedelta64("NaT"), avoid calling
@@ -660,10 +661,9 @@ def _add_timedeltalike_scalar(self, other):
660661
# Note: when calling parent class's _add_timedeltalike_scalar,
661662
# it will call delta_to_nanoseconds(delta). Because delta here
662663
# is an integer, delta_to_nanoseconds will return it unchanged.
663-
ordinals = super()._add_timedeltalike_scalar(other)
664-
return ordinals
664+
return super()._add_timedeltalike_scalar(other)
665665

666-
def _add_delta_tdi(self, other):
666+
def _add_timedelta_arraylike(self, other):
667667
"""
668668
Parameters
669669
----------
@@ -673,36 +673,18 @@ def _add_delta_tdi(self, other):
673673
-------
674674
result : ndarray[int64]
675675
"""
676-
assert isinstance(self.freq, Tick) # checked by calling function
676+
if not isinstance(self.freq, Tick):
677+
# We cannot add timedelta-like to non-tick PeriodArray
678+
raise raise_on_incompatible(self, other)
677679

678680
if not np.all(isna(other)):
679681
delta = self._check_timedeltalike_freq_compat(other)
680682
else:
681683
# all-NaT TimedeltaIndex is equivalent to a single scalar td64 NaT
682684
return self + np.timedelta64("NaT")
683685

684-
return self._addsub_int_array(delta, operator.add).asi8
685-
686-
def _add_delta(self, other):
687-
"""
688-
Add a timedelta-like, Tick, or TimedeltaIndex-like object
689-
to self, yielding a new PeriodArray
690-
691-
Parameters
692-
----------
693-
other : {timedelta, np.timedelta64, Tick,
694-
TimedeltaIndex, ndarray[timedelta64]}
695-
696-
Returns
697-
-------
698-
result : PeriodArray
699-
"""
700-
if not isinstance(self.freq, Tick):
701-
# We cannot add timedelta-like to non-tick PeriodArray
702-
raise raise_on_incompatible(self, other)
703-
704-
new_ordinals = super()._add_delta(other)
705-
return type(self)(new_ordinals, freq=self.freq)
686+
ordinals = self._addsub_int_array(delta, operator.add).asi8
687+
return type(self)(ordinals, dtype=self.dtype)
706688

707689
def _check_timedeltalike_freq_compat(self, other):
708690
"""

pandas/core/arrays/timedeltas.py

-17
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,6 @@ def _add_offset(self, other):
400400
f"cannot add the type {type(other).__name__} to a {type(self).__name__}"
401401
)
402402

403-
def _add_delta(self, delta):
404-
"""
405-
Add a timedelta-like, Tick, or TimedeltaIndex-like object
406-
to self, yielding a new TimedeltaArray.
407-
408-
Parameters
409-
----------
410-
other : {timedelta, np.timedelta64, Tick,
411-
TimedeltaIndex, ndarray[timedelta64]}
412-
413-
Returns
414-
-------
415-
result : TimedeltaArray
416-
"""
417-
new_values = super()._add_delta(delta)
418-
return type(self)._from_sequence(new_values, freq="infer")
419-
420403
def _add_datetime_arraylike(self, other):
421404
"""
422405
Add DatetimeArray/Index or ndarray[datetime64] to TimedeltaArray.

0 commit comments

Comments
 (0)