Skip to content
Learn Python with BNHR

Debugging drill

On this page
  1. Overview
  2. D1.1 · A filter in SQL
  3. D1.2 · A label
  4. D1.3 · Counting valid codes
  5. D1.4 · Counting failures
  6. D1.5 · Persons per region name

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.

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.

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.

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.

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.

Something new

Identify results
LayerExplore a debugging drill
Debugging drill D2Debugging drill: the report that goes wrong
AboutThe second debugging drill of Arrays & DataFrames, after module 5: five mistakes that put wrong numbers in a survey report, to predict, trace, fix and explain.
CourseArrays & DataFrames, free