pyspark.pandas.groupby.GroupBy.shift¶
-
GroupBy.
shift
(periods: int = 1, fill_value: Optional[Any] = None) → FrameLike¶ Shift each group by periods observations.
- Parameters
- periodsinteger, default 1
number of periods to shift
- fill_valueoptional
- Returns
- Series or DataFrame
Object shifted within each group.
Examples
>>> df = ps.DataFrame({ ... 'a': [1, 1, 1, 2, 2, 2, 3, 3, 3], ... 'b': [1, 2, 2, 2, 3, 3, 3, 4, 4]}, columns=['a', 'b']) >>> df a b 0 1 1 1 1 2 2 1 2 3 2 2 4 2 3 5 2 3 6 3 3 7 3 4 8 3 4
>>> df.groupby('a').shift().sort_index() b 0 NaN 1 1.0 2 2.0 3 NaN 4 2.0 5 3.0 6 NaN 7 3.0 8 4.0
>>> df.groupby('a').shift(periods=-1, fill_value=0).sort_index() b 0 2 1 2 2 0 3 3 4 3 5 0 6 4 7 4 8 0