Time-series manipulation with pandas groupby
I was asked this
No reports yet. Sign in to say where you were asked it.
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.
- Parse
ts_utcas timezone-aware UTC and convert it toAmerica/Los_Angeles, so that daylight saving changes are handled. Derive the local calendar date. - Daily active users (distinct
user_idper local date) and their 7-day moving average. - For every event, a 7-day rolling count of events for the same
user_idandevent_type(the event itself plus that pair's events in the previous 7 days). - Weekly retention: weeks start on Monday in local time; for each week
w, the number of users active inw, how many of them are also active in weekw + 1, and the rate. - 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— addsts_localanddate_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 withrolling_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.datefor the local date; whytz_localizewould be wrong here. - DAU with
groupby("date_local")["user_id"].nunique(), thenasfreq("D", fill_value=0)orreindexon a fulldate_rangebefore arolling(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 Mondayweek_start, then a merge with the same table shifted by one week, orgroupbywithshift. - 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
