Order Counts at the Grain Asked For
You're a data analyst at Whetcode Supply, an online shop selling desk gear. The ops lead is setting next year's rota, and she wants last year's order volume in calendar order so a quiet spring and a busy summer are visible at a glance. The trouble is that she does not always want the same size of bucket: the warehouse is staffed a month at a time, the board deck wants a single figure for the whole year, and the picking team argues about individual days. So the report takes the grain as a setting and gives the same shape of answer whichever one is asked for.
The orders table is no help as it stands. It records a full date on every order and nothing coarser — there is no month column, no year column, the dates are stored as plain text, and there is no promise the rows arrive in any particular order.
orders_df — one row per order placed last year. order_date is a plain string in YYYY-MM-DD form, not a date object.
| id | customer_id | product_id | quantity | order_date |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2023-02-01 |
| 2 | 1 | 4 | 1 | 2023-02-01 |
| 3 | 2 | 2 | 1 | 2023-02-10 |
| 4 | 3 | 3 | 3 | 2023-03-05 |
| 5 | 4 | 5 | 5 | 2023-03-11 |
| 6 | 1 | 3 | 1 | 2023-04-02 |
| 7 | 5 | 1 | 4 | 2023-05-20 |
| 8 | 2 | 5 | 2 | 2023-05-22 |
| 9 | 3 | 4 | 1 | 2023-06-01 |
| 10 | 5 | 2 | 1 | 2023-06-15 |
Input
The DataFrame above is already built for you — the order rows are not read from input. What does arrive is a single line holding one word, the grain the ops lead wants this time: year, month or day.
Task: Print a dict whose keys are the calendar buckets at the grain that arrives — written YYYY for year, YYYY-MM for month, YYYY-MM-DD for day — and whose values are how many orders were placed in each bucket, with the buckets in chronological order. A bucket in which nothing was ordered simply does not appear.
Example: if the shop had taken four orders in January 2023 and one in February and the grain were month, you'd print {'2023-01': 4, '2023-02': 1}.
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in