MCPcopy
hub / github.com/pandas-dev/pandas / shift

Method shift

pandas/core/generic.py:10487–10635  ·  view source on GitHub ↗

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=None,
        axis: Axis = 0,
        fill_value: Hashable = lib.no_default,
        suffix: str | None = None,
    )

Source from the content-addressed store, hash-verified

10485 )
10486
10487 def shift(
10488 self,
10489 periods: int | Sequence[int] = 1,
10490 freq=None,
10491 axis: Axis = 0,
10492 fill_value: Hashable = lib.no_default,
10493 suffix: str | None = None,
10494 ) -> Self | DataFrame:
10495 """
10496 Shift index by desired number of periods with an optional time `freq`.
10497
10498 When `freq` is not passed, shift the index without realigning the data.
10499 If `freq` is passed (in this case, the index must be date or datetime,
10500 or it will raise a `NotImplementedError`), the index will be
10501 increased using the periods and the `freq`. `freq` can be inferred
10502 when specified as "infer" as long as either freq or inferred_freq
10503 attribute is set in the index.
10504
10505 Parameters
10506 ----------
10507 periods : int or Sequence
10508 Number of periods to shift. Can be positive or negative.
10509 If an iterable of ints, the data will be shifted once by each int.
10510 This is equivalent to shifting by one value at a time and
10511 concatenating all resulting frames. The resulting columns will have
10512 the shift suffixed to their column names. For multiple periods,
10513 axis must not be 1.
10514 freq : DateOffset, tseries.offsets, timedelta, or str, optional
10515 Offset to use from the tseries module or time rule (e.g. 'EOM').
10516 If `freq` is specified then the index values are shifted but the
10517 data is not realigned. That is, use `freq` if you would like to
10518 extend the index when shifting and preserve the original data.
10519 If `freq` is specified as "infer" then it will be inferred from
10520 the freq or inferred_freq attributes of the index. If neither of
10521 those attributes exist, a ValueError is thrown.
10522 axis : {0 or 'index', 1 or 'columns', None}, default None
10523 Shift direction. For `Series` this parameter is unused and defaults to 0.
10524 fill_value : object, optional
10525 The scalar value to use for newly introduced missing values.
10526 the default depends on the dtype of `self`.
10527 For Boolean and numeric NumPy data types, ``np.nan`` is used.
10528 For datetime, timedelta, or period data, etc. :attr:`NaT` is used.
10529 For extension dtypes, ``self.dtype.na_value`` is used.
10530 suffix : str, optional
10531 If str and periods is an iterable, this is added after the column
10532 name and before the shift value for each shifted column name.
10533 For `Series` this parameter is unused and defaults to `None`.
10534
10535 Returns
10536 -------
10537 Series/DataFrame
10538 Copy of input object, shifted.
10539
10540 See Also
10541 --------
10542 Index.shift : Shift values of Index.
10543 DatetimeIndex.shift : Shift values of DatetimeIndex.
10544 PeriodIndex.shift : Shift values of PeriodIndex.

Callers 2

pct_changeMethod · 0.95
_shift_with_freqMethod · 0.45

Calls 6

_get_axis_numberMethod · 0.95
copyMethod · 0.95
_shift_with_freqMethod · 0.95
__finalize__Method · 0.80
to_frameMethod · 0.45
_constructor_from_mgrMethod · 0.45

Tested by

no test coverage detected