Pandas data engineering interview problem. Difficulty: intermediate. Pattern: Date Functions. About 18 minutes. Part of the Pro drill bank.
Roll daily revenue up to weeks that start on Monday and to calendar months. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
df has day (an ISO date string) and revenue, with some days missing. Return one DataFrame that stacks two summaries: rows with period_type = 'week': period_start is the Monday of the week and revenue is the sum over that week rows with period_type = 'month': period_start is the first day of the month and revenue is the sum over that month Write period_start as YYYY-MM-DD text. Columns: period_type, period_start, revenue. Sort by period_type, then period_start, reset the index. Assign the DataFrame to result.
Input: df day | revenue 2024-01-30 | 10 2024-01-31 | 20 2024-02-01 | 5 2024-02-04 | 15 2024-02-05 | 30 2024-02-07 | 25 2024-02-13 | 40 Output: period_type | period_start | revenue month | 2024-01-01 | 30 month | 2024-02-01 | 115 week | 2024-01-29 | 50 week | 2024-02-05 | 55 week | 2024-02-12 | 40 The week of Monday 2024-01-29 holds Jan 30, Jan 31, Feb 1 and Sunday Feb 4 (50.0) even though it crosses a month boundary; January only counts Jan 30 and 31 (30.0).
Topics: lakebench, pandas, resample, dates, weeks.
More interview problems · All interview problems · Learn data engineering
Interview-style drill: Roll daily revenue up to weeks that start on Monday and to calendar months.
`df` has `day` (an ISO date string) and `revenue`, with some days missing. Return one DataFrame that stacks two summaries: - rows with `period_type = 'week'`: `period_start` is the **Monday** of the week and `revenue` is the sum over that week - rows with `period_type = 'month'`: `period_start` is the first day of the month and `revenue` is the sum over that month Write `period_start` as `YYYY-MM-DD` text. Columns: `period_type`, `period_start`, `revenue`. Sort by `period_type`, then `period_start`, reset the index. Assign the DataFrame to `result`.