Sub topic 3
Looking ahead with negative periods
This chapter assumes you can create a pandas Series with a DatetimeIndex and that you know what shift(1) does. A positive periods value gives a lag. A negative value gives a lead.
What a negative shift does
shift(1) moves values forward in time, so each row shows the previous row's value. shift(-1) moves them backward, so each row shows the next row's value.
import pandas as pd
s = pd.Series(
[10, 12, 15, 11],
index=pd.date_range("2024-01-01", periods=4, freq="D"),
name="sales",
)
lead1 = s.shift(-1)
print(lead1)The output is:
2024-01-01 12.0
2024-01-02 15.0
2024-01-03 11.0
2024-01-04 NaN
Freq: D, Name: sales, dtype: float64On 2024-01-01 the lead column holds 12, which is the value recorded on 2024-01-02. The last date has no later row to borrow from, so it becomes NaN. The integer column also turns into float64, because integer dtypes cannot hold NaN.
The NaN moves to the opposite end compared with a lag. A lag leaves NaN at the start. A lead leaves it at the end.
Larger leads
The size of the negative number sets how many rows you look ahead. shift(-2) shows the value from two rows later and leaves two NaN values at the end.
print(s.shift(-2))The output is:
2024-01-01 15.0
2024-01-02 11.0
2024-01-03 NaN
2024-01-04 NaN
Freq: D, Name: sales, dtype: float64Each extra step of lead removes one more usable row at the end of the data.
Common uses of a lead
A lead is useful when you want to compare today with what happens next. Two cases come up often.
- Forward change. Subtract today's value from the next value to see how much the series will move.
- Prediction target. For a forecasting model, the thing you want to predict at row
tis the value att + 1. Shifting the column by -1 puts that answer on the same row as the inputs.
df = pd.DataFrame({"sales": s})
df["next_sales"] = df["sales"].shift(-1)
df["next_change"] = df["next_sales"] - df["sales"]
print(df)The output is:
sales next_sales next_change
2024-01-01 10 12.0 2.0
2024-01-02 12 15.0 3.0
2024-01-03 15 11.0 -4.0
2024-01-04 11 NaN NaNHere next_change on 2024-01-02 is 3.0 because sales rose from 12 to 15 on the following day. This is a different quantity from diff(). diff() reports the change that just happened, and next_change reports the change that is about to happen.
You can reach the same lead with s.shift(-1) or with s.iloc[1:] aligned to a shorter index, but shift keeps the original index. That is why it is the usual choice: every row keeps its date, and the columns stay aligned for later steps.
Leads use future information
A lead makes a row contain data from a later time. This is correct for a target or for an after-the-fact analysis. It is a bug if the lead ends up among the model's input features, because the model would see the answer during training and have nothing like it at prediction time. A later chapter covers this problem, called lookahead leakage, in detail.
Dropping the edge rows
The NaN rows at the end are rows where the target is unknown. For training, you usually drop them rather than fill them, because a made-up target teaches the model something false.
train = df.dropna(subset=["next_sales"])
print(train)The output keeps only the first three dates. Filling with fill_value is possible, but for a target column it usually hides the problem instead of fixing it.
Summary of the sign convention
| Call | Row `t` shows | `NaN` appears |
|---|---|---|
| `shift(1)` | value from `t - 1` | at the start |
| `shift(-1)` | value from `t + 1` | at the end |
| `shift(-2)` | value from `t + 2` | at the last two rows |
Exercise
A Series s has daily values on 2024-01-01 to 2024-01-05. You compute s.shift(-3). Which dates hold NaN, and which date's original value appears on 2024-01-01?
Show answer
The last three dates, 2024-01-03, 2024-01-04 and 2024-01-05, hold NaN, because they have no row three steps later. The value on 2024-01-01 is the original value from 2024-01-04, which is three rows ahead.