Sessions
Slides, scripts and exercises, posted before each session.
Every lab, every session: unzip the lab folder
inside your course folder na446a-starter, and open
na446a-starter in VS Code (File → Open Folder…),
never the lab folder on its own. Otherwise VS Code cannot find the
course Python and you get “No module named ‘pandas’”. When VS Code asks
which Python (kernel) to use, choose na446a … .venv
(click ↻ if it is missing). The terminal then starts in
na446a-starter: for commands such as
uv run check_s3.py, type cd s3_lab (the lab’s
folder name) once first, or open the file and press ▷. More in Common problems in
class.
Session 1. Projects and first steps (Mon 19 Oct)
Goals. After this session you can:
- open a Python project in VS Code that has its own environment, and run a script;
- organise a project so that someone else can rerun it;
- load a dataset in pandas and inspect it: size, first rows, summary statistics.
Before the session: complete the Setup, read the Bryan memo (see Readings) and the AI rules.
Video: 1. From Stata to pandas (8 min), with questions to test yourself.
Slides: handout, questions without answers (PDF). Full slides, with answers (PDF).
Lab: s1-lab.zip. Unzip it
inside your course folder; it contains its own data. Open
s1_lab.py and click Run Cell above each step.
Steps 1–3 from the slides as one script (under a minute; the last line
prints Lab done), then fix an AI-written script:
lab01_fix_the_bug.py runs without an error but gives a
wrong answer, and you find the bug by testing a case whose answer you
know.
Session 2. Working with data (Tue 20 Oct)
Goals. After this session you can:
- apply the intake checklist to a variable: type, missing values, range;
- fix types and missing values, including sentinel codes such as 999;
- create, subset, sort and aggregate variables, and summarise data in a table and a figure.
Before the session: read the Turrell chapter (see Readings), about 20 minutes. Session 2 is the day after session 1, so plan it into the weekend before.
Video: 2. The intake checklist, worked on one variable (6 min), with questions to test yourself.
Slides: handout, questions without answers (PDF). Full slides, with answers (PDF).
Lab: s2-lab.zip. Unzip it
inside your course folder; it contains its own data
(auto_messy.csv, titanic.csv). Open
s2_lab.py and click Run Cell above each step,
filling in the TODOs (about 40 minutes; the last line prints
Lab done). Check your work at any time with
uv run check_s2.py: all right is
9 of 9 steps right. The AI task: paste your intake
output (not the data) into a chatbot, ask what problems it shows,
and test two of its claims with one line of code each.
Exercise after the session: a Titanic intake, about 90 minutes, in the
same lab folder (titanic_intake.py, self-check
uv run check_exercise_s2.py, all right is
8 of 8 tasks right.). Formative, no hand-in.
Solutions to the lab and the exercise: s2-exercise-worked.zip. Unzip it
inside your s2-lab folder. It holds
worked/s2_lab_worked.py (the lab) and
worked/titanic_intake_worked.py (the exercise). Run each
and compare it with your own version, step by step.
Session 3. Combining data (Tue 27 Oct)
Goals. After this session you can:
- merge two tables, as with Stata’s
merge, and check that no row vanished or multiplied (validate=,indicator=,assert); - append yearly files, as with Stata’s
append; - make a lag, as with Stata’s
L..
Every code slide shows the Stata you know first. Reshape (wide and long) and the lag across a missing year are self-study slides in the appendix.
Before the session: read Gentzkow and Shapiro, chapter 5, Keys (see Readings).
Videos: 3. Anatomy of a merge audit (8 min) and 4. Wide and long (7 min), with questions to test yourself.
Slides: handout, questions without answers (PDF). Full slides, with answers (PDF).
Lab: s3_lab.zip. Unzip it
and put the folder s3_lab inside your course folder. In
class everyone uses pair 00, which is already in the folder: open
s3_lab.py and run the cells one by one. Each step shows the
Stata line as a comment; you write the pandas. In class: Steps 1–4. At
home: Steps 5–7, then Step 8, which runs a cleaning script written the
way real register code often is (the exercise needs it). Self-check:
uv run check_s3.py (all right is
8 of 8 steps right.). At home you may use your own data
instead: any pair_NN.zip from the Data
page, unzipped into s3_lab, with
PAIR set to match. All data are synthetic; no real person
or firm is in them.
Exercise after the session: the full bug hunt, about 2 hours, in the
same lab folder (bug_hunt_s3.py, self-check
uv run check_exercise_s3.py, all right is
6 of 6 tasks right.). The answers differ from pair to pair.
Formative, no hand-in.
Solutions to the lab and the exercise: s3-exercise-worked.zip. Unzip it
inside your s3_lab folder. It holds
worked/s3_lab_worked.py (the lab) and
worked/bug_hunt_s3_worked.py (the exercise). Set
PAIR in each to the pair you used ("00" unless
you chose another) and run it: it computes that pair’s answers, so
compare it with your own version, step by step.
Session 4. Getting data and scaling up (Thu 29 Oct)
Goals. After this session you can:
- fetch data from an open API, save the raw answer, and check that
every row arrived, as with Stata’s
assert _N == ...; - handle dates, as with Stata’s
monthly(),collapseandL4., and write a repeated step once, as with Stata’sprogram; - rebuild everything with one script, as with a master do-file, and
end with one regression table, as with
regressandesttab.
Every code slide shows the Stata you know first, a short Stata
comment on each Python line, and a small table of the new words: what
each name is, and which names we chose ourselves. To look up any Python
name, as with Stata’s help: type name? in the
Interactive window, or hover over it in VS Code. DuckDB (querying a file
too large to load) is a self-study slide in the appendix.
Before the session: read Gentzkow and Shapiro, chapter 2, Automation (see Readings).
Videos: 5. An API call, start to finish (9 min) and 6. run_all and git (7 min), with questions to test yourself.
Slides: handout, questions without answers (PDF). Full slides, with answers (PDF).
Lab: s4-lab.zip. Unzip it
inside your course folder. A mini-pipeline from Statistics Sweden’s open
API to one regression table. Each TODO shows the Stata line as a
comment; you write the Python. In class: TODO 1 in
scripts/01_get_data.py, then TODOs 2a–2d in
scripts/02_clean.py. At home: TODOs 3–5
(scripts/03_table.py, run_all.py), then
uv run run_all.py. Run each script whole with ▷ (Run Python
File). Self-check: uv run check_s4.py (all right is
7 of 7 steps right.); it also does the delete-the-outputs
test. No network? Set USE_SAVED = True in
scripts/01_get_data.py: the zip holds a saved copy of SCB’s
answers. The AI task: review an AI’s version of the cleaning step
(ai_02_clean.py) with asserts.
Exercise after the session: a World Bank mini-pipeline, about 90
minutes, in the same lab folder (wb_exercise.py, self-check
uv run check_exercise_s4.py, all right is
6 of 6 tasks right.). Formative, no hand-in.
Going further: the same way of working on a whole project, from the raw file to the report’s tables, in Stata and in Python side by side: a whole project, start to finish.
Solutions to the lab and the exercise: s4-exercise-worked.zip. Unzip it
inside your s4-lab folder. The exercise:
wb_exercise_worked.py, beside wb_exercise.py;
run it and compare it with your own version. The lab: the folder
lab_worked/, with the same file names as yours
(run_all.py, scripts/,
ai_02_clean_reviewed.py). Read each beside your own; to run
one, copy it to the same place in s4-lab (keep your own
version under another name first).
Recap (Thu 5 Nov)
Exam prep: a 20-minute live session reviewing the four sessions and the exam format.
Slides: recap with answers (PDF). Covers what the exam asks, how to prepare, three practice questions (predict on paper, then we trace them on the board), and your questions. Revisit the Readings too: one short text per session, and the exam has one or two questions on them.