Skip to content

Commit 0168e27

Browse files
authored
REF: remove _time_shift (#49882)
1 parent 70121c7 commit 0168e27

File tree

5 files changed

+38
-68
lines changed

5 files changed

+38
-68
lines changed

pandas/core/arrays/datetimelike.py

-39
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
from pandas.errors import (
7171
AbstractMethodError,
7272
InvalidComparison,
73-
NullFrequencyError,
7473
PerformanceWarning,
7574
)
7675
from pandas.util._decorators import (
@@ -1360,44 +1359,6 @@ def _addsub_object_array(self, other: np.ndarray, op):
13601359
result = result.reshape(self.shape)
13611360
return result
13621361

1363-
def _time_shift(
1364-
self: DatetimeLikeArrayT, periods: int, freq=None
1365-
) -> DatetimeLikeArrayT:
1366-
"""
1367-
Shift each value by `periods`.
1368-
1369-
Note this is different from ExtensionArray.shift, which
1370-
shifts the *position* of each element, padding the end with
1371-
missing values.
1372-
1373-
Parameters
1374-
----------
1375-
periods : int
1376-
Number of periods to shift by.
1377-
freq : pandas.DateOffset, pandas.Timedelta, or str
1378-
Frequency increment to shift by.
1379-
"""
1380-
if freq is not None and freq != self.freq:
1381-
if isinstance(freq, str):
1382-
freq = to_offset(freq)
1383-
offset = periods * freq
1384-
return self + offset
1385-
1386-
if periods == 0 or len(self) == 0:
1387-
# GH#14811 empty case
1388-
return self.copy()
1389-
1390-
if self.freq is None:
1391-
raise NullFrequencyError("Cannot shift with no freq")
1392-
1393-
start = self[0] + periods * self.freq
1394-
end = self[-1] + periods * self.freq
1395-
1396-
# Note: in the DatetimeTZ case, _generate_range will infer the
1397-
# appropriate timezone from `start` and `end`, so tz does not need
1398-
# to be passed explicitly.
1399-
return self._generate_range(start=start, end=end, periods=None, freq=self.freq)
1400-
14011362
@unpack_zerodim_and_defer("__add__")
14021363
def __add__(self, other):
14031364
other_dtype = getattr(other, "dtype", None)

pandas/core/arrays/period.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -538,28 +538,6 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
538538

539539
# --------------------------------------------------------------------
540540

541-
def _time_shift(self, periods: int, freq=None) -> PeriodArray:
542-
"""
543-
Shift each value by `periods`.
544-
545-
Note this is different from ExtensionArray.shift, which
546-
shifts the *position* of each element, padding the end with
547-
missing values.
548-
549-
Parameters
550-
----------
551-
periods : int
552-
Number of periods to shift by.
553-
freq : pandas.DateOffset, pandas.Timedelta, or str
554-
Frequency increment to shift by.
555-
"""
556-
if freq is not None:
557-
raise TypeError(
558-
"`freq` argument is not supported for "
559-
f"{type(self).__name__}._time_shift"
560-
)
561-
return self + periods
562-
563541
def _box_func(self, x) -> Period | NaTType:
564542
return Period._from_ordinal(ordinal=x, freq=self.freq)
565543

@@ -726,8 +704,7 @@ def _addsub_int_array_or_scalar(
726704
self, other: np.ndarray | int, op: Callable[[Any, Any], Any]
727705
) -> PeriodArray:
728706
"""
729-
Add or subtract array of integers; equivalent to applying
730-
`_time_shift` pointwise.
707+
Add or subtract array of integers.
731708
732709
Parameters
733710
----------

pandas/core/indexes/datetimelike.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
npt,
3434
)
3535
from pandas.compat.numpy import function as nv
36+
from pandas.errors import NullFrequencyError
3637
from pandas.util._decorators import (
3738
Appender,
3839
cache_readonly,
@@ -353,10 +354,7 @@ def shift(self: _T, periods: int = 1, freq=None) -> _T:
353354
Index.shift : Shift values of Index.
354355
PeriodIndex.shift : Shift values of PeriodIndex.
355356
"""
356-
arr = self._data.view()
357-
arr._freq = self.freq
358-
result = arr._time_shift(periods, freq=freq)
359-
return type(self)._simple_new(result, name=self.name)
357+
raise NotImplementedError
360358

361359
# --------------------------------------------------------------------
362360

@@ -400,6 +398,32 @@ def values(self) -> np.ndarray:
400398
# NB: For Datetime64TZ this is lossy
401399
return self._data._ndarray
402400

401+
@doc(DatetimeIndexOpsMixin.shift)
402+
def shift(self: _TDT, periods: int = 1, freq=None) -> _TDT:
403+
if freq is not None and freq != self.freq:
404+
if isinstance(freq, str):
405+
freq = to_offset(freq)
406+
offset = periods * freq
407+
return self + offset
408+
409+
if periods == 0 or len(self) == 0:
410+
# GH#14811 empty case
411+
return self.copy()
412+
413+
if self.freq is None:
414+
raise NullFrequencyError("Cannot shift with no freq")
415+
416+
start = self[0] + periods * self.freq
417+
end = self[-1] + periods * self.freq
418+
419+
# Note: in the DatetimeTZ case, _generate_range will infer the
420+
# appropriate timezone from `start` and `end`, so tz does not need
421+
# to be passed explicitly.
422+
result = self._data._generate_range(
423+
start=start, end=end, periods=None, freq=self.freq
424+
)
425+
return type(self)._simple_new(result, name=self.name)
426+
403427
# --------------------------------------------------------------------
404428
# Set Operation Methods
405429

pandas/core/indexes/period.py

+8
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,14 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
481481
iv = Period(parsed, freq=reso.attr_abbrev)
482482
return (iv.asfreq(self.freq, how="start"), iv.asfreq(self.freq, how="end"))
483483

484+
@doc(DatetimeIndexOpsMixin.shift)
485+
def shift(self, periods: int = 1, freq=None):
486+
if freq is not None:
487+
raise TypeError(
488+
f"`freq` argument is not supported for {type(self).__name__}.shift"
489+
)
490+
return self + periods
491+
484492

485493
def period_range(
486494
start=None, end=None, periods: int | None = None, freq=None, name=None

pandas/tests/indexes/period/methods/test_shift.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_shift_corner_cases(self):
6666
# GH#9903
6767
idx = PeriodIndex([], name="xxx", freq="H")
6868

69-
msg = "`freq` argument is not supported for PeriodArray._time_shift"
69+
msg = "`freq` argument is not supported for PeriodIndex.shift"
7070
with pytest.raises(TypeError, match=msg):
7171
# period shift doesn't accept freq
7272
idx.shift(1, freq="H")

0 commit comments

Comments
 (0)