Skip to content

ENH: allow Panel.shift on items axis #9890

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 15, 2015
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.16.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
15 changes: 8 additions & 7 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand All @@ -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):
Expand Down
11 changes: 6 additions & 5 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down