w3resource

Pandas Series: shift() function

Series shift() function

The shift() function is used to 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 will be increased using the periods and the freq.

Syntax:

Series.shift(self, periods=1, freq=None, axis=0, fill_value=None)
Pandas Series shift image

Parameters:

Name Description Type/Default Value Required / Optional
periods   Number of periods to shift. Can be positive or negative. int Required
freq Offset to use from the tseries module or time rule (e.g. ‘EOM’). If freq is specified then the index values are shifted but the data is not realigned. That is, use freq if you would like to extend the index when shifting and preserve the original data. DateOffset, tseries.offsets, timedelta, or str,  Optional
axis Shift direction. {0 or ‘index’, 1 or ‘columns’, None}
Default Value: None
Required
fill_value The scalar value to use for newly introduced missing values. the default depends on the dtype of 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. object Optional

Returns: Series - Copy of input object, shifted.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'C1': [20, 30, 25, 40, 55],
                   'C2': [23, 33, 18, 36, 58],
                   'C3': [27, 37, 21, 47, 62]})
df.shift(periods=3)

Output:

              C1   C2	 C3
0	         NaN   NaN	NaN
1	         NaN   NaN	NaN
2	         NaN   NaN	NaN
3	        20.0   23.0	27.0
4	        30.0   33.0	37.0
Pandas Series shift image

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'C1': [20, 30, 25, 40, 55],
                   'C2': [23, 33, 18, 36, 58],
                   'C3': [27, 37, 21, 47, 62]})
df.shift(periods=1, axis='columns')

Output:

   C1  C2	     C3
0	NaN	20.0	23.0
1	NaN	30.0	33.0
2	NaN	25.0	18.0
3	NaN	40.0	36.0
4	NaN	55.0	58.0

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'C1': [20, 30, 25, 40, 55],
                   'C2': [23, 33, 18, 36, 58],
                   'C3': [27, 37, 21, 47, 62]})
df.shift(periods=3, fill_value=0)

Output:

 C1	C2	C3
0	0	0	0
1	0	0	0
2	0	0	0
3	20	23	27
4	30	33	37

Previous: Get the last row(s) without any NaNs in Pandas series
Next: Resample Pandas time-series data



Follow us on Facebook and Twitter for latest update.