Exercise: Coffee Sales at a Glance
Answer a coffee chain owner's three questions about a month of sales with pandas and matplotlib, by writing four functions that load the data, total each store, find the busiest day and save a labeled bar chart, and let pytest's tests check each one.
Apprentice · Python · Updated
Rosa, who owns Bean Street Coffee, has the whole of October in a CSV file now: 31 days of sales at her three stores. She has three questions for the month: how much did each store sell, which day was the busiest, and can she see it as a chart for the staff meeting? You'll write the four functions that answer them with pandas and matplotlib, and the tests call each one and check what it returns, the chart included.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-python-coffee-sales-at-a-glance, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:
cd ~/python-course
git clone https://github.com/ask-codey/exercise-python-coffee-sales-at-a-glance.git
Git makes a folder, exercise-python-coffee-sales-at-a-glance, with the exercise code inside. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
On a Mac (zsh):
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
This time requirements.txt lists pandas and matplotlib as well as pytest, so the install takes a minute or two: pip also fetches the packages they need, such as NumPy. The prompt starts with (.venv) once the virtual environment is on.
The folder holds:
sales.py: the four functions, for you to finish.report.py: prints October at a glance with your functions and saves the chart assales-by-store.png. Ready to use.sales.csv: Bean Street's October, one row for each store on each day, under the headerdate,store,cups,sales. The sales are in dollars.tests/test_sales.py: the tests, which call each function and check what comes back. You don't need to change it.tests/sample_sales.csv: three days of sales, small enough to add up by hand, for the tests.tests/conftest.py: tells matplotlib to draw the tests' charts in memory, so no window opens.requirements.txtandpytest.ini: the packages, at exact versions, and where pytest finds the tests and your code.
What to do
Every function in sales.py is already declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should return, with examples. df is the name the functions use for a DataFrame, as most pandas code does:
def best_day(df):
"""Return the date whose sales, all three stores added together, are the highest.
For tests/sample_sales.csv, the three days add up to 1080 (2026-10-01),
1660 (2026-10-02) and 1730 (2026-10-03), so:
best_day(df) -> "2026-10-03"
"""
raise NotImplementedError("Not built yet.")
In each function, replace the raise NotImplementedError("Not built yet.") line with your own code:
load_sales(path)returns the CSV file atpathas a DataFrame.total_by_store(df)returns each store's total sales, a Series with the stores in A-to-Z order: for the sample file, Campus 920, Downtown 1610 and Harbor 1940.best_day(df)returns the date, as text such as"2026-10-03", whose sales across all three stores add up to the most.save_chart(df, path)draws a bar chart with one bar per store, as tall as that store's total, in the ordertotal_by_storegives them. Its title isSales by Store, its x-axis labelStoreand its y-axis labelSales ($). It saves the chart topathwithfig.savefig(path), then returnsfig, ax, so the tests can read the title, the labels and the bars.
Each function returns its answer. None of them prints anything.
Check your work
Run the tests from the exercise folder, with the virtual environment on:
pytest
At first all 13 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example best_day(sample) should return '2026-10-03', but it returned 8. That 8 is a row number, not a date: it's the row with the biggest single sale, not the day with the biggest total.
The chart tests don't look at the picture. They read the Axes your function returns: its title, its two axis labels, the names under the bars and each bar's height.
To see your functions at work, run python report.py, then open sales-by-store.png in VS Code.
Hints
- Add one up by hand first. In
tests/sample_sales.csv, Harbor sold 290, 700 and 950 over the three days: 1940. That's the numbertotal_by_storeshould give for Harbor. - A total per day works like a total per store. Your First pandas Table totals the sales for each store with
groupby; group by the date instead, andidxmax()gives the date with the biggest total. - Let one function call another.
save_chartneeds each store's total, andtotal_by_storealready works them out: the totals' index is the stores and their values are the heights. - Draw on the Axes, save the Figure. Your First Chart draws a bar chart with
fig, ax = plt.subplots()andax.bar, then adds the title and labels. Here the words must be exactlySales by Store,StoreandSales ($). - Return both.
return fig, axhands the two back together, the wayplt.subplots()hands them to you.
When you're done
Every test is green: pytest's last line says 13 passed. And python report.py prints:
Bean Street Coffee: October 2026
93 rows
Sales by store:
Campus: $28,486
Downtown: $36,562
Harbor: $30,141
Best day: 2026-10-02
Saved the chart as sales-by-store.png
Open sales-by-store.png: three bars, Downtown's the tallest, with the title and both labels. Now look back at your code. If Rosa opened a fourth store next month, which of your functions would need changing? With groupby, none of them: a new store in the file is a new group, and a new bar.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-python-coffee-sales-at-a-glance), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-python-coffee-sales-at-a-glance.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-python-coffee-sales-at-a-glance, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
Four small functions answered a month of questions and drew the chart for the meeting, and tests that read the numbers and the chart's Axes prove each piece works: the start of every data analysis you'll write.