Every exercise in 21_debugging_drill.py, with the solution and why it works. Each answer stays closed until you swear you tried it first. Opening one changes nothing in your notebook; your check still has to go green from your own code. Read the whole notebook, with its lessons.
Bug 1. A filter in SQL
The task
adults_in_region should count the people aged 15 or more in one region of the table people. It stops with an error.
If the check is red
Read the error: what does DuckDB think "01" is?
The solution, and why it works
Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
What was wrong: in SQL, double quotes name a column and single quotes make a text. region = "01" asks DuckDB for a column called 01, which does not exist, so the query stops with an error that names it.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def adults_in_region(con: duckdb.DuckDBPyConnection, region: str) -> int: """How many people aged 15 or more live in `region`.""" return con.sql(f"SELECT count(*) FROM people WHERE region = '{region}' AND age >= 15").fetchall()[0][0]Why each part is there
- The fix swaps the quotes: the Python string is now in double quotes, so the SQL inside can use single quotes,
region = '01', which compares the column with the text'01'. - The error said so, if read from its last line: DuckDB could not find a column
01. Reading what the database thinks a name is, is the drill's lesson. - Putting a value into SQL text is fine here, because the region comes from your own code. With text that comes from outside (a form, a file), pass it as a parameter instead,
con.execute("... WHERE region = ?", [region]), so it can never change the query.
Bug 2. A label
The task
describe_area("0100105000", "City of San Isidro") should give "0100105000: City of San Isidro". It gives something else, without any error.
If the check is red
Look at exactly what comes back: are the braces still there?
The solution, and why it works
Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
What was wrong: the text was missing its f. Without it, "{code}: {name}" is just those characters, braces included, and Python fills nothing in. No error, only a wrong answer.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def describe_area(code: str, name: str) -> str: """A code and its name, as one text.""" return f"{code}: {name}"Why each part is there
- The fix is one letter:
f"{code}: {name}"makes an f-string, which replaces each{...}with the value of what is inside. - The check showed it by naming what came back,
'{code}: {name}', the braces still there. Silent bugs like this one are why every function has a check: the code runs, so only comparing the answer catches it.
Bug 3. Counting valid codes
The task
count_valid_sex should count the values that are a valid sex code. Round 2's sex column holds "1", "2", and a few others. The function says there are none, without any error.
If the check is red
What type are the values in the column, and what type are the values in the list?
The solution, and why it works
Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
What was wrong: the column holds the codes as text ("1", "2"), the list held numbers (1, 2). "1" is never equal to 1, so nothing matched, and the count was 0 without any error.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def count_valid_sex(sex: pd.Series) -> int: """How many values are a valid sex code, 1 or 2.""" return int(sex.isin(["1", "2"]).sum())Why each part is there
- The fix writes the list in the column's own type:
sex.isin(["1", "2"]). - The check showed it as a count of 0 where some were expected: a sign that the comparison never matched, not that the data has no valid codes. It is the same mismatch as Exercise 5.1's lookup, and the Get module's codes that are text.
Bug 4. Counting failures
The task
count_failures should say how many failures a schema finds in a table. On a table with three problems it says 1.
If the check is red
Without one argument, pandera stops at the first failure. Which argument asks for all of them (20_check_round.py, section 2)?
The solution, and why it works
Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
What was wrong: without lazy=True, pandera stops at the first failure it finds, so the function could only ever answer 0 or 1.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def count_failures(schema: pa.DataFrameSchema, table: pd.DataFrame) -> int: """How many failures the schema finds in the table.""" try: schema.validate(table, lazy=True) return 0 except pa.errors.SchemaErrors as errors: return len(errors.failure_cases)Why each part is there
schema.validate(table, lazy=True)checks every rule on every row first, and only then raises, with all the failures together.- A lazy validation raises
SchemaErrors(plural), notSchemaError, so theexceptnames it; catching the singular would let the error through and stop the function. len(errors.failure_cases)counts the failures: one row each in the report you used for Exercise 2.2. On the check's table with three problems it now says 3.
Bug 5. Persons per region name
The task
persons_per_region should count the people of a round in each region, with the region's name from the code list. The totals come out far too big, without any error.
If the check is red
Compare the number of rows before and after the join. How many rows of the code list share the region '01'?
The solution, and why it works
Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.
The idea
What was wrong: the code list has a row for every area in a region (the region, its provinces, its towns), and all of them start with the same two digits. Joining on those two digits matched each person to every one of those rows, so each person was counted several times.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def persons_per_region(round_df: pd.DataFrame, code_list: pd.DataFrame) -> pd.DataFrame: """One row per region: its name and how many people the round has there.""" names = code_list[code_list["level"] == "Region"].assign(region=lambda t: t["code"].str[:2])[["region", "name"]] joined = round_df.merge(names, on="region", how="left", validate="many_to_one") return joined.groupby(["region", "name"]).size().rename("persons").reset_index()Why each part is there
- The fix keeps only the code list's region rows (
level == "Region") before making the two-digitregion, so each region appears once on that side. validate="many_to_one"on the join now proves it: if a region were still listed twice, the join would stop with an error instead of quietly multiplying rows.- The check showed it as totals far too big with no error, which is the signature of a join that copies rows. Comparing the number of rows before and after a join, as the hint says, finds it every time.