Time Series Ops: shift, diff & pct_change

Time-series operations compare each row to an earlier or later one — lagging values with shift, measuring the absolute step with diff, and the relative step with pct_change — to reveal trends, returns, and momentum in ordered data.

Learn Time Series Ops: shift, diff & pct_change in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise…

Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You will lag and lead with shift() , compute period-over-period change with diff() , find returns with pct_change() , and accumulate with cumsum() and cummax() over a DatetimeIndex .

shift(1) pushes every value down one row, so each row now carries the previous period's value — a lag . shift(-1) pulls values up , giving each row the next period's value — a lead . The row left empty at the top or bottom becomes NaN .

diff() subtracts each value from the one before it, giving the absolute change in units. pct_change() divides that change by the previous value, giving the relative change — the return or growth rate. Both leave the first row as NaN because there is nothing earlier to compare with.

Cumulative methods carry a result forward down the column. cumsum() gives a running total — perfect for cumulative revenue. cummax() tracks the highest value seen so far, the basis for "record high to date" and drawdown analysis. There is also cummin() and cumprod() .

Turn a daily price column into a small analytics table.

Lesson complete — your data can compare across time!

You can lag and lead with shift , measure absolute change with diff , relative change with pct_change , and accumulate with cumsum and cummax on a sorted DatetimeIndex .

🚀 Up next: Plotting with .plot() — turn these series straight into charts.

Practice quiz

What does s.shift(1) do?

  • Deletes the first row
  • Sorts the Series
  • Doubles every value
  • Moves each value down one row (a lag)

Answer: Moves each value down one row (a lag). shift(1) pushes values down, so each row shows the previous one.

What does s.shift(-1) give you?

  • The next period's value (a lead)
  • The previous period's value
  • The cumulative sum
  • An error

Answer: The next period's value (a lead). A negative shift pulls values up, giving the next row's value.

What appears in the rows left empty by a shift?

  • Zero
  • NaN
  • The mean
  • The last value repeated

Answer: NaN. Shifted-out edge rows are filled with NaN.

What does diff() compute?

  • The relative percentage change
  • The cumulative total
  • The absolute change from the previous value
  • The rolling mean

Answer: The absolute change from the previous value. diff() subtracts each value from the one before it (absolute step).

Which method gives the relative change as a fraction?

  • diff()
  • delta()
  • ratio()
  • pct_change()

Answer: pct_change(). pct_change() returns the relative change, e.g. 0.20 for +20%.

Why is the first row of diff() NaN?

  • There is no prior value to subtract from
  • diff drops the first row
  • The data was unsorted
  • diff needs three rows

Answer: There is no prior value to subtract from. With nothing earlier to compare, the first diff is NaN.

What does cumsum() produce?

  • A running total down the column
  • The maximum value
  • The mean of each row
  • The percentage change

Answer: The maximum value. cumsum() carries a running sum forward through the Series.

What does cummax() track?

  • The smallest value seen so far
  • The average so far
  • The highest value seen so far
  • The last value

Answer: The highest value seen so far. cummax() records the running maximum up to each row.

Why sort by date before diff() or pct_change()?

  • These methods work by row position, so order must be correct
  • Sorting removes NaN
  • It speeds up the computation
  • It converts dtypes

Answer: These methods work by row position, so order must be correct. They compare neighbouring rows, so unsorted data gives nonsense.

Which best describes the difference between diff and pct_change?

  • They are identical
  • diff is absolute units, pct_change is a relative fraction
  • diff is a fraction, pct_change is units
  • Both return cumulative totals

Answer: diff is absolute units, pct_change is a relative fraction. diff is the change in units; pct_change is that change relative to the prior value.