Year-over-Year Growth Report
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `yoy_growth_report(df)` in pandas. The input has a `month` column (`datetime64[ns]`, month-end, one row per month, at least 24 consecutive months, sorted) and a numeric `revenue` column. Compare each month against the row 12 earlier (lag 12) and return the year-over-year table.
The output must follow these rules:
- Columns, in this order: `month` (`datetime64[ns]`), `revenue` (`float64`), `revenue_prior_year` (`float64`), `yoy_pct` (`float64`), `direction` (`object`).
- `revenue_prior_year` is the revenue 12 rows earlier, and `NaN` for the first 12 months.
- `yoy_pct = (revenue - revenue_prior_year) / revenue_prior_year` as a fraction, rounded to 4 decimal places, and `NaN` for the first 12 months.
- `direction` is `'flat'` when `abs(yoy_pct) <= 0.02`, `'up'` above +2%, `'down'` below -2%, and `'n/a'` where `yoy_pct` is `NaN`.
- Rows stay in ascending month order with a 0..n-1 `RangeIndex`. Do not drop the first 12 rows.
- Do not mutate the input. Return a new DataFrame.
For example, if revenue was 200 in February 2023 and 250 in February 2024, the February 2024 row has `revenue_prior_year = 200.0`, `yoy_pct = 0.25` and `direction = 'up'`. A month that goes from 100 to 102 has `yoy_pct = 0.02` and is `'flat'`, because the 2% boundary belongs to the flat band.
```hint Boundary months
Check whether `direction` should be decided from the raw ratio or from the rounded `yoy_pct`, using a month that grows by exactly 2%.
```
### Constraints & Assumptions
- At least 24 rows, one per month, consecutive and sorted ascending, so the row 12 positions earlier is the same calendar month one year before.
- `revenue` is numeric and may be stored as integers; the output column is `float64`.
### What a Strong Answer Covers
- How the prior-year value is aligned and what the first 12 rows contain
- The growth formula, the rounding, and which value the direction bands are applied to
- Behavior exactly at plus or minus 2% and for `NaN` growth
- Column order, dtypes and index, with the input left unchanged
- What the positional lag assumes about the data and when that assumption breaks
### Follow-up Questions
- How would you handle gaps or missing months in the input?
- Why is a plus or minus 2% dead band more useful than a strict sign test for this report?
- What should `yoy_pct` and `direction` be when the prior-year revenue is 0?
Overview: Build a pandas year-over-year revenue report that pairs each month with the same month a year earlier, computes rounded fractional growth, and labels each month up, down, flat or n/a. It tests lag alignment, warm-up rows, boundary handling at the 2% band and output dtypes.