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
Changes from 1 commit
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
Next Next commit
ENH: Added NDFrame.slice_shift
added slice_shift func docs and removed freq argument
  • Loading branch information
dalejung committed Apr 27, 2014
commit 5e7bf20cf143b5764b25a93858a9b2f78e19e3bd
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