Skip to content

Commit 8cbb579

Browse files
committed
Merge pull request #9890 from mortada/panel_shift
ENH: allow Panel.shift on items axis
2 parents cef3c85 + ba0e47f commit 8cbb579

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Enhancements
5252
- ``Period`` now accepts ``datetime64`` as value input. (:issue:`9054`)
5353

5454
- Allow timedelta string conversion when leading zero is missing from time definition, ie `0:00:00` vs `00:00:00`. (:issue:`9570`)
55+
- Allow Panel.shift with ``axis='items'`` (:issue:`9890`)
5556

5657
.. _whatsnew_0161.api:
5758

pandas/core/panel.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1184,13 +1184,17 @@ def count(self, axis='major'):
11841184
@deprecate_kwarg(old_arg_name='lags', new_arg_name='periods')
11851185
def shift(self, periods=1, freq=None, axis='major'):
11861186
"""
1187-
Shift major or minor axis by specified number of leads/lags. Drops
1188-
periods right now compared with DataFrame.shift
1187+
Shift index by desired number of periods with an optional time freq.
1188+
The shifted data will not include the dropped periods and the
1189+
shifted axis will be smaller than the original. This is different
1190+
from the behavior of DataFrame.shift()
11891191
11901192
Parameters
11911193
----------
1192-
lags : int
1193-
axis : {'major', 'minor'}
1194+
periods : int
1195+
Number of periods to move, can be positive or negative
1196+
freq : DateOffset, timedelta, or time rule string, optional
1197+
axis : {'items', 'major', 'minor'} or {0, 1, 2}
11941198
11951199
Returns
11961200
-------
@@ -1199,9 +1203,6 @@ def shift(self, periods=1, freq=None, axis='major'):
11991203
if freq:
12001204
return self.tshift(periods, freq, axis=axis)
12011205

1202-
if axis == 'items':
1203-
raise ValueError('Invalid axis')
1204-
12051206
return super(Panel, self).slice_shift(periods, axis=axis)
12061207

12071208
def tshift(self, periods=1, freq=None, axis='major', **kwds):

pandas/tests/test_panel.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1696,22 +1696,23 @@ def test_shift(self):
16961696
# major
16971697
idx = self.panel.major_axis[0]
16981698
idx_lag = self.panel.major_axis[1]
1699-
17001699
shifted = self.panel.shift(1)
1701-
17021700
assert_frame_equal(self.panel.major_xs(idx),
17031701
shifted.major_xs(idx_lag))
17041702

17051703
# minor
17061704
idx = self.panel.minor_axis[0]
17071705
idx_lag = self.panel.minor_axis[1]
1708-
17091706
shifted = self.panel.shift(1, axis='minor')
1710-
17111707
assert_frame_equal(self.panel.minor_xs(idx),
17121708
shifted.minor_xs(idx_lag))
17131709

1714-
self.assertRaises(Exception, self.panel.shift, 1, axis='items')
1710+
# items
1711+
idx = self.panel.items[0]
1712+
idx_lag = self.panel.items[1]
1713+
shifted = self.panel.shift(1, axis='items')
1714+
assert_frame_equal(self.panel[idx],
1715+
shifted[idx_lag])
17151716

17161717
# negative numbers, #2164
17171718
result = self.panel.shift(-1)

0 commit comments

Comments
 (0)