Skip to content

Panel shift revert #6974

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 2 commits into from
Apr 28, 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: 1 addition & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ API Changes
to a non-unique item in the ``Index`` (previously raised a ``KeyError``). (:issue:`6738`)
- all offset operations now return ``Timestamp`` types (rather than datetime), Business/Week frequencies were incorrect (:issue:`4069`)
- ``Series.iteritems()`` is now lazy (returns an iterator rather than a list). This was the documented behavior prior to 0.14. (:issue:`6760`)
- ``Panel.shift`` now uses ``NDFrame.shift``. It no longer drops the ``nan`` data and retains its original shape. (:issue:`4867`)
- ``to_excel`` now converts ``np.inf`` into a string representation,
customizable by the ``inf_rep`` keyword argument (Excel has no native inf
representation) (:issue:`6782`)
Expand Down Expand Up @@ -424,6 +423,7 @@ Bug Fixes
- Bug in ``DataFrame.apply`` with functions that used *args or **kwargs and returned
an empty result (:issue:`6952`)
- Bug in sum/mean on 32-bit platforms on overflows (:issue:`6915`)
- Moved ``Panel.shift`` to ``NDFrame.slice_shift`` and fixed to respect multiple dtypes. (:issue:`6959`)

pandas 0.13.1
-------------
Expand Down
1 change: 0 additions & 1 deletion doc/source/v0.14.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ API changes
covs[df.index[-1]]

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

- Added ``nunique`` and ``value_counts`` functions to ``Index`` for counting unique elements. (:issue:`6734`)
- ``stack`` and ``unstack`` now raise a ``ValueError`` when the ``level`` keyword refers
Expand Down
36 changes: 36 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,42 @@ def shift(self, periods=1, freq=None, axis=0, **kwds):

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

def slice_shift(self, periods=1, axis=0, **kwds):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% happy with this (I like your soln better), but perf is odd. can we do this inside of Block instead? (e.g. just automatically do it if ndim >= 3), a bit cleaner that way and all the code in the same place

"""
Equivalent to `shift` without copying data. The shifted data will
not include the dropped periods and the shifted axis will be smaller
than the original.

Parameters
----------
periods : int
Number of periods to move, can be positive or negative

Notes
-----
While the `slice_shift` is faster than `shift`, you may pay for it
later during alignment.

Returns
-------
shifted : same type as caller
"""
if periods == 0:
return self

if periods > 0:
vslicer = slice(None, -periods)
islicer = slice(periods, None)
else:
vslicer = slice(-periods, None)
islicer = slice(None, periods)

new_obj = self._slice(vslicer, axis=axis)
shifted_axis = self._get_axis(axis)[islicer]
new_obj.set_axis(axis, shifted_axis)

return new_obj.__finalize__(self)

def tshift(self, periods=1, freq=None, axis=0, **kwds):
"""
Shift the time index, using the index's frequency if available
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,8 @@ 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.
Shift major or minor axis by specified number of leads/lags. Drops
periods right now compared with DataFrame.shift

Parameters
----------
Expand All @@ -1171,7 +1172,7 @@ def shift(self, periods=1, freq=None, axis='major'):
if axis == 'items':
raise ValueError('Invalid axis')

return super(Panel, self).shift(periods, freq=freq, axis=axis)
return super(Panel, self).slice_shift(periods, axis=axis)

def tshift(self, periods=1, freq=None, axis='major', **kwds):
return super(Panel, self).tshift(periods, freq, axis, **kwds)
Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
assert_almost_equal,
ensure_clean,
assertRaisesRegexp,
makeCustomDataframe as mkdf
makeCustomDataframe as mkdf,
makeMixedDataFrame
)
import pandas.core.panel as panelm
import pandas.util.testing as tm
Expand Down Expand Up @@ -1652,10 +1653,17 @@ def test_shift(self):

# negative numbers, #2164
result = self.panel.shift(-1)
expected = Panel(dict((i, f.shift(-1))
expected = Panel(dict((i, f.shift(-1)[:-1])
for i, f in compat.iteritems(self.panel)))
assert_panel_equal(result, expected)

# mixed dtypes #6959
data = [('item '+ch, makeMixedDataFrame()) for ch in list('abcde')]
data = dict(data)
mixed_panel = Panel.from_dict(data, orient='minor')
shifted = mixed_panel.shift(1)
assert_series_equal(mixed_panel.dtypes, shifted.dtypes)

def test_tshift(self):
# PeriodIndex
ps = tm.makePeriodPanel()
Expand Down