Skip to content

Commit f20d917

Browse files
arminvtm9k1
authored andcommitted
API/DEPR: 'periods' argument instead of 'n' for PeriodIndex.shift() (pandas-dev#22912)
1 parent 21d0830 commit f20d917

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ Deprecations
638638
- :meth:`Series.str.cat` has deprecated using arbitrary list-likes *within* list-likes. A list-like container may still contain
639639
many ``Series``, ``Index`` or 1-dimensional ``np.ndarray``, or alternatively, only scalar values. (:issue:`21950`)
640640
- :meth:`FrozenNDArray.searchsorted` has deprecated the ``v`` parameter in favor of ``value`` (:issue:`14645`)
641-
- :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`)
641+
- :func:`DatetimeIndex.shift` and :func:`PeriodIndex.shift` now accept ``periods`` argument instead of ``n`` for consistency with :func:`Index.shift` and :func:`Series.shift`. Using ``n`` throws a deprecation warning (:issue:`22458`, :issue:`22912`)
642642

643643
.. _whatsnew_0240.prior_deprecations:
644644

pandas/core/arrays/datetimelike.py

+1
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ def shift(self, periods, freq=None):
552552
See Also
553553
--------
554554
Index.shift : Shift values of Index.
555+
PeriodIndex.shift : Shift values of PeriodIndex.
555556
"""
556557
return self._time_shift(periods=periods, freq=freq)
557558

pandas/core/arrays/period.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pandas._libs.tslibs.fields import isleapyear_arr
1515

1616
from pandas import compat
17-
from pandas.util._decorators import cache_readonly
17+
from pandas.util._decorators import (cache_readonly, deprecate_kwarg)
1818

1919
from pandas.core.dtypes.common import (
2020
is_integer_dtype, is_float_dtype, is_period_dtype)
@@ -319,20 +319,32 @@ def _add_delta(self, other):
319319
ordinal_delta = self._maybe_convert_timedelta(other)
320320
return self._time_shift(ordinal_delta)
321321

322-
def shift(self, n):
322+
@deprecate_kwarg(old_arg_name='n', new_arg_name='periods')
323+
def shift(self, periods):
323324
"""
324-
Specialized shift which produces an Period Array/Index
325+
Shift index by desired number of increments.
326+
327+
This method is for shifting the values of period indexes
328+
by a specified time increment.
325329
326330
Parameters
327331
----------
328-
n : int
329-
Periods to shift by
332+
periods : int
333+
Number of periods (or increments) to shift by,
334+
can be positive or negative.
335+
336+
.. versionchanged:: 0.24.0
330337
331338
Returns
332339
-------
333-
shifted : Period Array/Index
340+
pandas.PeriodIndex
341+
Shifted index.
342+
343+
See Also
344+
--------
345+
DatetimeIndex.shift : Shift values of DatetimeIndex.
334346
"""
335-
return self._time_shift(n)
347+
return self._time_shift(periods)
336348

337349
def _time_shift(self, n):
338350
values = self._ndarray_values + n * self.freq.n

pandas/core/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -8304,6 +8304,7 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
83048304
--------
83058305
Index.shift : Shift values of Index.
83068306
DatetimeIndex.shift : Shift values of DatetimeIndex.
8307+
PeriodIndex.shift : Shift values of PeriodIndex.
83078308
83088309
Notes
83098310
-----

pandas/tests/indexes/period/test_arithmetic.py

+9
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ def test_shift_gh8083(self):
9797
expected = PeriodIndex(['2013-01-02', '2013-01-03', '2013-01-04',
9898
'2013-01-05', '2013-01-06'], freq='D')
9999
tm.assert_index_equal(result, expected)
100+
101+
def test_shift_periods(self):
102+
# GH #22458 : argument 'n' was deprecated in favor of 'periods'
103+
idx = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009')
104+
tm.assert_index_equal(idx.shift(periods=0), idx)
105+
tm.assert_index_equal(idx.shift(0), idx)
106+
with tm.assert_produces_warning(FutureWarning,
107+
check_stacklevel=True):
108+
tm.assert_index_equal(idx.shift(n=0), idx)

0 commit comments

Comments
 (0)