Skip to content

Change Panel.shift to use NDFrame.shift #6605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions doc/source/v0.14.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
29 changes: 3 additions & 26 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions vb_suite/panel_methods.py
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions vb_suite/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'panel_ctor',
'packers',
'parser_vb',
'panel_methods',
'plotting',
'reindex',
'replace',
Expand Down