What do pandas shift() and diff() do in time-series analysis?

Quick Overview

Asks what the pandas shift() and diff() methods return and how they are used on time-series data. Tests understanding of lags and leads, period-over-period change, and the row order, grouping and missing-date pitfalls that come with them.

What do pandas shift() and diff() do in time-series analysis?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What do `shift()` and `diff()` do in time-series analysis? Answer for pandas Series and DataFrames. ```hint Work a tiny case Write out what each method returns for the series 100, 105, 103, 110, including the first and last rows. ``` ### What a Strong Answer Covers - What each method returns, including what appears at the edges of the series - How the two methods relate to each other - Typical uses in feature building and in time-series modeling - Pitfalls with row order, groups and missing dates ### Follow-up Questions - How would you compute each customer's change in spend from their previous order in one long table of orders for many customers? - Your daily data has missing dates. What does `shift(1)` mean then, and how would you fix it? - How would you build a 7-day rolling mean feature for a forecasting model without leaking the current day's value? - When would you use `pct_change()` or a log difference instead of `diff()`?

Overview: Asks what the pandas shift() and diff() methods return and how they are used on time-series data. Tests understanding of lags and leads, period-over-period change, and the row order, grouping and missing-date pitfalls that come with them.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What do shift() and diff() do in time-series analysis? Answer for pandas Series and DataFrames.

What a Strong Answer Covers Guidance

  • What each method returns, including what appears at the edges of the series
  • How the two methods relate to each other
  • Typical uses in feature building and in time-series modeling
  • Pitfalls with row order, groups and missing dates

Follow-up Questions Guidance

  • How would you compute each customer's change in spend from their previous order in one long table of orders for many customers?
  • Your daily data has missing dates. What does shift(1) mean then, and how would you fix it?
  • How would you build a 7-day rolling mean feature for a forecasting model without leaking the current day's value?
  • When would you use pct_change() or a log difference instead of diff() ?