← Questions

Time-series manipulation with pandas groupby

0
AmazonCodingLast asked Aug 2025

A coding interview question asked in Amazon's technical phone screen, last asked Aug 2025. It tests pandas, time series and data manipulation.

You get a pandas DataFrame events with columns user_id, event_type, ts_utc (UTC timestamps) and revenue. Write idiomatic, vectorized pandas (no Python loops over rows) for five tasks, and explain the shape of each intermediate result.

  1. Parse ts_utc as timezone-aware UTC and convert it to America/Los_Angeles, so that daylight saving changes are handled. Derive the local calendar date.
  2. Daily active users (distinct user_id per local date) and their 7-day moving average.
  3. For every event, a 7-day rolling count of events for the same user_id and event_type (the event itself plus that pair's events in the previous 7 days).
  4. Weekly retention: weeks start on Monday in local time; for each week w, the number of users active in w, how many of them are also active in week w + 1, and the rate.
  5. Resample the daily series to a continuous calendar, filling dates with no events with 0.

Interface

Unless you agree on a different interface with the interviewer, assume one function per task, each taking the DataFrame (with the columns from step 1 added) and returning a new DataFrame:

  • to_local(events) -> DataFrame — adds ts_local and date_local.
  • daily_active(events) -> DataFrame — date_local, dau, dau_7d_avg, on a calendar with no gaps (this covers task 5).
  • rolling_counts(events) -> DataFrame — each event with rolling_7d_count.
  • weekly_retention(events) -> DataFrame — week_start, cohort_users, returning_users, retention_rate.

Example

user  event   ts_utc                 local time (America/Los_Angeles)
u1    click   2026-03-08 07:30Z  ->  2026-03-07 23:30 PST
u2    buy     2026-03-08 18:00Z  ->  2026-03-08 11:00 PDT   # DST began at 02:00 local that day
u1    click   2026-03-09 06:30Z  ->  2026-03-08 23:30 PDT
u1    click   2026-03-10 12:00Z  ->  2026-03-10 05:00 PDT

daily_active:  2026-03-07 -> 1,  2026-03-08 -> 2,  2026-03-09 -> 0 (filled),  2026-03-10 -> 1
weekly_retention:  week 2026-03-02 has u1 and u2 (cohort 2); only u1 returns in week 2026-03-09 -> rate 0.5

Grouping by the UTC date instead would put the first and third events on the wrong days.

Constraints

  • Millions of rows; the solution must stay vectorized.
  • Timestamps may arrive as strings or naive datetimes that are known to be UTC.

Clarifying questions

  • Should the 7-day moving average count days with zero users (after filling the calendar) or only days that had events?
  • Is the rolling window for task 3 seven calendar days on the timestamp ("7D") or seven local dates?
  • Is a week Monday to Sunday in local time, and which week does an event at 00:30 Monday local time belong to?
  • What does retention report for the last week, which has no following week yet?

What a strong answer covers

  • pd.to_datetime(..., utc=True) then .dt.tz_convert("America/Los_Angeles"), and .dt.normalize() or .dt.date for the local date; why tz_localize would be wrong here.
  • DAU with groupby("date_local")["user_id"].nunique(), then asfreq("D", fill_value=0) or reindex on a full date_range before a rolling(7, min_periods=1).mean().
  • Time-based rolling per group: sort by time, groupby(["user_id", "event_type"]).rolling("7D", on="ts_utc"), and how to align the result back to the original rows.
  • Retention: one row per (user, week) from dt.to_period("W-SUN") or a Monday week_start, then a merge with the same table shifted by one week, or groupby with shift.
  • What each step returns (a Series with a MultiIndex, a DataFrame, an index of dates) and how ties and duplicates are handled.

Follow-ups

  • Revenue per user per week with a running total.
  • The same five results in SQL with window functions.
  • Make it scale to data that does not fit in memory (chunks, Dask, Polars or a warehouse).

Where it's asked

  • Amazon · Technical phone screen · Last asked Aug 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

pandas · time series · data manipulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding