From ba0e47f07794aba37048a7034ac96cd43950ae24 Mon Sep 17 00:00:00 2001 From: Mortada Mehyar Date: Mon, 13 Apr 2015 19:43:10 -0700 Subject: [PATCH] ENH: allow Panel.shift on items axis --- doc/source/whatsnew/v0.16.1.txt | 1 + pandas/core/panel.py | 15 ++++++++------- pandas/tests/test_panel.py | 11 ++++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v0.16.1.txt b/doc/source/whatsnew/v0.16.1.txt index d4d8a05c66792..ffc08fa4ff169 100644 --- a/doc/source/whatsnew/v0.16.1.txt +++ b/doc/source/whatsnew/v0.16.1.txt @@ -52,6 +52,7 @@ Enhancements - ``Period`` now accepts ``datetime64`` as value input. (:issue:`9054`) - Allow timedelta string conversion when leading zero is missing from time definition, ie `0:00:00` vs `00:00:00`. (:issue:`9570`) +- Allow Panel.shift with ``axis='items'`` (:issue:`9890`) .. _whatsnew_0161.api: diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 7df23a54c737d..fb511bdd2a788 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1184,13 +1184,17 @@ def count(self, axis='major'): @deprecate_kwarg(old_arg_name='lags', new_arg_name='periods') def shift(self, periods=1, freq=None, axis='major'): """ - Shift major or minor axis by specified number of leads/lags. Drops - periods right now compared with DataFrame.shift + Shift index by desired number of periods with an optional time freq. + The shifted data will not include the dropped periods and the + shifted axis will be smaller than the original. This is different + from the behavior of DataFrame.shift() Parameters ---------- - lags : int - axis : {'major', 'minor'} + periods : int + Number of periods to move, can be positive or negative + freq : DateOffset, timedelta, or time rule string, optional + axis : {'items', 'major', 'minor'} or {0, 1, 2} Returns ------- @@ -1199,9 +1203,6 @@ def shift(self, periods=1, freq=None, axis='major'): if freq: return self.tshift(periods, freq, axis=axis) - if axis == 'items': - raise ValueError('Invalid axis') - return super(Panel, self).slice_shift(periods, axis=axis) def tshift(self, periods=1, freq=None, axis='major', **kwds): diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index 0fd03cb5804a8..a405fa78b2518 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1696,22 +1696,23 @@ def test_shift(self): # major idx = self.panel.major_axis[0] idx_lag = self.panel.major_axis[1] - shifted = self.panel.shift(1) - assert_frame_equal(self.panel.major_xs(idx), shifted.major_xs(idx_lag)) # minor idx = self.panel.minor_axis[0] idx_lag = self.panel.minor_axis[1] - shifted = self.panel.shift(1, axis='minor') - assert_frame_equal(self.panel.minor_xs(idx), shifted.minor_xs(idx_lag)) - self.assertRaises(Exception, self.panel.shift, 1, axis='items') + # items + idx = self.panel.items[0] + idx_lag = self.panel.items[1] + shifted = self.panel.shift(1, axis='items') + assert_frame_equal(self.panel[idx], + shifted[idx_lag]) # negative numbers, #2164 result = self.panel.shift(-1)