Skip to content

Commit

Permalink
DOC: updating docstring of Index.shift (pandas-dev#19996)
Browse files Browse the repository at this point in the history
  • Loading branch information
DataOmbudsman authored and jreback committed Mar 7, 2018
1 parent 09c416c commit dd2b224
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2211,13 +2211,55 @@ def sortlevel(self, level=None, ascending=True, sort_remaining=None):
return self.sort_values(return_indexer=True, ascending=ascending)

def shift(self, periods=1, freq=None):
"""
Shift Index containing datetime objects by input number of periods and
DateOffset
"""Shift index by desired number of time frequency increments.
This method is for shifting the values of datetime-like indexes
by a specified time increment a given number of times.
Parameters
----------
periods : int
Number of periods (or increments) to shift by,
can be positive or negative (default is 1).
freq : pandas.DateOffset, pandas.Timedelta or string
Frequency increment to shift by (default is None).
If None, the index is shifted by its own `freq` attribute.
Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc.
Returns
-------
shifted : Index
pandas.Index
shifted index
Examples
--------
Put the first 5 month starts of 2011 into an index.
>>> month_starts = pd.date_range('1/1/2011', periods=5, freq='MS')
>>> month_starts
DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01', '2011-04-01',
'2011-05-01'],
dtype='datetime64[ns]', freq='MS')
Shift the index by 10 days.
>>> month_starts.shift(10, freq='D')
DatetimeIndex(['2011-01-11', '2011-02-11', '2011-03-11', '2011-04-11',
'2011-05-11'],
dtype='datetime64[ns]', freq=None)
The default value of `freq` is the `freq` attribute of the index,
which is 'MS' (month start) in this example.
>>> month_starts.shift(10)
DatetimeIndex(['2011-11-01', '2011-12-01', '2012-01-01', '2012-02-01',
'2012-03-01'],
dtype='datetime64[ns]', freq='MS')
Notes
-----
This method is only implemented for datetime-like index classes,
i.e., DatetimeIndex, PeriodIndex and TimedeltaIndex.
"""
raise NotImplementedError("Not supported for type %s" %
type(self).__name__)
Expand Down

0 comments on commit dd2b224

Please sign in to comment.