- API reference
- DataFrame
- pandas.DataF...
pandas.DataFrame.shift#
- DataFrame.shift(periods=1,freq=None,axis=0,fill_value=<no_default>,suffix=None)[source]#
Shift index by desired number of periods with an optional timefreq.
Whenfreq is not passed, shift the index without realigning the data.Iffreq is passed (in this case, the index must be date or datetime,or it will raise aNotImplementedError), the index will beincreased using the periods and thefreq.freq can be inferredwhen specified as “infer” as long as either freq or inferred_freqattribute is set in the index.
- Parameters:
- periodsint or Sequence
Number of periods to shift. Can be positive or negative.If an iterable of ints, the data will be shifted once by each int.This is equivalent to shifting by one value at a time andconcatenating all resulting frames. The resulting columns will havethe shift suffixed to their column names. For multiple periods,axis must not be 1.
- freqDateOffset, tseries.offsets, timedelta, or str, optional
Offset to use from the tseries module or time rule (e.g. ‘EOM’).Iffreq is specified then the index values are shifted but thedata is not realigned. That is, usefreq if you would like toextend the index when shifting and preserve the original data.Iffreq is specified as “infer” then it will be inferred fromthe freq or inferred_freq attributes of the index. If neither ofthose attributes exist, a ValueError is thrown.
- axis{0 or ‘index’, 1 or ‘columns’, None}, default None
Shift direction. ForSeries this parameter is unused and defaults to 0.
- fill_valueobject, optional
The scalar value to use for newly introduced missing values.the default depends on the dtype ofself.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.- suffixstr, optional
If str and periods is an iterable, this is added after the columnname and before the shift value for each shifted column name.
- Returns:
- DataFrame
Copy of input object, shifted.
See also
Index.shift
Shift values of Index.
DatetimeIndex.shift
Shift values of DatetimeIndex.
PeriodIndex.shift
Shift values of PeriodIndex.
Examples
>>>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 Col32020-01-01 10 13 172020-01-02 20 23 272020-01-03 15 18 222020-01-04 30 33 372020-01-05 45 48 52
>>>df.shift(periods=3) Col1 Col2 Col32020-01-01 NaN NaN NaN2020-01-02 NaN NaN NaN2020-01-03 NaN NaN NaN2020-01-04 10.0 13.0 17.02020-01-05 20.0 23.0 27.0
>>>df.shift(periods=1,axis="columns") Col1 Col2 Col32020-01-01 NaN 10 132020-01-02 NaN 20 232020-01-03 NaN 15 182020-01-04 NaN 30 332020-01-05 NaN 45 48
>>>df.shift(periods=3,fill_value=0) Col1 Col2 Col32020-01-01 0 0 02020-01-02 0 0 02020-01-03 0 0 02020-01-04 10 13 172020-01-05 20 23 27
>>>df.shift(periods=3,freq="D") Col1 Col2 Col32020-01-04 10 13 172020-01-05 20 23 272020-01-06 15 18 222020-01-07 30 33 372020-01-08 45 48 52
>>>df.shift(periods=3,freq="infer") Col1 Col2 Col32020-01-04 10 13 172020-01-05 20 23 272020-01-06 15 18 222020-01-07 30 33 372020-01-08 45 48 52
>>>df['Col1'].shift(periods=[0,1,2]) Col1_0 Col1_1 Col1_22020-01-01 10 NaN NaN2020-01-02 20 10.0 NaN2020-01-03 15 20.0 10.02020-01-04 30 15.0 20.02020-01-05 45 30.0 15.0