Shift index by desired number of periods with an optional time `freq`. When `freq` is not passed, shift the index without realigning the data. If `freq` is passed (in this case, the index must be date or datetime, or it will raise a `NotImplementedError`), the index
(
self,
periods: int | Sequence[int] = 1,
freq: Frequency | None = None,
axis: Axis = 0,
fill_value: Hashable = lib.no_default,
suffix: str | None = None,
)
| 6578 | return res if inplace else res.__finalize__(self) |
| 6579 | |
| 6580 | def shift( |
| 6581 | self, |
| 6582 | periods: int | Sequence[int] = 1, |
| 6583 | freq: Frequency | None = None, |
| 6584 | axis: Axis = 0, |
| 6585 | fill_value: Hashable = lib.no_default, |
| 6586 | suffix: str | None = None, |
| 6587 | ) -> DataFrame: |
| 6588 | """ |
| 6589 | Shift index by desired number of periods with an optional time `freq`. |
| 6590 | |
| 6591 | When `freq` is not passed, shift the index without realigning the data. |
| 6592 | If `freq` is passed (in this case, the index must be date or datetime, |
| 6593 | or it will raise a `NotImplementedError`), the index will be |
| 6594 | increased using the periods and the `freq`. `freq` can be inferred |
| 6595 | when specified as "infer" as long as either freq or inferred_freq |
| 6596 | attribute is set in the index. |
| 6597 | |
| 6598 | Parameters |
| 6599 | ---------- |
| 6600 | periods : int or Sequence |
| 6601 | Number of periods to shift. Can be positive or negative. |
| 6602 | If an iterable of ints, the data will be shifted once by each int. |
| 6603 | This is equivalent to shifting by one value at a time and |
| 6604 | concatenating all resulting frames. The resulting columns will have |
| 6605 | the shift suffixed to their column names. For multiple periods, |
| 6606 | axis must not be 1. |
| 6607 | freq : DateOffset, tseries.offsets, timedelta, or str, optional |
| 6608 | Offset to use from the tseries module or time rule (e.g. 'EOM'). |
| 6609 | If `freq` is specified then the index values are shifted but the |
| 6610 | data is not realigned. That is, use `freq` if you would like to |
| 6611 | extend the index when shifting and preserve the original data. |
| 6612 | If `freq` is specified as "infer" then it will be inferred from |
| 6613 | the freq or inferred_freq attributes of the index. If neither of |
| 6614 | those attributes exist, a ValueError is thrown. |
| 6615 | axis : {0 or 'index', 1 or 'columns', None}, default None |
| 6616 | Shift direction. For `Series` this parameter is unused and defaults to 0. |
| 6617 | fill_value : object, optional |
| 6618 | The scalar value to use for newly introduced missing values. |
| 6619 | the default depends on the dtype of `self`. |
| 6620 | For Boolean and numeric NumPy data types, ``np.nan`` is used. |
| 6621 | For datetime, timedelta, or period data, etc. :attr:`NaT` is used. |
| 6622 | For extension dtypes, ``self.dtype.na_value`` is used. |
| 6623 | suffix : str, optional |
| 6624 | If str and periods is an iterable, this is added after the column |
| 6625 | name and before the shift value for each shifted column name. |
| 6626 | For `Series` this parameter is unused and defaults to `None`. |
| 6627 | |
| 6628 | Returns |
| 6629 | ------- |
| 6630 | DataFrame |
| 6631 | Copy of input object, shifted. |
| 6632 | |
| 6633 | See Also |
| 6634 | -------- |
| 6635 | Index.shift : Shift values of Index. |
| 6636 | DatetimeIndex.shift : Shift values of DatetimeIndex. |
| 6637 | PeriodIndex.shift : Shift values of PeriodIndex. |