Build a multiplicative seasonal index and deseasonalize monthly sales in pandas

Quick Overview

Write a pandas function that builds a multiplicative seasonal index for each calendar month and divides monthly sales by it to produce a seasonally adjusted series with a verdict per row. It tests groupby transforms, careful rounding order, and reasoning about when seasonal indices need renormalizing.

Build a multiplicative seasonal index and deseasonalize monthly sales in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `deseasonalized_trend_report(df)` in pandas. The input has a `month` column (`datetime64[ns]`, month-end dates, consecutive, covering whole years) and a numeric `sales` column. Build a multiplicative seasonal index for each calendar month (1-12), then divide each month's sales by its index to get the seasonally adjusted level. The point of the report is to separate a real move in sales from a calendar effect. Return a DataFrame with these columns, in this order: | Column | dtype | Meaning | |---|---|---| | `month` | `datetime64[ns]` | Same as the input | | `sales` | `float64` | Same as the input | | `seasonal_index` | `float64` | Mean of `sales` for that calendar month across all years, divided by the mean of `sales` over all rows, rounded to 4 decimals | | `adjusted` | `float64` | `sales` divided by the full-precision seasonal index, rounded to 4 decimals | | `verdict` | `object` | `'better_than_seasonal'` if `adjusted > sales`, `'worse_than_seasonal'` if `adjusted < sales`, else `'as_expected'` | Compare `adjusted` and `sales` after both are rounded to 4 decimals. Return the rows in ascending `month` order with `reset_index(drop=True)`. Example for December: ```python # dec_index = mean(sales in December) / mean(all sales) # If dec_index = 1.35 and one December's sales = 540: # adjusted = 540 / 1.35 = 400.0 # adjusted (400) < sales (540), so verdict = 'worse_than_seasonal': # the December spike comes from the calendar. ``` ```hint Check the normalization Before you add a step that rescales the twelve indices to average 1.0, work out what the whole-year guarantee already gives you. ``` ```hint Keep one row per input row Compute the calendar-month factor so it lines up with every original row (a groupby transform or a map from month number to index), with no merge that could reorder or duplicate rows. ``` ### Constraints & Assumptions - `month` holds consecutive month-end timestamps with no gaps, and the data covers whole calendar years, so each calendar month appears the same number of times. - `sales` is numeric and is returned as `float64`. - Nothing is imputed, clipped or dropped: the output has exactly one row per input row, and the original `sales` stays in the report so the correction is visible. ### What a Strong Answer Covers - Grouping by calendar month (not by year-month), computing the overall mean, and attaching each row's index without changing row count - Why whole-year input lets the indices average to 1.0 without a renormalization step - Applying the rounding in the specified order and comparing rounded values for the verdict - What the verdict actually depends on, and edge cases such as a zero overall mean, a calendar month whose mean is zero, or negative sales ### Follow-up Questions - How would you handle partial years, where some calendar months appear more often than others? - When would an additive seasonal model fit better than a multiplicative one? - If the series has a strong upward trend, how does that bias a simple average-by-month index, and how would you correct it?

Overview: Write a pandas function that builds a multiplicative seasonal index for each calendar month and divides monthly sales by it to produce a seasonally adjusted series with a verdict per row. It tests groupby transforms, careful rounding order, and reasoning about when seasonal indices need renormalizing.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement deseasonalized_trend_report(df) in pandas. The input has a month column (datetime64[ns], month-end dates, consecutive, covering whole years) and a numeric sales column. Build a multiplicative seasonal index for each calendar month (1-12), then divide each month's sales by its index to get the seasonally adjusted level. The point of the report is to separate a real move in sales from a calendar effect.

Return a DataFrame with these columns, in this order:

ColumndtypeMeaning
monthdatetime64[ns]Same as the input
salesfloat64Same as the input
seasonal_indexfloat64Mean of sales for that calendar month across all years, divided by the mean of sales over all rows, rounded to 4 decimals
adjustedfloat64sales divided by the full-precision seasonal index, rounded to 4 decimals
verdictobject'better_than_seasonal' if adjusted > sales, 'worse_than_seasonal' if adjusted < sales, else 'as_expected'

Compare adjusted and sales after both are rounded to 4 decimals. Return the rows in ascending month order with reset_index(drop=True).

Example for December:

# dec_index = mean(sales in December) / mean(all sales)
# If dec_index = 1.35 and one December's sales = 540:
#   adjusted = 540 / 1.35 = 400.0
# adjusted (400) < sales (540), so verdict = 'worse_than_seasonal':
# the December spike comes from the calendar.

Constraints & Assumptions

  • month holds consecutive month-end timestamps with no gaps, and the data covers whole calendar years, so each calendar month appears the same number of times.
  • sales is numeric and is returned as float64 .
  • Nothing is imputed, clipped or dropped: the output has exactly one row per input row, and the original sales stays in the report so the correction is visible.

What a Strong Answer Covers Guidance

  • Grouping by calendar month (not by year-month), computing the overall mean, and attaching each row's index without changing row count
  • Why whole-year input lets the indices average to 1.0 without a renormalization step
  • Applying the rounding in the specified order and comparing rounded values for the verdict
  • What the verdict actually depends on, and edge cases such as a zero overall mean, a calendar month whose mean is zero, or negative sales

Follow-up Questions Guidance

  • How would you handle partial years, where some calendar months appear more often than others?
  • When would an additive seasonal model fit better than a multiplicative one?
  • If the series has a strong upward trend, how does that bias a simple average-by-month index, and how would you correct it?