datatable.shift()

Shift columns’ data forward or backwards. This function is group-aware, i.e. in the presence of a groupby it will shift data separately within each group.

Parameters

cols
FExpr

Input data to be shifted.

n
int

The shift amount. For positive n, the data are shifted forward, i.e. a “lag” column is created. For negative n, the data are shifted backwards creating a “lead” column.

return
FExpr

f-expression that shifts input data by n rows. The resulting data will have the same number of rows as cols. Depending on n, i.e. positive/negative, n observations in the beginning/end will become missing and n observations at the end/beginning will be discarded.

Examples

from datatable import dt, f, by DT = dt.Frame({"object": [1, 1, 1, 2, 2], "period": [1, 2, 4, 4, 23], "value": [24, 67, 89, 5, 23]}) DT
objectperiodvalue
int32int32int32
01124
11267
21489
3245
422323

Shift forward by creating a “lag” column:

DT[:, dt.shift(f.period, n = 3)]
period
int32
0NA
1NA
2NA
31
42

Shift backwards by creating “lead” columns:

DT[:, dt.shift(f[:], n = -3)]
objectperiodvalue
int32int32int32
0245
122323
2NANANA
3NANANA
4NANANA

Shift within groups, i.e. in the presence of by():

DT[:, f[:].extend({"prev_value": dt.shift(f.value)}), by("object")]
objectperiodvalueprev_value
int32int32int32int32
01124NA
1126724
2148967
3245NA
4223235