# 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 November 19, 2026

By Gregor Dzierzon

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](https://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:

```bash
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):

```powershell
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

On a Mac (zsh):

```bash
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 as `sales-by-store.png`. Ready to use.
- `sales.csv`: Bean Street's October, one row for each store on each day, under the header `date,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.txt` and `pytest.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:

```python
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:

1. **`load_sales(path)`** returns the CSV file at `path` as a DataFrame.
2. **`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.
3. **`best_day(df)`** returns the date, as text such as `"2026-10-03"`, whose sales across all three stores add up to the most.
4. **`save_chart(df, path)`** draws a bar chart with one bar per store, as tall as that store's total, in the order `total_by_store` gives them. Its title is `Sales by Store`, its x-axis label `Store` and its y-axis label `Sales ($)`. It saves the chart to `path` with `fig.savefig(path)`, then returns `fig, 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:

```bash
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 number `total_by_store` should 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, and `idxmax()` gives the date with the biggest total.
- **Let one function call another.** `save_chart` needs each store's total, and `total_by_store` already 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()` and `ax.bar`, then adds the title and labels. Here the words must be exactly `Sales by Store`, `Store` and `Sales ($)`.
- **Return both.** `return fig, ax` hands the two back together, the way `plt.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:

```text
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.

1. 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.
2. 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:

   ```bash
   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 main
   ```

3. Paste 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.

## Key terms

- [pandas](https://www.ask-codey.com/glossary/pandas)
- [DataFrame](https://www.ask-codey.com/glossary/dataframe)
- [groupby()](https://www.ask-codey.com/glossary/groupby-python)
- [matplotlib](https://www.ask-codey.com/glossary/matplotlib)
- [Bar Chart](https://www.ask-codey.com/glossary/bar-chart)

## Related

- [Your First pandas Table](https://www.ask-codey.com/lessons/python-taste-pandas-first-table)
- [Your First Chart](https://www.ask-codey.com/lessons/python-taste-pandas-first-chart)

Source: https://www.ask-codey.com/lessons/exercise-python-coffee-sales-at-a-glance
