Month-over-month revenue growth report from daily revenue in pandas

Quick Overview

Write a pandas function that rolls daily revenue up to month-end totals, computes fractional month-over-month growth, and labels each month up, down or flat using a 1% dead band. It tests resampling, pct_change, rounding order, NaN handling for the first month and exact output dtypes.

Month-over-month revenue growth report from daily revenue in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write a function `mom_growth_report(df)` that turns daily revenue into a month-over-month growth table. The input DataFrame has a `date` column (`datetime64[ns]`, one row per day, sorted) and a numeric `revenue` column. The function must: - Aggregate to month-end totals with `resample('ME')` on a DatetimeIndex set from `date`. - Return the columns `month` (`datetime64[ns]`, the month-end date), `revenue` (`float64`), `mom_pct` (`float64`) and `direction` (object/str). - Set `mom_pct` to the fractional change from the previous month (0.2 for a 20% rise, not 20), rounded to 4 decimal places, with `NaN` for the first month. - Set `direction` to `'up'` if `mom_pct > 0.01`, `'down'` if `mom_pct < -0.01`, `'flat'` if `-0.01 <= mom_pct <= 0.01`, and `'n/a'` for the first month. - Keep months in ascending order and return a 0 to n-1 RangeIndex (`reset_index(drop=True)`). For example, if January totals 1000, February 1200 and March 1195, the result is: ```text month revenue mom_pct direction 0 2024-01-31 1000.0 NaN n/a 1 2024-02-29 1200.0 0.2000 up 2 2024-03-31 1195.0 -0.0042 flat ``` ```hint Rounding and labels Label each month from the same `mom_pct` value you return, and check what a comparison against `NaN` evaluates to before you rely on a default label. ``` ### Constraints & Assumptions - `date` is `datetime64[ns]` with one row per calendar day, sorted ascending. - `revenue` is numeric and may arrive as integers; the output column must still be `float64`. - `'ME'` is the month-end alias in pandas 2.2 and later (older versions use `'M'`). - The dead band is inclusive: a change of exactly 0.01 or -0.01 is `'flat'`. ### What a Strong Answer Covers - Monthly aggregation that yields one row per calendar month in chronological order. - A growth calculation that compares each month with its true predecessor and leaves the first month as `NaN`. - Labels that agree with the rounded `mom_pct` values, including the inclusive band edges and the first-month `'n/a'`. - Output columns, dtypes and index that match the contract exactly. - What the function returns for edge cases such as a month with zero revenue. ### Follow-up Questions - How would you add a trailing 3-month average revenue column? - What happens if the daily data has gaps, including a whole month with no rows? - The data ends partway through the current month. How should the report treat that partial month?

Overview: Write a pandas function that rolls daily revenue up to month-end totals, computes fractional month-over-month growth, and labels each month up, down or flat using a 1% dead band. It tests resampling, pct_change, rounding order, NaN handling for the first month and exact output dtypes.

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

Write a function mom_growth_report(df) that turns daily revenue into a month-over-month growth table. The input DataFrame has a date column (datetime64[ns], one row per day, sorted) and a numeric revenue column.

The function must:

  • Aggregate to month-end totals with resample('ME') on a DatetimeIndex set from date .
  • Return the columns month ( datetime64[ns] , the month-end date), revenue ( float64 ), mom_pct ( float64 ) and direction (object/str).
  • Set mom_pct to the fractional change from the previous month (0.2 for a 20% rise, not 20), rounded to 4 decimal places, with NaN for the first month.
  • Set direction to 'up' if mom_pct > 0.01 , 'down' if mom_pct < -0.01 , 'flat' if -0.01 <= mom_pct <= 0.01 , and 'n/a' for the first month.
  • Keep months in ascending order and return a 0 to n-1 RangeIndex ( reset_index(drop=True) ).

For example, if January totals 1000, February 1200 and March 1195, the result is:

       month  revenue  mom_pct direction
0 2024-01-31   1000.0      NaN       n/a
1 2024-02-29   1200.0   0.2000        up
2 2024-03-31   1195.0  -0.0042      flat

Constraints & Assumptions

  • date is datetime64[ns] with one row per calendar day, sorted ascending.
  • revenue is numeric and may arrive as integers; the output column must still be float64 .
  • 'ME' is the month-end alias in pandas 2.2 and later (older versions use 'M' ).
  • The dead band is inclusive: a change of exactly 0.01 or -0.01 is 'flat' .

What a Strong Answer Covers Guidance

  • Monthly aggregation that yields one row per calendar month in chronological order.
  • A growth calculation that compares each month with its true predecessor and leaves the first month as NaN .
  • Labels that agree with the rounded mom_pct values, including the inclusive band edges and the first-month 'n/a' .
  • Output columns, dtypes and index that match the contract exactly.
  • What the function returns for edge cases such as a month with zero revenue.

Follow-up Questions Guidance

  • How would you add a trailing 3-month average revenue column?
  • What happens if the daily data has gaps, including a whole month with no rows?
  • The data ends partway through the current month. How should the report treat that partial month?