From 022688f2c13181b22d70770adc4bee80f5aa9461 Mon Sep 17 00:00:00 2001 From: arminv Date: Sun, 30 Sep 2018 14:46:23 -0400 Subject: [PATCH 1/3] initial commit --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/core/arrays/datetimelike.py | 1 + pandas/core/arrays/period.py | 27 ++++++++++++++----- pandas/core/generic.py | 1 + .../tests/indexes/period/test_arithmetic.py | 9 +++++++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 3e1711edb0f27..de1a0b6da6e26 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -573,7 +573,7 @@ Deprecations - :meth:`Series.str.cat` has deprecated using arbitrary list-likes *within* list-likes. A list-like container may still contain many ``Series``, ``Index`` or 1-dimensional ``np.ndarray``, or alternatively, only scalar values. (:issue:`21950`) - :meth:`FrozenNDArray.searchsorted` has deprecated the ``v`` parameter in favor of ``value`` (:issue:`14645`) -- :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`) +- :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`) .. _whatsnew_0240.prior_deprecations: diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 91c119808db52..346a1e28e08ce 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -552,6 +552,7 @@ def shift(self, periods, freq=None): See Also -------- Index.shift : Shift values of Index. + PeriodIndex.shift : Shift values of PeriodIndex. """ if freq is not None and freq != self.freq: if isinstance(freq, compat.string_types): diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 481d5313f0e25..3e80c352e6ed5 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -14,7 +14,7 @@ from pandas._libs.tslibs.fields import isleapyear_arr from pandas import compat -from pandas.util._decorators import cache_readonly +from pandas.util._decorators import (cache_readonly, deprecate_kwarg) from pandas.core.dtypes.common import ( is_integer_dtype, is_float_dtype, is_period_dtype) @@ -319,20 +319,33 @@ def _add_delta(self, other): ordinal_delta = self._maybe_convert_timedelta(other) return self.shift(ordinal_delta) - def shift(self, n): + @deprecate_kwarg(old_arg_name='n', new_arg_name='periods') + def shift(self, periods): """ - Specialized shift which produces an Period Array/Index + Shift index by desired number of increments. + + This method is for shifting the values of period indexes + by a specified time increment. Parameters ---------- - n : int - Periods to shift by + periods : int + Number of periods (or increments) to shift by, + can be positive or negative. + + .. versionchanged:: 0.24.0 Returns ------- - shifted : Period Array/Index + pandas.PeriodIndex + Shifted index. + + See Also + -------- + Index.shift : Shift values of Index. + DatetimeIndex.shift : Shift values of DatetimeIndex. """ - values = self._ndarray_values + n * self.freq.n + values = self._ndarray_values + periods * self.freq.n if self.hasnans: values[self._isnan] = iNaT return self._shallow_copy(values=values) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 393e7caae5fab..7123bd856d7f4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8294,6 +8294,7 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None, -------- Index.shift : Shift values of Index. DatetimeIndex.shift : Shift values of DatetimeIndex. + PeriodIndex.shift : Shift values of PeriodIndex. Notes ----- diff --git a/pandas/tests/indexes/period/test_arithmetic.py b/pandas/tests/indexes/period/test_arithmetic.py index 3380a1ebc58dc..d9cbb3ea27d7b 100644 --- a/pandas/tests/indexes/period/test_arithmetic.py +++ b/pandas/tests/indexes/period/test_arithmetic.py @@ -97,3 +97,12 @@ def test_shift_gh8083(self): expected = PeriodIndex(['2013-01-02', '2013-01-03', '2013-01-04', '2013-01-05', '2013-01-06'], freq='D') tm.assert_index_equal(result, expected) + + def test_shift_periods(self): + # GH #22458 : argument 'n' was deprecated in favor of 'periods' + idx = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009') + tm.assert_index_equal(idx.shift(periods=0), idx) + tm.assert_index_equal(idx.shift(0), idx) + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=True): + tm.assert_index_equal(idx.shift(n=0), idx) From 4cfeb928906f8ccf4cbc0813ded535fb2cb1c16b Mon Sep 17 00:00:00 2001 From: arminv Date: Mon, 8 Oct 2018 14:08:37 -0400 Subject: [PATCH 2/3] typo --- pandas/core/arrays/period.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 8f4910caa5ed4..723b6ac8b82a1 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -344,14 +344,11 @@ def shift(self, periods): -------- DatetimeIndex.shift : Shift values of DatetimeIndex. """ -<<<<<<< HEAD values = self._ndarray_values + periods * self.freq.n -======= return self._time_shift(n) def _time_shift(self, n): values = self._ndarray_values + n * self.freq.n ->>>>>>> upstream/master if self.hasnans: values[self._isnan] = iNaT return self._shallow_copy(values=values) From d5586e47ccc54dbbdd0fcb888def937119f9cebe Mon Sep 17 00:00:00 2001 From: arminv Date: Mon, 8 Oct 2018 14:23:35 -0400 Subject: [PATCH 3/3] periods in internal _time_shift --- pandas/core/arrays/period.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 723b6ac8b82a1..96a18a628fcf9 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -344,8 +344,7 @@ def shift(self, periods): -------- DatetimeIndex.shift : Shift values of DatetimeIndex. """ - values = self._ndarray_values + periods * self.freq.n - return self._time_shift(n) + return self._time_shift(periods) def _time_shift(self, n): values = self._ndarray_values + n * self.freq.n