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 three days after the session.

Why this exercise

In the lab you found the bugs in clean_registers_flawed.py 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 from your pair’s data, so an answer copied from another pair, or from a chatbot, does not fit. 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.

What you need

The Session 3 lab folder s3_lab with your pair’s folder pair_NN in it, and clean_registers_flawed.py already run on your data (lab Step 8), so that pair_NN/data/processed/flawed_py/ holds its 20 output files. 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. Set PAIR in its CHANGE HERE block, and 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 differ from pair to pair and are stored as fingerprints, so the check cannot give them away.

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