Skip to content

Commit a4870b5

Browse files
committed
Merge pull request #6974 from dalejung/panel_shift_revert
Panel shift revert
2 parents 4614ac8 + 81675b6 commit a4870b5

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

doc/source/release.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ API Changes
151151
to a non-unique item in the ``Index`` (previously raised a ``KeyError``). (:issue:`6738`)
152152
- all offset operations now return ``Timestamp`` types (rather than datetime), Business/Week frequencies were incorrect (:issue:`4069`)
153153
- ``Series.iteritems()`` is now lazy (returns an iterator rather than a list). This was the documented behavior prior to 0.14. (:issue:`6760`)
154-
- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`)
155154
- ``to_excel`` now converts ``np.inf`` into a string representation,
156155
customizable by the ``inf_rep`` keyword argument (Excel has no native inf
157156
representation) (:issue:`6782`)
@@ -435,6 +434,7 @@ Bug Fixes
435434
- Bug in ``DataFrame.apply`` with functions that used \*args`` or \*\*kwargs and returned
436435
an empty result (:issue:`6952`)
437436
- Bug in sum/mean on 32-bit platforms on overflows (:issue:`6915`)
437+
- Moved ``Panel.shift`` to ``NDFrame.slice_shift`` and fixed to respect multiple dtypes. (:issue:`6959`)
438438

439439
pandas 0.13.1
440440
-------------

doc/source/v0.14.0.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ API changes
205205
covs[df.index[-1]]
206206

207207
- ``Series.iteritems()`` is now lazy (returns an iterator rather than a list). This was the documented behavior prior to 0.14. (:issue:`6760`)
208-
- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`)
209208

210209
- Added ``nunique`` and ``value_counts`` functions to ``Index`` for counting unique elements. (:issue:`6734`)
211210
- ``stack`` and ``unstack`` now raise a ``ValueError`` when the ``level`` keyword refers

pandas/core/generic.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,6 +3231,42 @@ def shift(self, periods=1, freq=None, axis=0, **kwds):
32313231

32323232
return self._constructor(new_data).__finalize__(self)
32333233

3234+
def slice_shift(self, periods=1, axis=0, **kwds):
3235+
"""
3236+
Equivalent to `shift` without copying data. The shifted data will
3237+
not include the dropped periods and the shifted axis will be smaller
3238+
than the original.
3239+
3240+
Parameters
3241+
----------
3242+
periods : int
3243+
Number of periods to move, can be positive or negative
3244+
3245+
Notes
3246+
-----
3247+
While the `slice_shift` is faster than `shift`, you may pay for it
3248+
later during alignment.
3249+
3250+
Returns
3251+
-------
3252+
shifted : same type as caller
3253+
"""
3254+
if periods == 0:
3255+
return self
3256+
3257+
if periods > 0:
3258+
vslicer = slice(None, -periods)
3259+
islicer = slice(periods, None)
3260+
else:
3261+
vslicer = slice(-periods, None)
3262+
islicer = slice(None, periods)
3263+
3264+
new_obj = self._slice(vslicer, axis=axis)
3265+
shifted_axis = self._get_axis(axis)[islicer]
3266+
new_obj.set_axis(axis, shifted_axis)
3267+
3268+
return new_obj.__finalize__(self)
3269+
32343270
def tshift(self, periods=1, freq=None, axis=0, **kwds):
32353271
"""
32363272
Shift the time index, using the index's frequency if available

pandas/core/panel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,8 @@ def count(self, axis='major'):
11541154
@deprecate_kwarg(old_arg_name='lags', new_arg_name='periods')
11551155
def shift(self, periods=1, freq=None, axis='major'):
11561156
"""
1157-
Shift major or minor axis by specified number of leads/lags.
1157+
Shift major or minor axis by specified number of leads/lags. Drops
1158+
periods right now compared with DataFrame.shift
11581159
11591160
Parameters
11601161
----------
@@ -1171,7 +1172,7 @@ def shift(self, periods=1, freq=None, axis='major'):
11711172
if axis == 'items':
11721173
raise ValueError('Invalid axis')
11731174

1174-
return super(Panel, self).shift(periods, freq=freq, axis=axis)
1175+
return super(Panel, self).slice_shift(periods, axis=axis)
11751176

11761177
def tshift(self, periods=1, freq=None, axis='major', **kwds):
11771178
return super(Panel, self).tshift(periods, freq, axis, **kwds)

pandas/tests/test_panel.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
assert_almost_equal,
2222
ensure_clean,
2323
assertRaisesRegexp,
24-
makeCustomDataframe as mkdf
24+
makeCustomDataframe as mkdf,
25+
makeMixedDataFrame
2526
)
2627
import pandas.core.panel as panelm
2728
import pandas.util.testing as tm
@@ -1652,10 +1653,17 @@ def test_shift(self):
16521653

16531654
# negative numbers, #2164
16541655
result = self.panel.shift(-1)
1655-
expected = Panel(dict((i, f.shift(-1))
1656+
expected = Panel(dict((i, f.shift(-1)[:-1])
16561657
for i, f in compat.iteritems(self.panel)))
16571658
assert_panel_equal(result, expected)
16581659

1660+
# mixed dtypes #6959
1661+
data = [('item '+ch, makeMixedDataFrame()) for ch in list('abcde')]
1662+
data = dict(data)
1663+
mixed_panel = Panel.from_dict(data, orient='minor')
1664+
shifted = mixed_panel.shift(1)
1665+
assert_series_equal(mixed_panel.dtypes, shifted.dtypes)
1666+
16591667
def test_tshift(self):
16601668
# PeriodIndex
16611669
ps = tm.makePeriodPanel()

0 commit comments

Comments
 (0)