Day-of-Week Seasonality Index from Daily Orders in Pandas

Quick Overview

Write a pandas function that turns daily order counts into a day-of-week seasonality profile: each weekday's mean divided by the overall mean, flagged when above average and sorted with a fixed tie-break. It tests datetime accessors, groupby aggregation and ratio-to-mean seasonality indices.

Day-of-Week Seasonality Index from Daily Orders in Pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `seasonality_index(df)` taking a DataFrame with a `date` column (`datetime64[ns]`, daily, spanning several whole weeks) and a numeric `orders` column. Return a day-of-week seasonality profile with these columns, in this order: - `day_name` (object): `'Monday'` to `'Sunday'`. - `mean_orders` (float64): the mean of `orders` on that weekday. - `index` (float64, rounded to 4dp): the weekday's mean orders divided by the overall mean of all rows. - `above_average` (bool): True where `index > 1.0`. Sort by `index` descending, with `day_name` ascending as the tie-breaker, then `reset_index(drop=True)`. For example, take 4 whole weeks starting on Monday 2024-01-01, where each weekday has the same number of orders every week: ```python dates = pd.date_range("2024-01-01", periods=28, freq="D") # Mon 1 Jan to Sun 28 Jan per_day = {"Monday": 100, "Tuesday": 90, "Wednesday": 88, "Thursday": 90, "Friday": 130, "Saturday": 120, "Sunday": 82} df = pd.DataFrame({"date": dates, "orders": [per_day[d] for d in dates.day_name()]}) ``` The overall mean is 100, and `seasonality_index(df)` returns these rows, in this order (index 0 to 6): | day_name | mean_orders | index | above_average | |---|---|---|---| | Friday | 130.0 | 1.3 | True | | Saturday | 120.0 | 1.2 | True | | Monday | 100.0 | 1.0 | False | | Thursday | 90.0 | 0.9 | False | | Tuesday | 90.0 | 0.9 | False | | Wednesday | 88.0 | 0.88 | False | | Sunday | 82.0 | 0.82 | False | ```hint Denominator first Decide where the denominator comes from before you group, and check whether `above_average` should compare the rounded or the unrounded ratio. ``` ### Constraints & Assumptions - `date` is `datetime64[ns]` with one row per day over several whole weeks, so each weekday appears the same number of times. - `orders` is numeric. - The result has exactly the four columns above, with those dtypes, in that order, and a fresh 0-based index. ### What a Strong Answer Covers - Using the overall mean of all rows as the denominator, computed once before grouping. - Weekday labels from the `date` column and a per-weekday mean of `orders`. - Rounding to 4dp and the strict `> 1.0` comparison, so a weekday sitting exactly at 1.0 is not above average. - The two-key sort (index descending, name ascending), the reset index, and the required column order and dtypes. ### Follow-up Questions - How would you extend this to month-of-year seasonality? - Why require whole weeks, and what breaks otherwise? - With only a few weeks of data, how would you tell whether a weekday's index differs from 1.0 by more than noise?

Overview: Write a pandas function that turns daily order counts into a day-of-week seasonality profile: each weekday's mean divided by the overall mean, flagged when above average and sorted with a fixed tie-break. It tests datetime accessors, groupby aggregation and ratio-to-mean seasonality indices.

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

Write seasonality_index(df) taking a DataFrame with a date column (datetime64[ns], daily, spanning several whole weeks) and a numeric orders column. Return a day-of-week seasonality profile with these columns, in this order:

  • day_name (object): 'Monday' to 'Sunday' .
  • mean_orders (float64): the mean of orders on that weekday.
  • index (float64, rounded to 4dp): the weekday's mean orders divided by the overall mean of all rows.
  • above_average (bool): True where index > 1.0 .

Sort by index descending, with day_name ascending as the tie-breaker, then reset_index(drop=True).

For example, take 4 whole weeks starting on Monday 2024-01-01, where each weekday has the same number of orders every week:

dates = pd.date_range("2024-01-01", periods=28, freq="D")  # Mon 1 Jan to Sun 28 Jan
per_day = {"Monday": 100, "Tuesday": 90, "Wednesday": 88, "Thursday": 90,
           "Friday": 130, "Saturday": 120, "Sunday": 82}
df = pd.DataFrame({"date": dates, "orders": [per_day[d] for d in dates.day_name()]})

The overall mean is 100, and seasonality_index(df) returns these rows, in this order (index 0 to 6):

day_namemean_ordersindexabove_average
Friday130.01.3True
Saturday120.01.2True
Monday100.01.0False
Thursday90.00.9False
Tuesday90.00.9False
Wednesday88.00.88False
Sunday82.00.82False

Constraints & Assumptions

  • date is datetime64[ns] with one row per day over several whole weeks, so each weekday appears the same number of times.
  • orders is numeric.
  • The result has exactly the four columns above, with those dtypes, in that order, and a fresh 0-based index.

What a Strong Answer Covers Guidance

  • Using the overall mean of all rows as the denominator, computed once before grouping.
  • Weekday labels from the date column and a per-weekday mean of orders .
  • Rounding to 4dp and the strict > 1.0 comparison, so a weekday sitting exactly at 1.0 is not above average.
  • The two-key sort (index descending, name ascending), the reset index, and the required column order and dtypes.

Follow-up Questions Guidance

  • How would you extend this to month-of-year seasonality?
  • Why require whole weeks, and what breaks otherwise?
  • With only a few weeks of data, how would you tell whether a weekday's index differs from 1.0 by more than noise?