Skip to content

Commit 5e7bf20

Browse files
committed
ENH: Added NDFrame.slice_shift
added slice_shift func docs and removed freq argument
1 parent 3b13646 commit 5e7bf20

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pandas/core/generic.py

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

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

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

0 commit comments

Comments
 (0)