Add lag and delta columns to a pandas time series DataFrame

Quick Overview

Add a one-row lag column and a row-over-row change column to a pandas DataFrame with a DatetimeIndex, without mutating the input. Tests shift and diff, NaN at the boundary, resulting dtypes, and how positional lags behave across groups and gaps in time.

Add lag and delta columns to a pandas time series DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `lag_and_diff(df: pd.DataFrame) -> pd.DataFrame`. The input has a `DatetimeIndex` and a numeric `'value'` column. Return a new DataFrame that keeps `'value'` and the original index and adds two derived columns: - `'prev'` holds the previous row's value, with `NaN` in the first row. - `'delta'` holds the current value minus the previous row's value, with `NaN` in the first row. Do not mutate the input. Operate on a copy (`df.copy()`) and return it. ```text df: value 2024-01-01 10 2024-01-02 13 2024-01-03 9 lag_and_diff(df): value prev delta 2024-01-01 10 NaN NaN 2024-01-02 13 10.0 3.0 2024-01-03 9 13.0 -4.0 ``` ```hint Built-in helpers pandas Series have vectorized methods that move values by a number of rows and that subtract an earlier row. Check what each one puts in the first position. ``` ### Constraints & Assumptions - Rows are already in chronological order, so no sorting is required. - `'value'` is numeric. ### What a Strong Answer Covers - Working on a copy so the caller's frame is unchanged, with `'value'` and the `DatetimeIndex` kept. - The vectorized pandas methods for a one-row lag and a one-row difference, and why both leave `NaN` in the first row. - Why the new columns come out as floats when `'value'` holds integers. - That these methods work on row position, and what that means for unsorted rows or gaps in time. ### Follow-up Questions - How would you compute `prev` and `delta` per group, for example one series per ticker? - What if the index is unsorted or has gaps in time? - How would you turn `delta` into a percent change, and what breaks when the previous value is 0?

Overview: Add a one-row lag column and a row-over-row change column to a pandas DataFrame with a DatetimeIndex, without mutating the input. Tests shift and diff, NaN at the boundary, resulting dtypes, and how positional lags behave across groups and gaps in time.

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

Implement lag_and_diff(df: pd.DataFrame) -> pd.DataFrame. The input has a DatetimeIndex and a numeric 'value' column. Return a new DataFrame that keeps 'value' and the original index and adds two derived columns:

  • 'prev' holds the previous row's value, with NaN in the first row.
  • 'delta' holds the current value minus the previous row's value, with NaN in the first row.

Do not mutate the input. Operate on a copy (df.copy()) and return it.

df:
            value
2024-01-01     10
2024-01-02     13
2024-01-03      9

lag_and_diff(df):
            value  prev  delta
2024-01-01     10   NaN    NaN
2024-01-02     13  10.0    3.0
2024-01-03      9  13.0   -4.0

Constraints & Assumptions

  • Rows are already in chronological order, so no sorting is required.
  • 'value' is numeric.

What a Strong Answer Covers Guidance

  • Working on a copy so the caller's frame is unchanged, with 'value' and the DatetimeIndex kept.
  • The vectorized pandas methods for a one-row lag and a one-row difference, and why both leave NaN in the first row.
  • Why the new columns come out as floats when 'value' holds integers.
  • That these methods work on row position, and what that means for unsorted rows or gaps in time.

Follow-up Questions Guidance

  • How would you compute prev and delta per group, for example one series per ticker?
  • What if the index is unsorted or has gaps in time?
  • How would you turn delta into a percent change, and what breaks when the previous value is 0?