From 81b67c4b7a1b3e3af3bbec573c22bee03b17d0cf Mon Sep 17 00:00:00 2001 From: Dale Jung Date: Wed, 18 Sep 2013 09:41:08 -0400 Subject: [PATCH] ENH: Panel.shift now uses NDFrame.shift. PRF: Adding Panels.shift test DOC: Panel.shift no longer drops periods. --- doc/source/release.rst | 2 ++ doc/source/v0.14.0.txt | 2 ++ pandas/core/panel.py | 29 +++-------------------------- pandas/tests/test_panel.py | 2 +- vb_suite/panel_methods.py | 19 +++++++++++++++++++ vb_suite/suite.py | 1 + 6 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 vb_suite/panel_methods.py diff --git a/doc/source/release.rst b/doc/source/release.rst index cd5b0cbd23353..4b87a78982662 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -152,6 +152,8 @@ API Changes - all offset operations now return ``Timestamp`` types (rather than datetime), Business/Week frequencies were incorrect (:issue:`4069`) +- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`) + Deprecations ~~~~~~~~~~~~ diff --git a/doc/source/v0.14.0.txt b/doc/source/v0.14.0.txt index 057f83bff44f2..29d503dcd7718 100644 --- a/doc/source/v0.14.0.txt +++ b/doc/source/v0.14.0.txt @@ -196,6 +196,8 @@ API changes covs = rolling_cov(df[['A','B','C']], df[['B','C','D']], 5, pairwise=True) covs[df.index[-1]] +- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`) + MultiIndexing Using Slicers ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 2bf50bb1bf142..eeb0e292c01d4 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -1116,8 +1116,7 @@ def count(self, axis='major'): def shift(self, lags, freq=None, axis='major'): """ - Shift major or minor axis by specified number of leads/lags. Drops - periods right now compared with DataFrame.shift + Shift major or minor axis by specified number of leads/lags. Parameters ---------- @@ -1128,35 +1127,13 @@ def shift(self, lags, freq=None, axis='major'): ------- shifted : Panel """ - values = self.values - items = self.items - major_axis = self.major_axis - minor_axis = self.minor_axis - if freq: return self.tshift(lags, freq, axis=axis) - if lags > 0: - vslicer = slice(None, -lags) - islicer = slice(lags, None) - elif lags == 0: - vslicer = islicer = slice(None) - else: - vslicer = slice(-lags, None) - islicer = slice(None, lags) - - axis = self._get_axis_name(axis) - if axis == 'major_axis': - values = values[:, vslicer, :] - major_axis = major_axis[islicer] - elif axis == 'minor_axis': - values = values[:, :, vslicer] - minor_axis = minor_axis[islicer] - else: + if axis == 'items': raise ValueError('Invalid axis') - return self._constructor(values, items=items, major_axis=major_axis, - minor_axis=minor_axis) + return super(Panel, self).shift(lags, freq=freq, axis=axis) def tshift(self, periods=1, freq=None, axis='major', **kwds): return super(Panel, self).tshift(periods, freq, axis, **kwds) diff --git a/pandas/tests/test_panel.py b/pandas/tests/test_panel.py index b2721689f574d..198e600e8edc7 100644 --- a/pandas/tests/test_panel.py +++ b/pandas/tests/test_panel.py @@ -1642,7 +1642,7 @@ def test_shift(self): # negative numbers, #2164 result = self.panel.shift(-1) - expected = Panel(dict((i, f.shift(-1)[:-1]) + expected = Panel(dict((i, f.shift(-1)) for i, f in compat.iteritems(self.panel))) assert_panel_equal(result, expected) diff --git a/vb_suite/panel_methods.py b/vb_suite/panel_methods.py new file mode 100644 index 0000000000000..6710be760e2df --- /dev/null +++ b/vb_suite/panel_methods.py @@ -0,0 +1,19 @@ +from vbench.api import Benchmark +from datetime import datetime + +common_setup = """from pandas_vb_common import * +""" + +#---------------------------------------------------------------------- +# shift + +setup = common_setup + """ +index = date_range(start="2000", freq="D", periods=1000) +panel = Panel(np.random.randn(100, len(index), 1000)) +""" + +panel_shift = Benchmark('panel.shift(1)', setup, + start_date=datetime(2012, 1, 12)) + +panel_shift_minor = Benchmark('panel.shift(1, axis=minor)', setup, + start_date=datetime(2012, 1, 12)) diff --git a/vb_suite/suite.py b/vb_suite/suite.py index 03f85da698ff8..a1b38e8509e4e 100644 --- a/vb_suite/suite.py +++ b/vb_suite/suite.py @@ -18,6 +18,7 @@ 'panel_ctor', 'packers', 'parser_vb', + 'panel_methods', 'plotting', 'reindex', 'replace',