Sub topic 6
Shifting by Position Versus Shifting the Index with freq
This chapter assumes you know basic Python, how to build a pandas Series, and how a DatetimeIndex and pd.date_range work. It covers the two ways shift() can move data and when each one is the right choice.
Two meanings of "shift"
By default, shift(n) moves the values by n rows and leaves the index alone. The index labels stay where they are, so the values slide past them. Rows at the edge have nothing to receive, so they become NaN.
When you pass freq, shift() does the opposite. It moves the index labels by that amount of time and leaves the values unchanged. Nothing slides past anything, so no NaN values appear.
import pandas as pd
s = pd.Series(
[10, 12, 15, 11],
index=pd.date_range("2024-01-01", periods=4, freq="D"),
name="sales",
)
by_position = s.shift(1)
print(by_position)The output shows the first date as NaN. Each later date holds the previous day's value.
moved = s.shift(1, freq="D")
print(moved)Here the values are still 10, 12, 15, 11, but the index now runs from 2024-01-02 to 2024-01-05. The value that was recorded on January 1 now sits under January 2. The data is the same, and only its labels moved.
How the two results compare
| Aspect | `shift(1)` | `shift(1, freq="D")` |
|---|---|---|
| What moves | Values | Index labels |
| Index after the call | Same as the original | Shifted by one day |
| Edge `NaN` values | Yes, unless `fill_value` is set | None |
| Length of the result | Same as the original | Same as the original |
| Counts in | Rows | Calendar time |
Why the difference matters
The positional shift counts rows. If the series has one row per day, one row is one day, and the two approaches agree on which value lands where. They differ in what you get back: the positional version aligns with the original index, and the freq version does not.
That alignment is the reason to choose between them. Subtracting a positionally shifted series from the original works directly, because both share one index.
change = s - s.shift(1)
print(change)If you try the same with the freq version, pandas aligns on labels, and the result differs.
aligned = s - s.shift(1, freq="D")
print(aligned)Only January 2 to January 4 exist in both indexes. January 1 and January 5 appear in only one of them, so they become NaN. The numbers on the shared dates are the same as before, but the edge handling is different.
A practical rule:
- Use positional
shift(n)when you want to build a column next to the original, such as a lag feature. - Use
shift(n, freq=...)when you want to move a series in calendar time, for example to compare this year's data with last year's by label.
Shifting by calendar time on irregular data
The two modes diverge when the index has gaps. A positional shift of 1 means "the previous row," which might be three days earlier. A freq shift always means exactly one day.
irregular = s.drop(pd.Timestamp("2024-01-03"))
print(irregular.shift(1))
print(irregular.shift(1, freq="D"))In the first result, January 4 receives the value from January 2, because that is the previous row. In the second result, the labels move by exactly one day, so no value lands on a date that is not one day after its source.
Choosing a freq value
The freq argument accepts any pandas offset alias, such as "D", "h", "W", or "MS". You can also pass an offset object like pd.offsets.BDay() to move by business days. If the series has a freq attribute set on its index, shift(1, freq="infer") uses that frequency.
weekly = pd.Series(
[1, 2, 3],
index=pd.date_range("2024-01-07", periods=3, freq="W"),
)
print(weekly.shift(1, freq="infer"))
print(weekly.shift(2, freq="D"))The first call moves each label forward by one week, and the second moves each label forward by two days.
Exercise
A daily series has its January 3 row missing. You call s.shift(1) and s.shift(1, freq="D"). Which call can put a value from January 2 under the label January 4, and why?
Show answer
s.shift(1) can. It moves values by one row, and the row before January 4 is January 2. s.shift(1, freq="D") moves labels by exactly one calendar day, so January 2's value goes under January 3 and never under January 4.
Exercise
You want sales_lag1 as a new column in a DataFrame that shares the original index. Do you use shift(1) or shift(1, freq="D")?
Show answer
Use shift(1). It keeps the original index, so the result lines up with the other columns. The freq version changes the index, and assignment would align on labels, leaving NaN at the ends.