Pandas DataFrame: shift() function
DataFrame - shift() function
The shift() function is used to shift index by desired number of periods with an optional time freq.
Syntax:
DataFrame.shift(self, periods=1, freq=None, axis=0, fill_value=None)
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 Default Value: 0 |
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 |
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.
Returns: DataFrame
Copy of input object, shifted.
Example:
Download the Pandas DataFrame Notebooks from here.
Previous: DataFrame - asof() function
Next: DataFrame - resample() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/dataframe/dataframe-shift.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics