pandas.Index.shift#
- Index.shift(periods=1, freq=None)[source] #
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:
- periodsint, default 1
Number of periods (or increments) to shift by, can be positive or negative.
- freqpandas.DateOffset, pandas.Timedelta or str, optional
Frequency increment to shift by. If None, the index is shifted by its own freq attribute. Offset aliases are valid strings, e.g., ‘D’, ‘W’, ‘M’ etc.
- Returns:
- pandas.Index
Shifted index.
See also
Series.shift
Shift values of Series.
Notes
This method is only implemented for datetime-like index classes, i.e., DatetimeIndex, PeriodIndex and TimedeltaIndex.
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')