pandas 1.4.2

ParametersReturnsBackRef
shift(self: 'NDFrameT', periods=1, freq=None, axis=0, fill_value=None) -> 'NDFrameT'

When :None:None:`freq` is not passed, shift the index without realigning the data. If :None:None:`freq` is passed (in this case, the index must be date or datetime, or it will raise a :None:None:`NotImplementedError`), the index will be increased using the periods and the :None:None:`freq`. :None:None:`freq` can be inferred when specified as "infer" as long as either freq or inferred_freq attribute is set in the index.

Parameters

periods : int

Number of periods to shift. Can be positive or negative.

freq : DateOffset, tseries.offsets, timedelta, or str, optional

Offset to use from the tseries module or time rule (e.g. 'EOM'). If :None:None:`freq` is specified then the index values are shifted but the data is not realigned. That is, use :None:None:`freq` if you would like to extend the index when shifting and preserve the original data. If :None:None:`freq` is specified as "infer" then it will be inferred from the freq or inferred_freq attributes of the index. If neither of those attributes exist, a ValueError is thrown.

axis : {0 or 'index', 1 or 'columns', None}, default None

Shift direction.

fill_value : object, optional

The scalar value to use for newly introduced missing values. the default depends on the dtype of :None:None:`self`. For numeric data, np.nan is used. For datetime, timedelta, or period data, etc. NaT is used. For extension dtypes, self.dtype.na_value is used.

versionchanged

Returns

Series/DataFrame

Copy of input object, shifted.

Shift index by desired number of periods with an optional time :None:None:`freq`.

See Also

DatetimeIndex.shift

Shift values of DatetimeIndex.

Index.shift

Shift values of Index.

PeriodIndex.shift

Shift values of PeriodIndex.

tshift

Shift the time index, using the index's frequency if available.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({"Col1": [10, 20, 15, 30, 45],
...  "Col2": [13, 23, 18, 33, 48],
...  "Col3": [17, 27, 22, 37, 52]},
...  index=pd.date_range("2020-01-01", "2020-01-05"))
... df Col1 Col2 Col3 2020-01-01 10 13 17 2020-01-02 20 23 27 2020-01-03 15 18 22 2020-01-04 30 33 37 2020-01-05 45 48 52
This example is valid syntax, but we were not able to check execution
>>> df.shift(periods=3)
            Col1  Col2  Col3
2020-01-01   NaN   NaN   NaN
2020-01-02   NaN   NaN   NaN
2020-01-03   NaN   NaN   NaN
2020-01-04  10.0  13.0  17.0
2020-01-05  20.0  23.0  27.0
This example is valid syntax, but we were not able to check execution
>>> df.shift(periods=1, axis="columns")
            Col1  Col2  Col3
2020-01-01   NaN    10    13
2020-01-02   NaN    20    23
2020-01-03   NaN    15    18
2020-01-04   NaN    30    33
2020-01-05   NaN    45    48
This example is valid syntax, but we were not able to check execution
>>> df.shift(periods=3, fill_value=0)
            Col1  Col2  Col3
2020-01-01     0     0     0
2020-01-02     0     0     0
2020-01-03     0     0     0
2020-01-04    10    13    17
2020-01-05    20    23    27
This example is valid syntax, but we were not able to check execution
>>> df.shift(periods=3, freq="D")
            Col1  Col2  Col3
2020-01-04    10    13    17
2020-01-05    20    23    27
2020-01-06    15    18    22
2020-01-07    30    33    37
2020-01-08    45    48    52
This example is valid syntax, but we were not able to check execution
>>> df.shift(periods=3, freq="infer")
            Col1  Col2  Col3
2020-01-04    10    13    17
2020-01-05    20    23    27
2020-01-06    15    18    22
2020-01-07    30    33    37
2020-01-08    45    48    52
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.generic.NDFrame.slice_shift

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#9353
type: <class 'function'>
Commit: