Skip to content

Commit 4612a82

Browse files
arminvTomAugspurger
authored andcommitted
API/DEPR: 'periods' argument instead of 'n' for DatetimeIndex.shift() (#22697)
1 parent f8c5705 commit 4612a82

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

Diff for: doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ Deprecations
562562
- :meth:`Series.str.cat` has deprecated using arbitrary list-likes *within* list-likes. A list-like container may still contain
563563
many ``Series``, ``Index`` or 1-dimensional ``np.ndarray``, or alternatively, only scalar values. (:issue:`21950`)
564564
- :meth:`FrozenNDArray.searchsorted` has deprecated the ``v`` parameter in favor of ``value`` (:issue:`14645`)
565+
- :func:`DatetimeIndex.shift` now accepts ``periods`` argument instead of ``n`` for consistency with :func:`Index.shift` and :func:`Series.shift`. Using ``n`` throws a deprecation warning (:issue:`22458`)
565566

566567
.. _whatsnew_0240.prior_deprecations:
567568

Diff for: pandas/core/arrays/datetimelike.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from pandas.core.algorithms import checked_add_with_arr
3939

4040
from .base import ExtensionOpsMixin
41+
from pandas.util._decorators import deprecate_kwarg
4142

4243

4344
def _make_comparison_op(op, cls):
@@ -522,40 +523,54 @@ def _addsub_offset_array(self, other, op):
522523
kwargs['freq'] = 'infer'
523524
return type(self)(res_values, **kwargs)
524525

525-
def shift(self, n, freq=None):
526+
@deprecate_kwarg(old_arg_name='n', new_arg_name='periods')
527+
def shift(self, periods, freq=None):
526528
"""
527-
Specialized shift which produces a Datetime/Timedelta Array/Index
529+
Shift index by desired number of time frequency increments.
530+
531+
This method is for shifting the values of datetime-like indexes
532+
by a specified time increment a given number of times.
528533
529534
Parameters
530535
----------
531-
n : int
532-
Periods to shift by
533-
freq : DateOffset or timedelta-like, optional
536+
periods : int
537+
Number of periods (or increments) to shift by,
538+
can be positive or negative.
539+
540+
.. versionchanged:: 0.24.0
541+
542+
freq : pandas.DateOffset, pandas.Timedelta or string, optional
543+
Frequency increment to shift by.
544+
If None, the index is shifted by its own `freq` attribute.
545+
Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc.
534546
535547
Returns
536548
-------
537-
shifted : same type as self
549+
pandas.DatetimeIndex
550+
Shifted index.
551+
552+
See Also
553+
--------
554+
Index.shift : Shift values of Index.
538555
"""
539556
if freq is not None and freq != self.freq:
540557
if isinstance(freq, compat.string_types):
541558
freq = frequencies.to_offset(freq)
542-
offset = n * freq
559+
offset = periods * freq
543560
result = self + offset
544-
545561
if hasattr(self, 'tz'):
546562
result._tz = self.tz
547-
548563
return result
549564

550-
if n == 0:
565+
if periods == 0:
551566
# immutable so OK
552567
return self.copy()
553568

554569
if self.freq is None:
555570
raise NullFrequencyError("Cannot shift with no freq")
556571

557-
start = self[0] + n * self.freq
558-
end = self[-1] + n * self.freq
572+
start = self[0] + periods * self.freq
573+
end = self[-1] + periods * self.freq
559574
attribs = self._get_attributes_dict()
560575
return self._generate_range(start=start, end=end, periods=None,
561576
**attribs)

Diff for: pandas/core/generic.py

+5
Original file line numberDiff line numberDiff line change
@@ -8288,6 +8288,11 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
82888288
See Notes.
82898289
axis : %(axes_single_arg)s
82908290
8291+
See Also
8292+
--------
8293+
Index.shift : Shift values of Index.
8294+
DatetimeIndex.shift : Shift values of DatetimeIndex.
8295+
82918296
Notes
82928297
-----
82938298
If freq is specified then the index values are shifted but the data

Diff for: pandas/tests/indexes/datetimes/test_ops.py

+10
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,16 @@ def test_shift(self):
540540
shifted = rng.shift(1, freq=CDay())
541541
assert shifted[0] == rng[0] + CDay()
542542

543+
def test_shift_periods(self):
544+
# GH #22458 : argument 'n' was deprecated in favor of 'periods'
545+
idx = pd.DatetimeIndex(start=START, end=END,
546+
periods=3)
547+
tm.assert_index_equal(idx.shift(periods=0), idx)
548+
tm.assert_index_equal(idx.shift(0), idx)
549+
with tm.assert_produces_warning(FutureWarning,
550+
check_stacklevel=True):
551+
tm.assert_index_equal(idx.shift(n=0), idx)
552+
543553
def test_pickle_unpickle(self):
544554
unpickled = tm.round_trip_pickle(self.rng)
545555
assert unpickled.freq is not None

0 commit comments

Comments
 (0)