NA446A Python for data you can trust

Exercise after Session 3: the full bug hunt

Formative, no hand-in. About 2 hours. Solution on the site Fri 30 Oct at 08:00.

Why this exercise

In lab Step 8 you ran clean_registers_flawed.py and found the bugs that show without reading its logic. Here you hunt the rest: where the documentation is wrong, where the logic is wrong, and where the rules of the data or the language make a line do something its author did not mean. Every answer is a number or a name you find in the data, so a chatbot that has not seen them cannot give it to you. The exam asks you to read code like this and say what it does, without AI. Try each task yourself first; use the tutor prompt if you are stuck.

Do this first: run the flawed script on your data (3 minutes)

The bug hunt reads the files that clean_registers_flawed.py writes. Until it has run on your data, pair_00/data/processed/flawed_py/ is empty and every task fails. This is lab Step 8:

  1. In VS Code’s Explorer, right-click your pair folder (pair_00) and choose Copy Path.
  2. Open s3_lab/clean_registers_flawed.py, press Ctrl+H (Mac: Cmd+Alt+F), and replace C:/Users/teacher/project/ with the path you copied. Use forward slashes (Windows copies \; change each to /) and end with a slash, e.g. C:/Users/anna/na446a-starter/s3_lab/pair_00/. Replace All, then save (Ctrl+S).
  3. Run the whole file with ▷ (Run Python File). It prints nothing but writes 20 files into pair_00/data/processed/flawed_py/. Check in the Explorer that they are there.

What you need

The Session 3 lab folder s3_lab with a pair folder in it, and the 20 files from the step above in pair_NN/data/processed/flawed_py/. Open na446a-starter in VS Code (the course folder, not s3_lab on its own) and work in s3_lab/bug_hunt_s3.py, one cell at a time. Which pair? Pair 00 is fine: it is already in s3_lab, PAIR = "00" is already set, and the self-check knows its answers. Nobody hands out numbers. If you want your own data, pick any number 01–30 yourself, download that pair_NN.zip from the Data page, unzip it into s3_lab and set PAIR in the CHANGE HERE block to match. Use the names given in each TODO: the self-check looks for them.

Open check_exercise_s3.py and press ▷ (Run Python File), or type in the terminal:

cd s3_lab
uv run check_exercise_s3.py

(cd s3_lab once per terminal: the terminal starts in na446a-starter.) Save your script first: the self-check reads the saved file.

It says which tasks are right and gives a hint for the others. When all is right, the last line reads 6 of 6 tasks right. The right answers are stored as fingerprints, so the check cannot give them away. Everyone on pair 00 has the same answers; another pair’s data give different ones.

Tasks, from easy to hard

Task What to do Time
1. It runs (easy) Count the output files the flawed script wrote. 5 min
2. Level 1 in numbers (easy) Rows per person in the grade-9 file; the year labels in the appended person panel; the years in the appended firm panel; persons with a missing value in the migration file. For each, say which line causes it. 20 min
3. Trust but verify (medium) The codebook disagrees with the data in three places. Run checklist items 1 (names), 6 (categories) and 8 (IDs and keys) on every raw file, not only the ones you need, and write each disagreement as an erratum: the codebook says, the data say, how you found it. 25 min
4. Follow the logic (medium) The youth flag in the municipal register: how many persons does the script flag, and how many should it? Which line is wrong, and why? 20 min
5. Know the data (hard) Education in 2021: the share the script leaves missing, and the share that is really unknown. What changed in 2021, and how would you classify so that the next code vintage cannot break it? 25 min
6. Know the language (hard) The firm block “added in April”: how many rows its key-based drop_duplicates removes, how many of those are true copies, and the right share of person-months on permanent contracts. Which rows should it have kept, and what does fillna(0) do to a coded variable? 25 min

Questions to answer in your script (as comments)

  1. Task 2: which of the four problems would a single assert after each step have stopped? Write the assert.
  2. Task 3: for each erratum, three sources can disagree: the codebook, the data and the comments in the flawed script. Which do you believe, and what would you need to settle it?
  3. Task 4: the same condition appears in two registers. Does it go wrong in the same way in both?
  4. Task 6: the rows without a person id are real pay from the firm. Where do they belong in a person-level file, and where in a firm-level file?

If you finish early (optional, 20 min)

Write the smallest set of assert statements that would have stopped the flawed script at its first wrong number in each register, and say where each one goes. Two of them should catch bugs that never raise an error in pandas.

Two rules for this exercise