import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Check%3A%20Round%202%20arrives%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20(and%20run.py)%20uses.%0A%20%20%20%20import%20json%0A%20%20%20%20import%20sqlite3%0A%20%20%20%20import%20tomllib%0A%20%20%20%20import%20warnings%0A%20%20%20%20from%20contextlib%20import%20closing%0A%20%20%20%20from%20pathlib%20import%20Path%0A%20%20%20%20from%20typing%20import%20Literal%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20pandera.pandas%20as%20pa%20%20%23%20pandera%3A%20write%20down%20the%20rules%20a%20correct%20table%20must%20meet%2C%20and%20check%20them%0A%20%20%20%20import%20tomli_w%0A%20%20%20%20from%20pydantic%20import%20BaseModel%2C%20ConfigDict%2C%20Field%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20import%20explain%20%20%23%20the%20explainers'%20drawings%2C%20shared%20with%20the%20primers%20(90_primers.py)%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20look%2C%20pick_first%2C%20waiting_for%0A%0A%20%20%20%20ROUND_ID%20%3D%20%222024Q1%22%20%20%23%20the%20round%20this%20module%20brings%20in%0A%20%20%20%20PREVIOUS_ROUND%20%3D%20%222023Q4%22%0A%20%20%20%20REGIONS%20%3D%20%5B%2201%22%2C%20%2202%22%2C%20%2203%22%2C%20%2204%22%2C%20%2205%22%2C%20%2206%22%5D%0A%20%20%20%20PUBLISHED_YEAR%20%3D%202023%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Check%3A%20Round%202%20arrives%0A%0A%20%20%20%20**Where%20you%20are%3A**%20Get%20%E2%86%92%20**Check**%20%E2%86%92%20Store%20%E2%86%92%20Share.%20In%20the%20Get%20module%20you%20built%20the%20**Get**%20stage%3A%20Round%201%20is%20read%2C%20joined%20to%20the%20code%20list%2C%20and%20staged%20in%20%60survey.duckdb%60.%20Now%20Round%202%20arrives%2C%20and%20it%20is%20not%20as%20tidy.%0A%0A%20%20%20%20In%20this%20module%20you%20will%3A%0A%0A%20%20%20%201.%20bring%20Round%202%20in%20with%20the%20Get%20module's%20code%2C%20by%20changing%20only%20its%20configuration%3B%0A%20%20%20%202.%20write%20down%2C%20once%2C%20the%20**rules**%20a%20correct%20round%20must%20meet%3A%20the%20**contract**%3B%0A%20%20%20%203.%20read%20the%20complete%20list%20of%20what%20breaks%20those%20rules%2C%20and%20the%20warnings%20that%20are%20not%20failures%3B%0A%20%20%20%204.%20**clean**%20with%20code%20that%20runs%20again%20next%20quarter%2C%20recording%20every%20decision%3B%0A%20%20%20%205.%20see%20the%20contract%20pass%2C%20and%20run%20it%20all%20with%20%60run.py%60.%0A%0A%20%20%20%20Then%20the%20Store%20module%20(%6022_store_round.py%60)%20stores%20the%20clean%20rounds%20in%20a%20database%20whose%20rules%20come%20from%20the%20same%20contract.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20Get%20module%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20last%20line%20first%3B%20primer%20%5B%C2%A710%5D(%2F%3Ffile%3D90_primers.py)%20shows%20how.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%223.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.mermaid(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20%20%20%20%20A%5B%22Get%22%5D%20--%3E%20B%5B%22Check%22%5D%3A%3A%3Anow%20--%3E%20C%5B%22Store%22%5D%20--%3E%20D%5B%22Share%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20classDef%20now%20fill%3A%23ffa500%2Cstroke%3A%230c2340%2Ccolor%3A%230c2340%2Cfont-weight%3Abold%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Round%202%20lands%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2025%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20that%20a%20pipeline%20survives%20a%20new%20round%20when%20only%20its%20configuration%20changes.%0A%20%20%20%20-%20**You%20build%3A**%20Round%202's%20configuration%2C%20and%20Round%202%20staged%20in%20%60survey.duckdb%60%20by%20the%20Get%20module's%20code.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_raw%20%3D%20pd.read_csv(PATHS.rounds%20%2F%20ROUND_ID%20%2F%20%22survey.csv%22%2C%20dtype%3Dstr%2C%20nrows%3D5)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20Round%202%2C%20the%20first%20quarter%20of%202024%2C%20is%20a%20CSV%20file%3A%20%60rounds%2F%7BROUND_ID%7D%2Fsurvey.csv%60.%20Its%20columns%20have%20**other%20names**%20than%20Round%201's.%20Here%20are%20its%20first%20rows%3A%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20_raw%2C%0A%20%20%20%20%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Do%20this%20now%2C%20in%20%6010_get_round.py%60**%20(%5Bopen%20it%5D(%2F%3Ffile%3D10_get_round.py))%3A%0A%0A%20%20%20%201.%20In%20section%202%2C%20pick%20%60rounds%2F2024Q1%2Fsurvey.csv%60%20in%20the%20file%20browser.%0A%20%20%20%202.%20Choose%20the%20column%20for%20each%20role.%20Two%20of%20them%20have%20names%20the%20dropdowns%20could%20not%20guess%3A%20%60LINE_NO%60%20(the%20member%20number)%20and%20%60HOURS_WORKED%60.%0A%20%20%20%203.%20Type%20the%20round%20ID%20**%602024Q1%60**%2C%20keep%20the%20code%20list%20edition%20**2023Q4**%2C%20and%20save%20the%20configuration.%0A%20%20%20%204.%20In%20a%20terminal%2C%20run%20%60uv%20run%20python%20run.py%20--round%202024Q1%60.%20It%20stops%3A%20Round%202%20has%20an%20area%20code%20that%20is%20not%20in%20the%20code%20list%2C%20and%20every%20round%20needs%20its%20own%20decision.%20Make%20it%20in%20section%203%20(*Decide*)%20for%20round%202024Q1%2C%20then%20run%20the%20command%20again.%0A%0A%20%20%20%20The%20Get%20module's%20code%20did%20all%20of%20that.%20You%20changed%20only%20the%20configuration%3A%20that%20is%20the%20point%20of%20describing%20each%20round%20instead%20of%20rewriting%20code%20for%20it.%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20load_staged(round_id%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20round%20as%20the%20Get%20stage%20left%20it%3A%20the%20table%20staging_%3Cround%3E%20in%20survey.duckdb.%22%22%22%0A%20%20%20%20with%20course.database(read_only%3DTrue)%20as%20con%3A%0A%20%20%20%20%20%20%20%20return%20con.sql(f%22SELECT%20*%20FROM%20staging_%7Bround_id.lower()%7D%22).df()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20staged_button%20%3D%20mo.ui.run_button(label%3Df%22Load%20staged%20round%20%7BROUND_ID%7D%20(after%20run.py%20has%20staged%20it)%22)%0A%20%20%20%20staged_button%0A%20%20%20%20return%20(staged_button%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(staged_button)%3A%0A%20%20%20%20staged_button%20%20%23%20pressing%20the%20button%20runs%20this%20cell%20again%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20round2%20%3D%20load_staged(ROUND_ID)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(%5Bmo.md(f%22**Round%20%7BROUND_ID%7D%20staged%3A**%20%7Blen(round2)%3A%2C%7D%20rows%2C%20in%20the%20standard%20columns.%20The%20first%20rows%3A%22)%2C%20round2.head()%5D)%0A%20%20%20%20except%20Exception%20as%20_error%3A%0A%20%20%20%20%20%20%20%20round2%20%3D%20NOT_YET%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Round%20%7BROUND_ID%7D%20is%20not%20staged%20yet%20(%7Btype(_error).__name__%7D).%20Do%20the%20four%20steps%20above%2C%20then%20press%20the%20button.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20_out%0A%20%20%20%20return%20(round2%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round2)%3A%0A%20%20%20%20mo.stop(round2%20is%20NOT_YET)%0A%20%20%20%20_short%20%3D%20int((pd.read_csv(PATHS.rounds%20%2F%20ROUND_ID%20%2F%20%22survey.csv%22%2C%20dtype%3Dstr)%5B%22PSGC%22%5D.str.len()%20%3D%3D%209).sum())%0A%20%20%20%20mo.callout(mo.md(%0A%20%20%20%20%20%20%20%20f%22**The%20Get%20module's%20work%20paying%20off.**%20The%20file%20has%20**%7B_short%7D%20area%20codes%20with%209%20digits**%3A%20they%20lost%20their%20leading%20zero%20somewhere%20along%20the%20way.%20%22%0A%20%20%20%20%20%20%20%20f%22After%20the%20Get%20stage%2C%20every%20area%20code%20has%2010%20characters%20again%3A%20your%20%60code_to_text%60%20repaired%20them%20without%20anyone%20asking.%20%22%0A%20%20%20%20%20%20%20%20f%22Every%20other%20problem%20in%20this%20file%20is%20still%20there.%20Finding%20them%20is%20this%20module's%20work.%22%0A%20%20%20%20)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20The%20contract%3A%20rules%20written%20once%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2055%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20how%20to%20write%20a%20table's%20rules%20as%20a%20**schema**%20with%20pandera%2C%20run%20it%20on%20the%20whole%20round%2C%20and%20read%20the%20complete%20report%20of%20everything%20that%20breaks%20them.%0A%20%20%20%20-%20**You%20build%3A**%20%60survey_schema%60%2C%20the%20contract%20every%20round%20must%20meet%20from%20now%20on%2C%20and%20%60failure_summary%60%2C%20a%20short%20version%20of%20the%20report.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20the%20Get%20module%20you%20checked%20Round%201%20by%20looking%3A%20rows%20in%20and%20out%2C%20code%20lengths.%20Looking%20does%20not%20scale%20to%20every%20round%2C%20every%20quarter%2C%20for%20years.%20Instead%20you%20write%20the%20rules%20down%20**once**%2C%20in%20a%20form%20a%20program%20can%20check%3A%20a%20**contract**%20for%20what%20a%20correct%20round%20looks%20like.%0A%0A%20%20%20%20**pandera**%20writes%20that%20contract%20as%20a%20**schema**%3A%20one%20line%20per%20column%2C%20saying%20its%20type%20and%20the%20checks%20its%20values%20must%20pass%2C%20plus%20rules%20that%20involve%20several%20columns.%20%60schema.validate(table%2C%20lazy%3DTrue)%60%20checks%20every%20rule%20on%20every%20row%2C%20and%20if%20anything%20fails%2C%20gives%20you%20**all**%20the%20failures%20at%20once%2C%20in%20a%20table.%0A%0A%20%20%20%20%23%23%23%23%20Concept%3A%20a%20schema%20and%20its%20report%0A%20%20%20%20Pick%20which%20rules%20the%20schema%20has%2C%20and%20switch%20%60lazy%60%20on%20and%20off%2C%20on%20a%20five-row%20table%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20card_rules%20%3D%20mo.ui.multiselect(options%3Dlist(explain.PANDERA_RULES)%2C%20value%3Dlist(explain.PANDERA_RULES)%5B%3A2%5D%2C%20label%3D%22rules%3A%22)%0A%20%20%20%20card_lazy%20%3D%20mo.ui.checkbox(label%3D%22lazy%3DTrue%20(the%20full%20report)%22%2C%20value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bcard_rules%2C%20card_lazy%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20card_lazy%2C%20card_rules%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_lazy%2C%20card_rules)%3A%0A%20%20%20%20explain.pandera_demo(card_rules.value%2C%20card_lazy.value)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20More%2C%20with%20common%20mistakes%3A%20%5Bprimer%2011%2C%20Rules%20with%20pandera%5D(%2F%3Ffile%3D90_primers.py).%0A%0A%20%20%20%20%23%23%23%23%20The%20pieces%20of%20a%20pandera%20schema%0A%0A%20%20%20%20%7C%20piece%20%7C%20means%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60pa.Column(str)%60%20%7C%20the%20column%20holds%20text%20%7C%0A%20%20%20%20%7C%20%60pa.Column(float%2C%20coerce%3DTrue)%60%20%7C%20the%20column%20holds%20numbers%3B%20whole%20numbers%20are%20turned%20into%20decimals%20first%20%7C%0A%20%20%20%20%7C%20%60nullable%3DTrue%60%20%7C%20missing%20values%20are%20allowed%20(without%20it%2C%20a%20missing%20value%20is%20a%20failure)%20%7C%0A%20%20%20%20%7C%20%60pa.Check.in_range(0%2C%20120)%60%20%7C%20every%20value%20is%20between%200%20and%20120%20%7C%0A%20%20%20%20%7C%20%60pa.Check.isin(%5B%221%22%2C%20%222%22%5D)%60%20%7C%20every%20value%20is%20one%20of%20these%20%7C%0A%20%20%20%20%7C%20%60pa.Check.gt(0)%60%20%7C%20every%20value%20is%20greater%20than%200%20(%60ge%60%3A%20greater%20or%20equal%3B%20%60lt%60%2C%20%60le%60%3A%20less%20than%2C%20less%20or%20equal)%20%7C%0A%20%20%20%20%7C%20%60pa.Check.str_matches(r%22%5E%5Cd%7B10%7D%24%22)%60%20%7C%20every%20text%20has%20exactly%2010%20digits%20(%60%5E%60%20start%2C%20%60%5Cd%7B10%7D%60%20ten%20digits%2C%20%60%24%60%20end)%20%7C%0A%20%20%20%20%7C%20%60unique%3D%5B%22hhid%22%2C%20%22member_no%22%5D%60%20%7C%20no%20two%20rows%20share%20these%20values%3A%20each%20person%20once%20%7C%0A%20%20%20%20%7C%20%60checks%3D%5Bpa.Check(lambda%20t%3A%20...%2C%20name%3D%22...%22)%5D%60%20%7C%20a%20rule%20on%20several%20columns%20at%20once%3A%20%60t%60%20is%20the%20whole%20table%2C%20and%20the%20rule%20gives%20%60True%60%20or%20%60False%60%20for%20each%20row%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.1.%20The%20contract%20for%20a%20survey%20round%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20schema%20is%20the%20single%20place%20where%20%22a%20correct%20round%22%20is%20defined.%20Every%20later%20round%20is%20checked%20against%20it%2C%20and%20in%20the%20Store%20module%20the%20database's%20own%20rules%20are%20generated%20from%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20set%20of%20area%20codes%20in%20the%20code%20list%20%E2%86%92%20a%20pandera%20%60DataFrameSchema%60.%0A%0A%20%20%20%20The%20schema%20below%20is%20half%20written.%20The%20comments%20mark%20the%20**six%20rules**%20to%20add%3B%20the%20explainer%20and%20the%20table%20above%20show%20every%20piece%20you%20need.%20The%20rules%20a%20correct%20round%20must%20meet%3A%0A%0A%20%20%20%20%7C%20column%20or%20rule%20%7C%20the%20rule%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60age%60%20%7C%20a%20number%20between%200%20and%20120%3B%20may%20be%20missing%20(a%20person%20who%20did%20not%20say)%20%7C%0A%20%20%20%20%7C%20%60sex%60%20%7C%20%60%221%22%60%20or%20%60%222%22%60%3B%20may%20be%20missing%20%7C%0A%20%20%20%20%7C%20%60hours%60%20%7C%20a%20number%20between%200%20and%20112%20(the%20hours%20in%20a%20week%20a%20person%20can%20work%2C%20at%20most%2016%20a%20day)%3B%20may%20be%20missing%20%7C%0A%20%20%20%20%7C%20%60weight%60%20%7C%20a%20number%20greater%20than%200%3B%20never%20missing%20%7C%0A%20%20%20%20%7C%20each%20person%20once%20%7C%20no%20two%20rows%20share%20%60hhid%60%20and%20%60member_no%60%20%7C%0A%20%20%20%20%7C%20region%20matches%20area%20%7C%20%60region%60%20is%20the%20first%20two%20characters%20of%20%60area_code%60%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20survey_schema(area_codes%3A%20set%5Bstr%5D)%20-%3E%20pa.DataFrameSchema%3A%0A%20%20%20%20%22%22%22The%20contract%3A%20the%20rules%20every%20staged%20round%20must%20meet.%22%22%22%0A%20%20%20%20%23%20Exercise%202.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20The%20skeleton%20is%20commented%20out.%20Select%20all%20of%20it%20and%20press%20Ctrl%20%2B%20%2F%20(Mac%3A%20Cmd%20%2B%20%2F)%20to%20uncomment%20it%2C%0A%20%20%20%20%23%20then%20replace%20each%20...%20with%20the%20rule%20its%20comment%20describes.%0A%20%20%20%20%23%20return%20pa.DataFrameSchema(%0A%20%20%20%20%23%20%20%20%20%20%7B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22round%22%3A%20pa.Column(str)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22hhid%22%3A%20pa.Column(str)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22member_no%22%3A%20pa.Column(int%2C%20pa.Check.ge(1)%2C%20coerce%3DTrue)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22region%22%3A%20pa.Column(str%2C%20pa.Check.isin(REGIONS))%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22area_code%22%3A%20pa.Column(str%2C%20pa.Check.str_matches(r%22%5E%5Cd%7B10%7D%24%22))%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22age%22%3A%20...%2C%20%20%23%20rule%201%3A%20a%20number%20from%200%20to%20120%2C%20may%20be%20missing%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22sex%22%3A%20...%2C%20%20%23%20rule%202%3A%20%221%22%20or%20%222%22%2C%20may%20be%20missing%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22employed%22%3A%20pa.Column(str%2C%20pa.Check.isin(%5B%221%22%2C%20%222%22%5D)%2C%20nullable%3DTrue)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22hours%22%3A%20...%2C%20%20%23%20rule%203%3A%20a%20number%20from%200%20to%20112%2C%20may%20be%20missing%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22weight%22%3A%20...%2C%20%20%23%20rule%204%3A%20a%20number%20greater%20than%200%2C%20never%20missing%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%22in_code_list%22%3A%20pa.Column(bool)%2C%0A%20%20%20%20%23%20%20%20%20%20%7D%2C%0A%20%20%20%20%23%20%20%20%20%20checks%3D%5B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20...%2C%20%20%23%20rule%206%3A%20region%20is%20the%20first%20two%20characters%20of%20area_code%20(a%20lambda%20on%20the%20table%20t)%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20pa.Check(lambda%20t%3A%20t%5B%22hours%22%5D.isna()%20%7C%20t%5B%22employed%22%5D.eq(%221%22)%2C%20name%3D%22hours%20only%20for%20the%20employed%22)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20pa.Check(lambda%20t%3A%20t%5B%22in_code_list%22%5D%20%3D%3D%20t%5B%22area_code%22%5D.isin(area_codes)%2C%20name%3D%22in_code_list%20says%20whether%20the%20code%20is%20in%20the%20code%20list%22)%2C%0A%20%20%20%20%23%20%20%20%20%20%5D%2C%0A%20%20%20%20%23%20%20%20%20%20unique%3D...%2C%20%20%23%20rule%205%3A%20each%20person%20once%0A%20%20%20%20%23%20)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.1%22)%0A%0A%0A%40app.function%0Adef%20contract_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20set%5Bstr%5D%5D%3A%0A%20%20%20%20%22%22%22A%20small%20round%20with%20one%20problem%20per%20row%20(rows%201%20to%208)%2C%20used%20by%20the%20check%20of%20Exercise%202.1.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22H1%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%2034.0%2C%20%221%22%2C%20%221%22%2C%2040.0%2C%20500.0%2C%20True)%2C%20%20%20%23%200%3A%20correct%0A%20%20%20%20%20%20%20%20(%22H2%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%20150.0%2C%20%221%22%2C%20%221%22%2C%2040.0%2C%20500.0%2C%20True)%2C%20%20%23%201%3A%20age%20above%20120%0A%20%20%20%20%20%20%20%20(%22H3%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%2030.0%2C%20%223%22%2C%20%221%22%2C%2040.0%2C%20500.0%2C%20True)%2C%20%20%20%23%202%3A%20sex%203%0A%20%20%20%20%20%20%20%20(%22H4%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%2030.0%2C%20%222%22%2C%20%221%22%2C%20140.0%2C%20500.0%2C%20True)%2C%20%20%23%203%3A%20hours%20above%20112%0A%20%20%20%20%20%20%20%20(%22H5%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%2030.0%2C%20%222%22%2C%20%221%22%2C%2040.0%2C%200.0%2C%20True)%2C%20%20%20%20%20%23%204%3A%20weight%200%0A%20%20%20%20%20%20%20%20(%22H6%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%2030.0%2C%20%222%22%2C%20%222%22%2C%2040.0%2C%20500.0%2C%20True)%2C%20%20%20%23%205%3A%20hours%2C%20not%20employed%0A%20%20%20%20%20%20%20%20(%22H1%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%2034.0%2C%20%221%22%2C%20%221%22%2C%2040.0%2C%20500.0%2C%20True)%2C%20%20%20%23%206%3A%20same%20person%20as%20row%200%0A%20%20%20%20%20%20%20%20(%22H7%22%2C%201%2C%20%2202%22%2C%20%220100105000%22%2C%2030.0%2C%20%222%22%2C%20%221%22%2C%2040.0%2C%20500.0%2C%20True)%2C%20%20%20%23%207%3A%20region%20does%20not%20match%0A%20%20%20%20%20%20%20%20(%22H8%22%2C%201%2C%20%2201%22%2C%20%220100105000%22%2C%20None%2C%20None%2C%20None%2C%20None%2C%20500.0%2C%20True)%2C%20%23%208%3A%20missing%20age%2C%20sex%2C%20hours%3A%20allowed%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22hhid%22%2C%20%22member_no%22%2C%20%22region%22%2C%20%22area_code%22%2C%20%22age%22%2C%20%22sex%22%2C%20%22employed%22%2C%20%22hours%22%2C%20%22weight%22%2C%20%22in_code_list%22%5D)%0A%20%20%20%20table.insert(0%2C%20%22round%22%2C%20%222024Q1%22)%0A%20%20%20%20return%20table%2C%20%7B%220100105000%22%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20def%20_failing_rows(schema)%3A%0A%20%20%20%20%20%20%20%20%23%20Which%20rows%20break%20a%20rule%2C%20and%20how%20many%20failures%20in%20all%3A%20the%20same%20answer%20for%20any%20correct%20way%20of%20writing%20the%20rules%0A%20%20%20%20%20%20%20%20table%2C%20_%20%3D%20contract_fixture()%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20schema.validate(table%2C%20lazy%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%5B%5D%0A%20%20%20%20%20%20%20%20except%20pa.errors.SchemaErrors%20as%20errors%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%3D%20errors.failure_cases%5B%22index%22%5D.dropna().astype(int)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20sorted(set(rows.tolist()))%0A%0A%20%20%20%20_table%2C%20_codes%20%3D%20contract_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.1%22%2C%20survey_schema%2C%20%5B(%22a%20small%20round%20with%20one%20problem%20in%20each%20of%20rows%201%20to%207%22%2C%20(_codes%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22test%20tables%22%2C%20summarize%3D_failing_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Rows%201%20to%207%20each%20break%20one%20rule%20(row%206%20copies%20row%200%2C%20so%20the%20each-person-once%20rule%20flags%20both)%3B%20row%208%20breaks%20none.%20Use%20the%20Try%20it%20cell%20to%20see%20which%20rows%20your%20schema%20flags.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20run%20your%20schema%20on%20the%20small%20test%20round%2C%20and%20see%20which%20rows%20fail%20which%20rule.%0A%20%20%20%20def%20_try_contract()%3A%0A%20%20%20%20%20%20%20%20table%2C%20codes%20%3D%20contract_fixture()%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20survey_schema(codes).validate(table%2C%20lazy%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.md(%22No%20failures%20at%20all%3A%20every%20row%20passed.%20Rows%201%20to%207%20should%20not.%22)%0A%20%20%20%20%20%20%20%20except%20pa.errors.SchemaErrors%20as%20errors%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20errors.failure_cases%5B%5B%22column%22%2C%20%22check%22%2C%20%22failure_case%22%2C%20%22index%22%5D%5D%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20schema%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%0A%20%20%20%20_try_contract()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Row%208%20is%20flagged%20%7C%20A%20rule%20refuses%20missing%20values%20where%20they%20are%20allowed.%20%7C%20%60nullable%3DTrue%60%20on%20age%2C%20sex%2C%20hours%20%7C%0A%20%20%20%20%7C%20Row%206%20is%20not%20flagged%20%7C%20The%20uniqueness%20rule%20is%20missing.%20%7C%20%60unique%3D%5B%22hhid%22%2C%20%22member_no%22%5D%60%20%7C%0A%20%20%20%20%7C%20Row%207%20is%20not%20flagged%20%7C%20The%20region%20rule%20is%20missing%2C%20or%20compares%20the%20wrong%20part.%20%7C%20%60t%5B%22region%22%5D%20%3D%3D%20t%5B%22area_code%22%5D.str%5B%3A2%5D%60%20%7C%0A%20%20%20%20%7C%20*SchemaError%3A%20expected%20series%20%E2%80%A6%20to%20have%20type%20float64%2C%20got%20int64*%20%7C%20A%20number%20column%20without%20%60coerce%3DTrue%60.%20%7C%20add%20%60coerce%3DTrue%60%20to%20age%2C%20hours%2C%20weight%20%7C%0A%20%20%20%20%7C%20*NameError*%20inside%20a%20lambda%20%7C%20The%20table%20is%20called%20%60t%60%20inside%20the%20lambda.%20%7C%20%60lambda%20t%3A%20t%5B%22region%22%5D%20...%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Run%20the%20contract%20on%20Round%202%0A%0A%20%20%20%20Now%20the%20real%20thing%3A%20your%20schema%20on%20the%20whole%20of%20Round%202%2C%20with%20%60lazy%3DTrue%60.%20Every%20failure%2C%20every%20rule%2C%20every%20row%2C%20in%20one%20table.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20round_edition(round_id%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20code%20list%20edition%20a%20round%20uses%2C%20from%20its%20saved%20configuration%20(2023Q4%20if%20it%20has%20none%20yet).%22%22%22%0A%20%20%20%20path%20%3D%20PATHS.config%20%2F%20f%22%7Bround_id%7D.toml%22%0A%20%20%20%20return%20tomllib.loads(path.read_text(encoding%3D%22utf-8%22)).get(%22code_list_edition%22%2C%20%222023Q4%22)%20if%20path.exists()%20else%20%222023Q4%22%0A%0A%0A%40app.function%0Adef%20code_list_codes(edition%3A%20str%20%3D%20%222023Q4%22)%20-%3E%20set%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20area%20codes%20of%20one%20edition%20of%20the%20code%20list%2C%20read%20the%20way%20the%20Get%20module%20read%20it.%22%22%22%0A%20%20%20%20table%20%3D%20pd.read_excel(PATHS.reference%20%2F%20f%22psgc_%7Bedition%7D.xlsx%22%2C%20header%3D3%2C%20dtype%3Dstr)%0A%20%20%20%20codes%20%3D%20table%5B%22Code%22%5D.dropna()%0A%20%20%20%20return%20set(codes%5Bcodes.str.fullmatch(r%22%5Cd%2B%22)%5D.str.zfill(10))%0A%0A%0A%40app.function%0Adef%20validate_round(table%3A%20pd.DataFrame%2C%20schema%3A%20pa.DataFrameSchema)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Check%20a%20round%20against%20the%20contract.%20Returns%20the%20failures%20(an%20empty%20table%20when%20there%20are%20none).%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20schema.validate(table%2C%20lazy%3DTrue)%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(columns%3D%5B%22column%22%2C%20%22check%22%2C%20%22failure_case%22%2C%20%22index%22%5D)%0A%20%20%20%20except%20pa.errors.SchemaErrors%20as%20errors%3A%0A%20%20%20%20%20%20%20%20failures%20%3D%20errors.failure_cases%0A%20%20%20%20%20%20%20%20%23%20A%20rule%20on%20several%20columns%20is%20reported%20once%20per%20column%20of%20each%20failing%20row%3B%20keep%20one%20line%20per%20row%2C%20with%20no%20column%0A%20%20%20%20%20%20%20%20unique%20%3D%20failures%5B%22check%22%5D.astype(str).eq(%22multiple_fields_uniqueness%22)%0A%20%20%20%20%20%20%20%20several%20%3D%20failures%5B%22schema_context%22%5D.eq(%22DataFrameSchema%22)%20%26%20~unique%0A%20%20%20%20%20%20%20%20collapsed%20%3D%20failures%5Bseveral%5D.drop_duplicates(subset%3D%5B%22check%22%2C%20%22index%22%5D).assign(column%3DNone%2C%20failure_case%3D%22(the%20whole%20row)%22)%0A%20%20%20%20%20%20%20%20%23%20The%20each-person-once%20rule%20likewise%3A%20one%20line%20per%20row%2C%20showing%20the%20household%20ID%0A%20%20%20%20%20%20%20%20people%20%3D%20failures%5Bunique%20%26%20failures%5B%22column%22%5D.eq(%22hhid%22)%5D.assign(column%3D%22hhid%2C%20member_no%22)%0A%20%20%20%20%20%20%20%20failures%20%3D%20pd.concat(%5Bfailures%5B~several%20%26%20~unique%5D%2C%20collapsed%2C%20people%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20%20%20%20%20return%20failures%5B%5B%22column%22%2C%20%22check%22%2C%20%22failure_case%22%2C%20%22index%22%5D%5D.reset_index(drop%3DTrue)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round2)%3A%0A%20%20%20%20mo.stop(round2%20is%20NOT_YET%2C%20waiting_for(%22Round%202%2C%20staged%20(section%201)%22))%0A%20%20%20%20_schema%20%3D%20attempt(survey_schema%2C%20code_list_codes(round_edition(ROUND_ID)))%0A%20%20%20%20mo.stop(_schema%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.1%22))%0A%20%20%20%20raw_failures%20%3D%20validate_round(round2%2C%20_schema)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(raw_failures)%3A%2C%7D%20failures**%20in%20Round%202.%20The%20first%20rows%20of%20the%20report%3A%22)%2C%0A%20%20%20%20%20%20%20%20raw_failures.head(15)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(raw_failures%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.2.%20A%20short%20version%20of%20the%20report%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20report%20of%20dozens%20of%20rows%20is%20hard%20to%20read%20in%20a%20meeting.%20A%20summary%20with%20one%20row%20per%20rule%20says%20what%20is%20wrong%20and%20how%20often%2C%20and%20points%20to%20where%20to%20look.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20failures%20table%20(columns%20%60column%60%2C%20%60check%60%2C%20%60failure_case%60%2C%20%60index%60)%20%E2%86%92%20one%20row%20per%20column%20and%20rule%2C%20with%20%60failures%60%20(how%20many)%20and%20%60example%60%20(the%20first%20failing%20value)%2C%20the%20most%20frequent%20first.%0A%0A%20%20%20%20%7C%20column%20%7C%20check%20%7C%20failures%20%7C%20example%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20age%20%7C%20in_range(0%2C%20120)%20%7C%206%20%7C%20999.0%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20Tools%3A%20grouping%2C%20as%20in%20the%20Get%20module%20(primer%20%5B%C2%A75%5D(%2F%3Ffile%3D90_primers.py))%3A%20%60table.groupby(%5B%22column%22%2C%20%22check%22%5D%2C%20dropna%3DFalse).agg(failures%3D(%22failure_case%22%2C%20%22size%22)%2C%20example%3D(%22failure_case%22%2C%20%22first%22))%60.%20%60dropna%3DFalse%60%20keeps%20rules%20that%20involve%20several%20columns%2C%20whose%20%60column%60%20is%20empty.%20Then%20%60.reset_index()%60%20and%20sort%20by%20%60failures%60%2C%20largest%20first%2C%20then%20by%20%60check%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20failure_summary(failures%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20column%20and%20rule%3A%20how%20many%20failures%2C%20and%20one%20example%20value.%20Most%20frequent%20first.%22%22%22%0A%20%20%20%20%23%20Exercise%202.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20group%20the%20failures%20by%20column%20and%20check%20(dropna%3DFalse)%2C%20counting%20them%20and%20taking%20the%20first%20value%0A%20%20%20%20%23%20Step%202%3A%20.reset_index()%2C%20sort%20by%20failures%20(largest%20first)%20then%20check%2C%20.reset_index(drop%3DTrue)%2C%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_failures%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22column%22%3A%20%5B%22age%22%2C%20%22age%22%2C%20%22sex%22%2C%20None%2C%20%22weight%22%2C%20%22age%22%5D%2C%0A%20%20%20%20%20%20%20%20%22check%22%3A%20%5B%22in_range(0%2C%20120)%22%2C%20%22in_range(0%2C%20120)%22%2C%20%22isin(%5B'1'%2C%20'2'%5D)%22%2C%20%22hours%20only%20for%20the%20employed%22%2C%20%22greater_than(0)%22%2C%20%22in_range(0%2C%20120)%22%5D%2C%0A%20%20%20%20%20%20%20%20%22failure_case%22%3A%20%5B999.0%2C%20999.0%2C%20%223%22%2C%20False%2C%200.0%2C%20150.0%5D%2C%0A%20%20%20%20%20%20%20%20%22index%22%3A%20%5B4%2C%209%2C%202%2C%207%2C%203%2C%2012%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.2%22%2C%20failure_summary%2C%20%5B(%22a%20report%20of%20six%20failures%22%2C%20(_failures%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22reports%22%2C%20summarize%3Dlambda%20t%3A%20%5B%5BNone%20if%20pd.isna(c)%20else%20str(c)%2C%20str(k)%2C%20int(n)%2C%20str(e)%5D%20for%20c%2C%20k%2C%20n%2C%20e%20in%20t%5B%5B%22column%22%2C%20%22check%22%2C%20%22failures%22%2C%20%22example%22%5D%5D.itertuples(index%3DFalse)%5D%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Four%20rules%20failed%3A%20age%203%20times%2C%20then%20three%20rules%20once%20each%2C%20in%20the%20order%20of%20their%20check%20names.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_failures)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20summary%20of%20Round%202's%20report.%0A%20%20%20%20course.try_it(failure_summary%2C%20raw_failures)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20A%20rule%20is%20missing%20from%20the%20summary%20%7C%20Rules%20on%20several%20columns%20have%20no%20%60column%60%2C%20and%20%60groupby%60%20drops%20empty%20keys%20by%20default.%20%7C%20%60dropna%3DFalse%60%20%7C%0A%20%20%20%20%7C%20The%20counts%20are%20right%2C%20the%20order%20is%20not%20%7C%20The%20sort%20is%20missing%20or%20reversed.%20%7C%20%60failures%60%20largest%20first%2C%20then%20%60check%60%20%7C%0A%20%20%20%20%7C%20%60example%60%20holds%20a%20count%20%7C%20The%20second%20result%20counts%20again.%20%7C%20%60example%3D(%22failure_case%22%2C%20%22first%22)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_failures)%3A%0A%20%20%20%20_summary%20%3D%20attempt(failure_summary%2C%20raw_failures)%0A%20%20%20%20mo.stop(_summary%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.2%22))%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Round%202's%20report%2C%20summarised%3A**%22)%2C%20_summary%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20A%20contract%20is%20the%20rules%20of%20a%20correct%20table%2C%20written%20once%2C%20checked%20by%20a%20program%20on%20every%20round.%0A%20%20%20%20-%20%60lazy%3DTrue%60%20reports%20**every**%20failure%20at%20once%2C%20so%20you%20see%20the%20whole%20problem%20before%20fixing%20any%20of%20it.%0A%20%20%20%20-%20Some%20rules%20are%20about%20one%20column%2C%20some%20about%20several%3B%20both%20live%20in%20the%20same%20schema.%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20contract%2C%20schema%2C%20lazy%20validation%2C%20failure%20report.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Warnings%2C%20and%20looking%20before%20you%20change%20a%20rule%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2045%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20that%20some%20checks%20should%20**warn**%20rather%20than%20fail%2C%20and%20that%20a%20failure%20is%20a%20question%20about%20the%20data%20before%20it%20is%20a%20reason%20to%20change%20a%20rule.%0A%20%20%20%20-%20**You%20build%3A**%20%60regions_outside%60%2C%20the%20heart%20of%20a%20warning%20that%20compares%20every%20round%20with%20the%20published%20totals.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Some%20problems%20are%20certain%3A%20an%20age%20of%20150%20is%20wrong.%20Others%20are%20only%20**unusual**%3A%20a%20region%20whose%20weighted%20total%20is%202.6%20%25%20above%20the%20published%20figure%20may%20have%20a%20reason.%20A%20check%20for%20an%20unusual%20thing%20should%20**warn**%20(tell%20someone%2C%20and%20let%20the%20data%20through)%20rather%20than%20**fail**%20(stop%20everything).%20In%20pandera%2C%20a%20check%20warns%20instead%20of%20failing%20when%20it%20has%20%60raise_warning%3DTrue%60.%0A%0A%20%20%20%20In%20the%20Get%20module%20you%20compared%20Round%201%20with%20the%20published%20totals%20by%20hand.%20Here%20that%20comparison%20becomes%20a%20warning%20that%20runs%20on%20every%20round.%0A%0A%20%20%20%20%23%23%23%20Exercise%203.1.%20Which%20regions%20are%20far%20from%20the%20published%20totals%3F%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20comparison%20you%20will%20run%20on%20every%20round%20from%20now%20on%3B%20written%20as%20a%20function%2C%20it%20can.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20round%2C%20the%20published%20population%20(%60region%60%2C%20%60year%60%2C%20%60population%60)%2C%20a%20year%20and%20a%20tolerance%20%E2%86%92%20the%20sorted%20list%20of%20regions%20whose%20weighted%20total%20is%20further%20than%20the%20tolerance%20from%20the%20published%20figure.%0A%0A%20%20%20%20%7C%20call%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60regions_outside(round2%2C%20published%2C%202023%2C%200.01)%60%20%7C%20%60%5B%2204%22%5D%60%20%7C%0A%20%20%20%20%7C%20%60regions_outside(round2%2C%20published%2C%202023%2C%200.05)%60%20%7C%20%60%5B%5D%60%20%7C%0A%0A%20%20%20%20It%20is%20the%20Get%20module's%20Exercise%204.4%2C%20made%20shorter%3A%20sum%20the%20weights%20per%20region%2C%20compare%20with%20the%20published%20population%2C%20keep%20the%20regions%20where%20%60abs(survey%20-%20published)%20%2F%20published%60%20is%20greater%20than%20the%20tolerance.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20published_population()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20published%20population%20by%20region%20and%20year%2C%20from%20the%20API%20pages%20(region%2C%20year%2C%20population).%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20page_name%20in%20(%22page-1.json%22%2C%20%22page-2.json%22)%3A%0A%20%20%20%20%20%20%20%20page%20%3D%20json.loads((PATHS.reference%20%2F%20%22openstat_api%22%20%2F%20page_name).read_text(encoding%3D%22utf-8%22))%0A%20%20%20%20%20%20%20%20for%20record%20in%20page%5B%22data%22%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20value%20%3D%20record%5B%22values%22%5D%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22region%22%3A%20record%5B%22key%22%5D%5B0%5D%2C%20%22year%22%3A%20int(record%5B%22key%22%5D%5B1%5D)%2C%20%22population%22%3A%20None%20if%20value%20in%20(%22..%22%2C%20%22-%22)%20else%20int(value)%7D)%0A%20%20%20%20return%20pd.DataFrame(rows)%0A%0A%0A%40app.function%0Adef%20regions_outside(round_df%3A%20pd.DataFrame%2C%20published%3A%20pd.DataFrame%2C%20year%3A%20int%2C%20tolerance%3A%20float)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20regions%20whose%20weighted%20total%20is%20further%20than%20%60tolerance%60%20(0.01%20%3D%201%20%25)%20from%20the%20published%20population.%22%22%22%0A%20%20%20%20%23%20Exercise%203.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20weighted%20total%20per%20region%3A%20round_df.groupby(%22region%22)%5B%22weight%22%5D.sum()%0A%20%20%20%20%23%20Step%202%3A%20the%20published%20population%20of%20%60year%60%20per%20region%2C%20as%20a%20Series%20indexed%20by%20region%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20sorted%20list%20of%20regions%20where%20abs(survey%20-%20published)%20%2F%20published%20%3E%20tolerance%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_published%20%3D%20pd.DataFrame(%7B%22region%22%3A%20%5B%2201%22%2C%20%2202%22%2C%20%2201%22%2C%20%2202%22%5D%2C%20%22year%22%3A%20%5B2023%2C%202023%2C%202022%2C%202022%5D%2C%20%22population%22%3A%20%5B1000%2C%202000%2C%20990%2C%201980%5D%7D)%0A%20%20%20%20_round%20%3D%20pd.DataFrame(%7B%22region%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2202%22%2C%20%2202%22%5D%2C%20%22weight%22%3A%20%5B500.0%2C%20520.0%2C%201000.0%2C%201000.0%5D%7D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.1%22%2C%20regions_outside%2C%0A%20%20%20%20%20%20%20%20%5B(%22two%20regions%2C%201%20%25%20tolerance%22%2C%20(_round%2C%20_published%2C%202023%2C%200.01))%2C%20(%22two%20regions%2C%205%20%25%20tolerance%22%2C%20(_round%2C%20_published%2C%202023%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20same%20against%202022%22%2C%20(_round%2C%20_published%2C%202022%2C%200.01))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22comparisons%22%2C%20hint%3D%22Region%2001%20is%202%20%25%20above%20its%202023%20figure%3B%20region%2002%20is%20exactly%20on%20it.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(round2)%3A%0A%20%20%20%20%23%20Try%20it%3A%20Round%202%20against%20the%20published%202023%20population%2C%20with%20a%201%20%25%20tolerance.%0A%20%20%20%20course.try_it(regions_outside%2C%20round2%2C%20published_population()%2C%20PUBLISHED_YEAR%2C%200.01)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Every%20region%20is%20listed%20%7C%20The%20tolerance%20is%20compared%20in%20percent%20(1)%20instead%20of%20as%20a%20share%20(0.01)%2C%20or%20the%20sign%20is%20kept.%20%7C%20divide%20by%20%60published%60%3B%20%60abs(...)%60%3B%20%60%3E%20tolerance%60%20%7C%0A%20%20%20%20%7C%20The%20wrong%20year's%20figures%20%7C%20The%20published%20table%20was%20not%20filtered%20to%20%60year%60.%20%7C%20%60published%5B%22year%22%5D%20%3D%3D%20year%60%20%7C%0A%20%20%20%20%7C%20%60NaN%60%20in%20the%20comparison%20%7C%20The%20two%20Series%20have%20regions%20in%20a%20different%20form%20(%601%60%20and%20%60'01'%60).%20%7C%20both%20indexed%20by%20the%20text%20region%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20warnings_for(round_df%3A%20pd.DataFrame%2C%20previous%3A%20pd.DataFrame%20%7C%20None%2C%20tolerance%3A%20float%20%3D%200.01)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20round's%20warnings%3A%20totals%20far%20from%20the%20published%20figures%2C%20and%20a%20weight%20distribution%20unlike%20the%20previous%20round's.%22%22%22%0A%20%20%20%20found%20%3D%20%5B%5D%0A%20%20%20%20far%20%3D%20regions_outside(round_df%2C%20published_population()%2C%20PUBLISHED_YEAR%2C%20tolerance)%0A%20%20%20%20if%20far%3A%0A%20%20%20%20%20%20%20%20found.append(f%22weighted%20totals%20more%20than%20%7Btolerance%3A.0%25%7D%20from%20the%20published%20%7BPUBLISHED_YEAR%7D%20population%20in%20region(s)%20%7B'%2C%20'.join(far)%7D%22)%0A%20%20%20%20if%20previous%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20change%20%3D%20round_df%5B%22weight%22%5D.mean()%20%2F%20previous%5B%22weight%22%5D.mean()%20-%201%0A%20%20%20%20%20%20%20%20if%20abs(change)%20%3E%200.10%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20found.append(f%22the%20average%20weight%20changed%20by%20%7Bchange%3A%2B.0%25%7D%20since%20the%20previous%20round%22)%0A%20%20%20%20return%20found%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round2)%3A%0A%20%20%20%20mo.stop(round2%20is%20NOT_YET)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_previous%20%3D%20load_staged(PREVIOUS_ROUND)%0A%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20_previous%20%3D%20None%0A%20%20%20%20_found%20%3D%20attempt(warnings_for%2C%20round2%2C%20_previous)%0A%20%20%20%20mo.stop(_found%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.1%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Round%202's%20warnings.**%20They%20are%20reported%20every%20time%20the%20pipeline%20runs%2C%20and%20they%20never%20stop%20it%3A%22)%2C%0A%20%20%20%20%20%20%20%20*(%5Blook(%22Warning%22%2C%20w)%20for%20w%20in%20_found%5D%20or%20%5Bmo.callout(mo.md(%22No%20warnings.%22)%2C%20kind%3D%22success%22)%5D)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Region%2004%20again%3A%20in%20the%20Get%20module%20you%20decided%20to%20report%20it%2C%20not%20to%20change%20the%20weights.%20A%20warning%20keeps%20reporting%20it%2C%20every%20round%2C%20until%20the%20weights%20are%20looked%20at.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Before%20changing%20a%20rule%2C%20look%20at%20the%20rows%0A%0A%20%20%20%20When%20a%20check%20fails%2C%20there%20are%20three%20possible%20answers%3A%20the%20**data**%20is%20wrong%2C%20an%20**earlier%20step**%20is%20wrong%2C%20or%20the%20**rule**%20is%20wrong.%20Changing%20the%20rule%20until%20the%20check%20passes%20is%20the%20one%20answer%20that%20is%20almost%20never%20right.%20Look%20at%20the%20failing%20rows%20first.%20Pick%20a%20rule%20from%20Round%202's%20report%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_failures)%3A%0A%20%20%20%20_checks%20%3D%20sorted(raw_failures%5B%22check%22%5D.astype(str).unique())%0A%20%20%20%20rule_to_look_at%20%3D%20mo.ui.dropdown(options%3D_checks%2C%20value%3D_checks%5B0%5D%20if%20_checks%20else%20None%2C%20label%3D%22Show%20the%20rows%20that%20break%3A%22)%0A%20%20%20%20rule_to_look_at%0A%20%20%20%20return%20(rule_to_look_at%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_failures%2C%20round2%2C%20rule_to_look_at)%3A%0A%20%20%20%20mo.stop(rule_to_look_at.value%20is%20None)%0A%20%20%20%20_rows%20%3D%20raw_failures.loc%5Braw_failures%5B%22check%22%5D.astype(str)%20%3D%3D%20rule_to_look_at.value%2C%20%22index%22%5D.dropna().astype(int).unique()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(_rows)%7D%20row(s)**%20of%20Round%202%20break%20*%7Brule_to_look_at.value%7D*%3A%22)%2C%0A%20%20%20%20%20%20%20%20round2.loc%5B_rows%5D.head(20)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Ask%20of%20each%3A%20is%20this%20value%20impossible%20(fix%20or%20remove%20it)%2C%20a%20known%20code%20for%20*not%20stated*%20(turn%20it%20into%20missing)%2C%20or%20a%20real%20case%20the%20rule%20did%20not%20foresee%20(then%2C%20and%20only%20then%2C%20change%20the%20rule%2C%20and%20write%20down%20why)%3F%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20A%20**failure**%20stops%20the%20pipeline%3B%20a%20**warning**%20reports%20and%20lets%20the%20data%20through.%20Use%20warnings%20for%20the%20unusual%2C%20failures%20for%20the%20impossible.%0A%20%20%20%20-%20A%20failing%20check%20is%20a%20question%20about%20the%20data.%20Look%20at%20the%20rows%20before%20changing%20any%20rule.%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20warning%20check%2C%20tolerance.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204%20%C2%B7%20Cleaning%20as%20code%2C%20part%201%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2060%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20to%20clean%20with%20code%20that%20runs%20again%20next%20quarter%2C%20not%20with%20edits%20in%20a%20spreadsheet%3A%20duplicates%2C%20and%20codes%20that%20mean%20*not%20stated*.%0A%20%20%20%20-%20**You%20build%3A**%20%60drop_duplicate_persons%60%2C%20%60blank_missing_codes%60%2C%20and%20your%20decision%20about%20duplicates.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Never%20fix%20data%20by%20editing%20the%20file%3A%20next%20quarter's%20file%20will%20have%20the%20same%20problems%2C%20and%20nobody%20will%20know%20what%20you%20changed.%20Fix%20it%20with%20**code**%20that%20runs%20again%20on%20every%20round%2C%20and%20records%20what%20it%20changed.%0A%0A%20%20%20%20%23%23%23%23%20Concept%3A%20duplicates%2C%20and%20the%20rule%20that%20says%20which%20row%20wins%0A%0A%20%20%20%20Some%20rows%20are%20the%20**same%20row%20twice**%20(copied%20by%20mistake)%3A%20one%20copy%20goes.%20Some%20are%20the%20**same%20person**%20with%20**different%20values**%3A%20a%20rule%20must%20say%20which%20row%20stays.%20Pick%20a%20rule%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20card_dup_rule%20%3D%20mo.ui.radio(options%3D%5B%22first%20in%20the%20file%22%2C%20%22fewer%20missing%20values%22%5D%2C%20value%3D%22first%20in%20the%20file%22%2C%20label%3D%22rule%3A%22%2C%20inline%3DTrue)%0A%20%20%20%20card_dup_rule%0A%20%20%20%20return%20(card_dup_rule%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_dup_rule)%3A%0A%20%20%20%20explain.duplicate_demo(card_dup_rule.value)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.1.%20Drop%20duplicate%20persons%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20person%20counted%20twice%20is%20counted%20twice%20in%20every%20total.%20The%20rule%20for%20which%20copy%20stays%20must%20be%20explicit%2C%20so%20it%20can%20be%20explained%20and%20repeated.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20round%20and%20a%20rule%20(%60%22first%22%60%20or%20%60%22most-complete%22%60)%20%E2%86%92%20the%20round%20with%20each%20person%20(%60hhid%60%2C%20%60member_no%60)%20once.%0A%0A%20%20%20%201.%20Drop%20**exact**%20duplicates%20(identical%20rows)%3A%20%60table.drop_duplicates()%60.%0A%20%20%20%202.%20Then%2C%20for%20people%20still%20listed%20more%20than%20once%3A%0A%20%20%20%20%20%20%20-%20rule%20%60%22first%22%60%3A%20keep%20the%20first%3A%20%60table.drop_duplicates(subset%3D%5B%22hhid%22%2C%20%22member_no%22%5D%2C%20keep%3D%22first%22)%60%3B%0A%20%20%20%20%20%20%20-%20rule%20%60%22most-complete%22%60%3A%20keep%20the%20row%20with%20the%20fewest%20missing%20values%3B%20if%20equal%2C%20the%20first.%20One%20way%3A%20add%20a%20column%20with%20each%20row's%20number%20of%20missing%20values%20(%60table.isna().sum(axis%3D1)%60)%2C%20sort%20by%20it%20with%20%60kind%3D%22stable%22%60%20(so%20equal%20rows%20keep%20their%20order)%2C%20drop%20duplicates%20keeping%20the%20first%2C%20drop%20the%20helper%20column%2C%20and%20put%20the%20rows%20back%20in%20their%20original%20order%20with%20%60.sort_index()%60.%0A%20%20%20%203.%20Return%20the%20table%20with%20%60.reset_index(drop%3DTrue)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20drop_duplicate_persons(table%3A%20pd.DataFrame%2C%20rule%3A%20Literal%5B%22first%22%2C%20%22most-complete%22%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20person%20once%3A%20exact%20duplicates%20dropped%2C%20then%20the%20rule%20decides%20between%20different%20rows%20for%20the%20same%20person.%22%22%22%0A%20%20%20%20%23%20Exercise%204.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20drop%20exact%20duplicates%0A%20%20%20%20%23%20Step%202%3A%20if%20rule%20%3D%3D%20%22first%22%3A%20keep%20the%20first%20row%20per%20person%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20if%20rule%20%3D%3D%20%22most-complete%22%3A%20keep%20the%20row%20with%20the%20fewest%20missing%20values%20(see%20the%20task%20above)%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20table%20with%20.reset_index(drop%3DTrue)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_table%20%3D%20explain.DUPLICATE_TABLE%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.1%22%2C%20drop_duplicate_persons%2C%20%5B(%22the%20five-row%20example%2C%20rule%20'first'%22%2C%20(_table%2C%20%22first%22))%2C%20(%22the%20five-row%20example%2C%20rule%20'most-complete'%22%2C%20(_table%2C%20%22most-complete%22))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20t%3A%20t.astype(str).values.tolist()%2C%0A%20%20%20%20%20%20%20%20hint%3D%22With%20'first'%2C%20H2%20keeps%20age%2052%20(no%20hours)%3B%20with%20'most-complete'%2C%20age%2051%20(with%20hours).%20Three%20rows%20remain%20either%20way.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20five-row%20example%20from%20the%20explainer%2C%20with%20the%20rule%20%22most-complete%22.%0A%20%20%20%20course.try_it(drop_duplicate_persons%2C%20explain.DUPLICATE_TABLE%2C%20%22most-complete%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Four%20rows%20remain%20%7C%20The%20exact%20duplicates%20were%20not%20dropped%20first.%20%7C%20step%201%20%7C%0A%20%20%20%20%7C%20Both%20rules%20give%20the%20same%20result%20%7C%20The%20rule%20is%20not%20used.%20%7C%20%60if%20rule%20%3D%3D%20%22first%22%3A%20...%20else%3A%20...%60%20%7C%0A%20%20%20%20%7C%20The%20right%20rows%2C%20in%20another%20order%20%7C%20The%20sort%20for%20%22most-complete%22%20was%20not%20undone.%20%7C%20%60.sort_index()%60%20before%20returning%20%7C%0A%20%20%20%20%7C%20A%20helper%20column%20is%20still%20there%20%7C%20The%20count%20of%20missing%20values%20was%20kept.%20%7C%20%60.drop(columns%3D%22_missing%22)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Decide%3A%20which%20copy%20wins%20in%20Round%202%3F%0A%0A%20%20%20%20Round%202%20has%20the%20same%20person%20twice%20with%20different%20values.%20Choose%20the%20rule%20the%20pipeline%20uses%20for%20this%20round%2C%20and%20say%20why.%20Your%20decision%20is%20saved%20beside%20the%20round's%20configuration%2C%20in%20%60config%2Frounds%2F2024Q1.check.toml%60%2C%20and%20%60run.py%60%20follows%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.class_definition%0Aclass%20CheckConfig(BaseModel)%3A%0A%20%20%20%20%22%22%22The%20Check%20stage's%20decisions%20for%20one%20round.%20Saved%20as%20config%2Frounds%2F%3Cround_id%3E.check.toml.%22%22%22%0A%0A%20%20%20%20model_config%20%3D%20ConfigDict(extra%3D%22forbid%22)%0A%0A%20%20%20%20round_id%3A%20str%20%3D%20Field(pattern%3Dr%22%5E%5Cd%7B4%7DQ%5B1-4%5D%24%22)%0A%20%20%20%20duplicate_rule%3A%20Literal%5B%22first%22%2C%20%22most-complete%22%5D%0A%20%20%20%20duplicate_reason%3A%20str%20%3D%20Field(min_length%3D1)%0A%0A%0A%40app.function%0Adef%20check_config_path(round_id%3A%20str)%20-%3E%20Path%3A%0A%20%20%20%20return%20PATHS.config%20%2F%20f%22%7Bround_id%7D.check.toml%22%0A%0A%0A%40app.function%0Adef%20load_check_config(round_id%3A%20str)%20-%3E%20CheckConfig%20%7C%20None%3A%0A%20%20%20%20%22%22%22The%20saved%20decisions%20for%20a%20round%2C%20or%20None%20if%20none%20were%20saved.%22%22%22%0A%20%20%20%20path%20%3D%20check_config_path(round_id)%0A%20%20%20%20return%20CheckConfig.model_validate(tomllib.loads(path.read_text(encoding%3D%22utf-8%22)))%20if%20path.exists()%20else%20None%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20dup_choice%20%3D%20mo.ui.radio(options%3D%7B%22Keep%20the%20first%20copy%20in%20the%20file%22%3A%20%22first%22%2C%20%22Keep%20the%20copy%20with%20the%20fewest%20missing%20values%22%3A%20%22most-complete%22%7D%2C%20label%3D%22**Decide.**%20Which%20copy%20of%20a%20person%20stays%3F%22)%0A%20%20%20%20dup_reason%20%3D%20mo.ui.text_area(label%3D%22Why%3F%20One%20or%20two%20sentences.%22%2C%20full_width%3DTrue)%0A%20%20%20%20dup_button%20%3D%20mo.ui.run_button(label%3Df%22Save%20this%20decision%20for%20round%20%7BROUND_ID%7D%22)%0A%20%20%20%20mo.vstack(%5Bdup_choice%2C%20dup_reason%2C%20dup_button%5D)%0A%20%20%20%20return%20dup_button%2C%20dup_choice%2C%20dup_reason%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(dup_button%2C%20dup_choice%2C%20dup_reason)%3A%0A%20%20%20%20mo.stop(not%20dup_button.value)%0A%20%20%20%20if%20dup_choice.value%20is%20None%20or%20not%20dup_reason.value.strip()%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(%22Choose%20a%20rule%20**and**%20write%20a%20reason%20first%3A%20a%20decision%20without%20a%20reason%20cannot%20be%20reviewed%20later.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_config%20%3D%20CheckConfig(round_id%3DROUND_ID%2C%20duplicate_rule%3Ddup_choice.value%2C%20duplicate_reason%3Ddup_reason.value.strip())%0A%20%20%20%20%20%20%20%20check_config_path(ROUND_ID).write_text(tomli_w.dumps(_config.model_dump())%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Saved%20in%20%60config%2Frounds%2F%7BROUND_ID%7D.check.toml%60.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20Concept%3A%20codes%20that%20mean%20%22not%20stated%22%0A%0A%20%20%20%20A%20survey%20writes%20**999**%20for%20an%20age%20or%20hours%20that%20were%20not%20given%2C%20and%20**9**%20for%20a%20sex%20that%20was%20not%20stated.%20They%20are%20not%20values%3A%20they%20are%20**codes%20for%20missing**%2C%20and%20they%20must%20become%20missing%20before%20any%20total%20is%20computed.%20The%20old%20system%20lists%20them%20in%20its%20table%20%60missing_value_codes%60.%20One%20trap%20waits%3A%20a%20number%20column%20with%20a%20missing%20value%20holds%20**decimals**%2C%20so%20999%20is%20stored%20as%20%60999.0%60.%20Compare%20as%20text%20or%20as%20a%20number%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20card_text_compare%20%3D%20mo.ui.switch(label%3D%22compare%20as%20text%22%2C%20value%3DTrue)%0A%20%20%20%20card_text_compare%0A%20%20%20%20return%20(card_text_compare%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(card_text_compare)%3A%0A%20%20%20%20explain.missing_code_demo(card_text_compare.value)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20missing_value_codes()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20old%20system's%20list%20of%20codes%20that%20mean%20%22not%20stated%22%3A%20variable%2C%20code%2C%20meaning.%22%22%22%0A%20%20%20%20with%20closing(sqlite3.connect(PATHS.reference%20%2F%20%22legacy_system.sqlite%22))%20as%20con%3A%0A%20%20%20%20%20%20%20%20return%20pd.read_sql_query(%22SELECT%20variable%2C%20code%2C%20meaning%20FROM%20missing_value_codes%20ORDER%20BY%20variable%2C%20code%22%2C%20con)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.vstack(%5Bmo.md(%22The%20old%20system's%20%60missing_value_codes%60%3A%22)%2C%20missing_value_codes()%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.2.%20Turn%20%22not%20stated%22%20codes%20into%20missing%20values%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20age%20of%20999%20left%20in%20the%20data%20raises%20every%20average%3B%20a%20missing%20age%20does%20not.%20Taking%20the%20codes%20from%20the%20old%20system's%20table%2C%20not%20typing%20them%20into%20your%20code%2C%20means%20a%20new%20code%20added%20there%20is%20used%20here%20too.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20round%20and%20the%20codes%20table%20(%60variable%60%2C%20%60code%60%2C%20%60meaning%60)%20%E2%86%92%20the%20round%20with%20each%20listed%20code%20in%20its%20column%20replaced%20by%20a%20missing%20value.%0A%0A%20%20%20%20For%20each%20row%20of%20the%20codes%20table%20(%60for%20row%20in%20codes.itertuples()%3A%60)%2C%20if%20the%20round%20has%20that%20column%3A%0A%0A%20%20%20%20-%20if%20the%20column%20holds%20**numbers**%20(%60pd.api.types.is_numeric_dtype(table%5Brow.variable%5D)%60)%2C%20compare%20as%20a%20number%3A%20%60table%5Brow.variable%5D%20%3D%3D%20row.code%60%3B%0A%20%20%20%20-%20otherwise%20it%20holds%20**text%20codes**%3A%20compare%20as%20text%2C%20%60table%5Brow.variable%5D%20%3D%3D%20str(row.code)%60%3B%0A%20%20%20%20-%20where%20it%20matches%2C%20set%20a%20missing%20value%3A%20%60table.loc%5Bmatches%2C%20row.variable%5D%20%3D%20None%60.%0A%0A%20%20%20%20Work%20on%20a%20copy%20(%60table%20%3D%20table.copy()%60%20first)%2C%20so%20the%20table%20you%20were%20given%20does%20not%20change%2C%20and%20return%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20blank_missing_codes(table%3A%20pd.DataFrame%2C%20codes%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20table%20with%20every%20%22not%20stated%22%20code%20(from%20the%20codes%20table)%20replaced%20by%20a%20missing%20value.%22%22%22%0A%20%20%20%20%23%20Exercise%204.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20table%20%3D%20table.copy()%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20row%20of%20codes%20whose%20variable%20is%20a%20column%20of%20table%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20%20%20compare%20as%20a%20number%20for%20number%20columns%2C%20as%20text%20for%20the%20others%2C%20and%20set%20the%20matches%20to%20None%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_table%20%3D%20pd.DataFrame(%7B%22age%22%3A%20%5B34.0%2C%20999.0%2C%2020.0%5D%2C%20%22hours%22%3A%20%5B40.0%2C%20None%2C%20999.0%5D%2C%20%22sex%22%3A%20%5B%221%22%2C%20%229%22%2C%20%222%22%5D%2C%20%22region%22%3A%20%5B%2201%22%2C%20%22999%22%2C%20%2202%22%5D%7D)%0A%20%20%20%20_codes%20%3D%20pd.DataFrame(%7B%22variable%22%3A%20%5B%22age%22%2C%20%22hours%22%2C%20%22sex%22%2C%20%22employed%22%5D%2C%20%22code%22%3A%20%5B999%2C%20999%2C%209%2C%209%5D%2C%20%22meaning%22%3A%20%5B%22not%20stated%22%5D%20*%204%7D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.2%22%2C%20blank_missing_codes%2C%20%5B(%22ages%2C%20hours%20and%20sex%20with%20codes%2C%20and%20a%20region%20that%20only%20looks%20like%20one%22%2C%20(_table%2C%20_codes))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20t%3A%20t.astype(object).where(t.notna()%2C%20None).values.tolist()%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Only%20columns%20listed%20in%20the%20codes%20table%20change%3A%20the%20region%20'999'%20stays.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(round2)%3A%0A%20%20%20%20%23%20Try%20it%3A%20Round%202's%20rows%20with%20an%20age%20of%20999%2C%20before%20and%20after.%0A%20%20%20%20_before%20%3D%20round2%5Bround2%5B%22age%22%5D%20%3D%3D%20999%5D%20if%20round2%20is%20not%20NOT_YET%20else%20NOT_YET%0A%20%20%20%20course.try_it(blank_missing_codes%2C%20_before%2C%20missing_value_codes())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20999s%20are%20still%20there%20%7C%20The%20number%20column%20was%20compared%20as%20text%3A%20%60'999.0'%60%20is%20not%20%60'999'%60.%20%7C%20compare%20numbers%20as%20numbers%20%7C%0A%20%20%20%20%7C%20Sex%209%20is%20still%20there%20%7C%20The%20text%20column%20was%20compared%20with%20the%20number%209.%20%7C%20%60str(row.code)%60%20for%20text%20columns%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'employed'*%20%7C%20A%20variable%20in%20the%20codes%20table%20is%20not%20a%20column%20of%20this%20table.%20%7C%20skip%20it%3A%20%60if%20row.variable%20not%20in%20table.columns%3A%20continue%60%20%7C%0A%20%20%20%20%7C%20The%20region%20999%20became%20missing%20%7C%20A%20column%20not%20in%20the%20codes%20table%20was%20changed.%20%7C%20change%20only%20%60row.variable%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%223.4%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205%20%C2%B7%20Cleaning%20as%20code%2C%20part%202%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**In%20this%20section**%20(about%2040%20minutes)%0A%0A%20%20%20%20-%20**You%20learn%3A**%20to%20recode%20through%20a%20lookup%20table%2C%20and%20to%20report%20every%20change%20the%20cleaning%20made.%0A%20%20%20%20-%20**You%20build%3A**%20%60add_labels%60%2C%20%60count_changed_values%60%2C%20and%20a%20clean%20Round%202%20that%20meets%20the%20contract.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.1.%20Labels%20from%20a%20lookup%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20codes%20are%20for%20joining%3B%20labels%20are%20for%20people.%20Taking%20labels%20from%20the%20lookup%20table%2C%20by%20a%20join%2C%20means%20nobody%20types%20%22Female%22%20by%20hand%2C%20and%20a%20code%20with%20no%20label%20stands%20out.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20round%2C%20a%20lookup%20table%20(%60code%60%20as%20a%20whole%20number%2C%20%60label%60)%20and%20a%20column%20name%20%E2%86%92%20the%20round%20with%20a%20new%20column%20%60%3Ccolumn%3E_label%60.%0A%0A%20%20%20%20The%20old%20system%20stores%20codes%20as%20**numbers**%20(%601%60)%2C%20the%20round%20as%20**text**%20(%60%221%22%60)%3A%20a%20join%20between%20them%20matches%20nothing%20(the%20Get%20module's%20lesson).%20Make%20the%20lookup's%20code%20text%20first%3A%0A%0A%20%20%20%201.%20%60lookup%20%3D%20lookup.assign(code%3Dlookup%5B%22code%22%5D.astype(str))%60%2C%20and%20rename%20%60code%60%20to%20the%20column's%20name%20and%20%60label%60%20to%20%60%3Ccolumn%3E_label%60%3B%0A%20%20%20%202.%20left-join%20the%20round%20to%20it%20on%20the%20column%2C%20with%20%60validate%3D%22many_to_one%22%60%3B%0A%20%20%20%203.%20return%20the%20joined%20table.%20The%20number%20of%20rows%20must%20not%20change.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20add_labels(table%3A%20pd.DataFrame%2C%20lookup%3A%20pd.DataFrame%2C%20column%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20table%20with%20a%20%3Ccolumn%3E_label%20column%2C%20from%20a%20lookup%20table%20of%20codes%20and%20labels.%22%22%22%0A%20%20%20%20%23%20Exercise%205.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20lookup's%20code%20as%20text%2C%20renamed%20to%20%60column%60%3B%20its%20label%20renamed%20to%20f%22%7Bcolumn%7D_label%22%0A%20%20%20%20%23%20Step%202%3A%20left-join%20table%20to%20it%20on%20%60column%60%20(validate%3D%22many_to_one%22)%2C%20and%20return%20the%20result%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.1%22)%0A%0A%0A%40app.function%0Adef%20lookups()%20-%3E%20dict%5Bstr%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22The%20old%20system's%20lookup%20tables%20for%20sex%20and%20employment%20(code%2C%20label).%22%22%22%0A%20%20%20%20with%20closing(sqlite3.connect(PATHS.reference%20%2F%20%22legacy_system.sqlite%22))%20as%20con%3A%0A%20%20%20%20%20%20%20%20return%20%7B%22sex%22%3A%20pd.read_sql_query(%22SELECT%20code%2C%20label%20FROM%20lookup_sex%22%2C%20con)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22employed%22%3A%20pd.read_sql_query(%22SELECT%20code%2C%20label%20FROM%20lookup_employment%22%2C%20con)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_table%20%3D%20pd.DataFrame(%7B%22hhid%22%3A%20%5B%22H1%22%2C%20%22H2%22%2C%20%22H3%22%2C%20%22H4%22%5D%2C%20%22sex%22%3A%20%5B%221%22%2C%20%222%22%2C%20None%2C%20%221%22%5D%7D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.1%22%2C%20add_labels%2C%20%5B(%22four%20people%2C%20with%20the%20old%20system's%20sex%20lookup%22%2C%20(_table%2C%20lookups()%5B%22sex%22%5D%2C%20%22sex%22))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20t%3A%20t.astype(object).where(t.notna()%2C%20None).values.tolist()%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Expect%20a%20column%20sex_label%20with%20Male%2C%20Female%2C%20nothing%20(missing%20sex)%2C%20Male.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20four%20people%20and%20the%20old%20system's%20sex%20lookup.%0A%20%20%20%20course.try_it(add_labels%2C%20pd.DataFrame(%7B%22hhid%22%3A%20%5B%22H1%22%2C%20%22H2%22%2C%20%22H3%22%5D%2C%20%22sex%22%3A%20%5B%221%22%2C%20%222%22%2C%20None%5D%7D)%2C%20lookups()%5B%22sex%22%5D%2C%20%22sex%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Every%20label%20is%20missing%20%7C%20The%20lookup's%20codes%20are%20numbers%2C%20the%20round's%20are%20text.%20%7C%20%60.astype(str)%60%20on%20the%20lookup's%20%60code%60%20%7C%0A%20%20%20%20%7C%20*KeyError*%20in%20the%20merge%20%7C%20The%20lookup's%20%60code%60%20was%20not%20renamed%20to%20the%20column's%20name.%20%7C%20%60rename(columns%3D%7B%22code%22%3A%20column%2C%20...%7D)%60%20%7C%0A%20%20%20%20%7C%20The%20label%20column%20has%20another%20name%20%7C%20It%20must%20be%20%60f%22%7Bcolumn%7D_label%22%60.%20%7C%20the%20rename%20of%20%60label%60%20%7C%0A%20%20%20%20%7C%20More%20rows%20than%20before%20%7C%20The%20lookup%20lists%20a%20code%20twice.%20%7C%20%60validate%3D%22many_to_one%22%60%20will%20say%20so%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.2.%20Count%20what%20a%20step%20changed%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20cleaning%20step%20that%20changes%205%20values%20is%20a%20fix%3B%20one%20that%20changes%205%2C000%20is%20probably%20a%20bug.%20The%20cleaning%20report%20counts%20every%20step's%20changes%2C%20so%20anyone%20can%20see%20what%20cleaning%20did.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20column%20before%20a%20step%20and%20the%20same%20column%20after%20it%20(same%20rows%2C%20same%20order)%20%E2%86%92%20how%20many%20values%20differ%2C%20counting%20%22missing%20before%20and%20missing%20after%22%20as%20**no**%20change.%0A%0A%20%20%20%20%60before.ne(after)%60%20is%20%60True%60%20where%20the%20values%20differ%2C%20but%20also%20where%20both%20are%20missing%20(to%20pandas%2C%20missing%20is%20not%20equal%20to%20missing).%20Remove%20those%3A%20%60%26%20~(before.isna()%20%26%20after.isna())%60.%20Count%20the%20%60True%60s%20with%20%60.sum()%60%2C%20and%20return%20a%20whole%20number%20with%20%60int(...)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20count_changed_values(before%3A%20pd.Series%2C%20after%3A%20pd.Series)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20values%20differ%20between%20two%20versions%20of%20a%20column%3B%20missing%20in%20both%20counts%20as%20unchanged.%22%22%22%0A%20%20%20%20%23%20Exercise%205.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20One%20line%3A%20values%20that%20differ%2C%20but%20not%20where%20both%20are%20missing%3B%20count%20them%3B%20return%20an%20int%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_cases%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22ages%3A%20one%20999%20became%20missing%22%2C%20(pd.Series(%5B34.0%2C%20999.0%2C%20None%2C%2020.0%5D)%2C%20pd.Series(%5B34.0%2C%20None%2C%20None%2C%2020.0%5D)))%2C%0A%20%20%20%20%20%20%20%20(%22sex%20codes%3A%20nothing%20changed%22%2C%20(pd.Series(%5B%221%22%2C%20None%2C%20%222%22%5D)%2C%20pd.Series(%5B%221%22%2C%20None%2C%20%222%22%5D)))%2C%0A%20%20%20%20%20%20%20%20(%22regions%3A%20two%20corrected%22%2C%20(pd.Series(%5B%2201%22%2C%20%2299%22%2C%20%2205%22%5D)%2C%20pd.Series(%5B%2201%22%2C%20%2202%22%2C%20%2203%22%5D)))%2C%0A%20%20%20%20%5D%0A%20%20%20%20check_cases(%22Exercise%205.2%22%2C%20count_changed_values%2C%20_cases%2C%20what%3D%22pairs%20of%20columns%22%2C%20hint%3D%22Missing%20before%20and%20missing%20after%20is%20not%20a%20change.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20one%20value%20changed%2C%20one%20missing%20in%20both.%0A%20%20%20%20course.try_it(count_changed_values%2C%20pd.Series(%5B34.0%2C%20999.0%2C%20None%5D)%2C%20pd.Series(%5B34.0%2C%20None%2C%20None%5D))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20One%20too%20many%20for%20every%20missing%20value%20%7C%20Missing%20in%20both%20is%20counted%20as%20a%20change.%20%7C%20%60%26%20~(before.isna()%20%26%20after.isna())%60%20%7C%0A%20%20%20%20%7C%20The%20result%20is%20%60True%60%20or%20a%20Series%20%7C%20The%20count%20is%20missing.%20%7C%20%60.sum()%60%2C%20then%20%60int(...)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20whole%20cleaning%2C%20as%20one%20function%0A%0A%20%20%20%20%60clean_round%60%20(below%2C%20already%20written)%20runs%20every%20step%20in%20order%2C%20using%20**your**%20functions%20and%20**your**%20decision%2C%20and%20records%20each%20step%20in%20the%20**cleaning%20report**%3A%20rows%20before%20and%20after%2C%20values%20changed%2C%20and%20why.%20Read%20its%20comments%3A%20each%20step%20is%20one%20of%20this%20module's%20lessons.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20clean_round(table%3A%20pd.DataFrame%2C%20config%3A%20CheckConfig%20%7C%20None)%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Clean%20a%20staged%20round.%20Returns%20the%20clean%20round%20and%20the%20cleaning%20report%20(one%20row%20per%20step).%22%22%22%0A%20%20%20%20report%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20step(name%3A%20str%2C%20before%3A%20pd.DataFrame%2C%20after%3A%20pd.DataFrame%2C%20why%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20changed%20%3D%20sum(count_changed_values(before%5Bc%5D%2C%20after%5Bc%5D)%20for%20c%20in%20before.columns%20if%20c%20in%20after.columns)%20if%20len(before)%20%3D%3D%20len(after)%20else%200%0A%20%20%20%20%20%20%20%20report.append(%7B%22step%22%3A%20name%2C%20%22rows%20before%22%3A%20len(before)%2C%20%22rows%20after%22%3A%20len(after)%2C%20%22values%20changed%22%3A%20changed%2C%20%22why%22%3A%20why%7D)%0A%20%20%20%20%20%20%20%20return%20after%0A%0A%20%20%20%20%23%201.%20Each%20person%20once.%20A%20rule%20is%20needed%20only%20when%20the%20same%20person%20appears%20with%20different%20values.%0A%20%20%20%20keyed%20%3D%20table.drop_duplicates()%0A%20%20%20%20if%20keyed.duplicated(%5B%22hhid%22%2C%20%22member_no%22%5D).any()%3A%0A%20%20%20%20%20%20%20%20if%20config%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20RuntimeError(f%22Round%20%7Btable%5B'round'%5D.iloc%5B0%5D%7D%20lists%20the%20same%20person%20twice%20with%20different%20values%2C%20and%20no%20rule%20was%20chosen.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Decide%20in%2020_check_round.py%2C%20section%204%20(Decide%3A%20which%20copy%20wins).%22)%0A%20%20%20%20%20%20%20%20rule%2C%20why%20%3D%20config.duplicate_rule%2C%20config.duplicate_reason%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20rule%2C%20why%20%3D%20%22first%22%2C%20%22no%20person%20is%20listed%20twice%20with%20different%20values%22%0A%20%20%20%20table%20%3D%20step(%22drop%20duplicate%20persons%22%2C%20table%2C%20drop_duplicate_persons(table%2C%20rule)%2C%20f%22exact%20copies%20dropped%3B%20rule%20'%7Brule%7D'%3A%20%7Bwhy%7D%22)%0A%20%20%20%20%23%202.%20Codes%20that%20mean%20%22not%20stated%22%20become%20missing%20(from%20the%20old%20system's%20list)%0A%20%20%20%20table%20%3D%20step(%22not-stated%20codes%20to%20missing%22%2C%20table%2C%20blank_missing_codes(table%2C%20missing_value_codes())%2C%20%22999%20and%209%20mean%20not%20stated%2C%20not%20a%20value%22)%0A%20%20%20%20%23%203.%20Codes%20that%20are%20in%20no%20lookup%20are%20not%20valid%3A%20they%20become%20missing%20too%2C%20and%20are%20counted%20here%0A%20%20%20%20after%20%3D%20table.copy()%0A%20%20%20%20for%20column%2C%20lookup%20in%20lookups().items()%3A%0A%20%20%20%20%20%20%20%20valid%20%3D%20set(lookup%5B%22code%22%5D.astype(str))%0A%20%20%20%20%20%20%20%20after.loc%5Bafter%5Bcolumn%5D.notna()%20%26%20~after%5Bcolumn%5D.isin(valid)%2C%20column%5D%20%3D%20None%0A%20%20%20%20table%20%3D%20step(%22invalid%20codes%20to%20missing%22%2C%20table%2C%20after%2C%20%22a%20code%20in%20no%20lookup%20table%20cannot%20be%20interpreted%22)%0A%20%20%20%20%23%204.%20The%20area%20code%20is%20the%20authority%20on%20the%20region%3A%20where%20they%20disagree%2C%20the%20region%20follows%20the%20area%20code%0A%20%20%20%20table%20%3D%20step(%22region%20from%20the%20area%20code%22%2C%20table%2C%20table.assign(region%3Dtable%5B%22area_code%22%5D.str%5B%3A2%5D)%2C%20%22the%20region%20is%20part%20of%20the%20area%20code%22)%0A%20%20%20%20%23%205.%20Hours%20are%20only%20asked%20of%20employed%20people%0A%20%20%20%20table%20%3D%20step(%22hours%20only%20for%20the%20employed%22%2C%20table%2C%20table.assign(hours%3Dtable%5B%22hours%22%5D.where(table%5B%22employed%22%5D.eq(%221%22)))%2C%20%22the%20questionnaire%20asks%20hours%20only%20of%20the%20employed%22)%0A%20%20%20%20%23%206.%20A%20weight%20of%200%20or%20less%20cannot%20stand%20for%20anyone%3A%20the%20record%20is%20dropped%2C%20and%20counted%20here%0A%20%20%20%20table%20%3D%20step(%22drop%20records%20without%20a%20weight%22%2C%20table%2C%20table%5Btable%5B%22weight%22%5D%20%3E%200%5D.reset_index(drop%3DTrue)%2C%20%22a%20weight%20of%200%20stands%20for%20nobody%22)%0A%20%20%20%20%23%207.%20Labels%20for%20people%20to%20read%0A%20%20%20%20for%20column%2C%20lookup%20in%20lookups().items()%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20add_labels(table%2C%20lookup%2C%20column)%0A%20%20%20%20report.append(%7B%22step%22%3A%20%22labels%20from%20the%20lookups%22%2C%20%22rows%20before%22%3A%20len(table)%2C%20%22rows%20after%22%3A%20len(table)%2C%20%22values%20changed%22%3A%200%2C%20%22why%22%3A%20%22sex_label%20and%20employed_label%20added%22%7D)%0A%20%20%20%20return%20table%2C%20pd.DataFrame(report)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(round2)%3A%0A%20%20%20%20mo.stop(round2%20is%20NOT_YET)%0A%20%20%20%20clean_button%20%3D%20mo.ui.run_button(label%3Df%22Clean%20round%20%7BROUND_ID%7D%20with%20your%20functions%20and%20your%20decision%22)%0A%20%20%20%20clean_button%0A%20%20%20%20return%20(clean_button%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(clean_button%2C%20round2)%3A%0A%20%20%20%20mo.stop(not%20clean_button.value)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20clean2%2C%20cleaning_report%20%3D%20clean_round(round2%2C%20load_check_config(ROUND_ID))%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(%5Bmo.md(f%22**The%20cleaning%20report%20for%20round%20%7BROUND_ID%7D%3A**%20%7Blen(round2)%3A%2C%7D%20rows%20in%2C%20%7Blen(clean2)%3A%2C%7D%20rows%20out.%22)%2C%20cleaning_report%5D)%0A%20%20%20%20except%20NotImplementedError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20clean2%2C%20_out%20%3D%20NOT_YET%2C%20waiting_for(str(_error))%0A%20%20%20%20except%20RuntimeError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20clean2%2C%20_out%20%3D%20NOT_YET%2C%20mo.callout(mo.md(str(_error))%2C%20kind%3D%22warn%22)%0A%20%20%20%20_out%0A%20%20%20%20return%20(clean2%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(clean2)%3A%0A%20%20%20%20mo.stop(clean2%20is%20NOT_YET)%0A%20%20%20%20_failures%20%3D%20validate_round(clean2%2C%20survey_schema(code_list_codes(round_edition(ROUND_ID))))%0A%20%20%20%20if%20_failures.empty%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22**The%20clean%20round%20%7BROUND_ID%7D%20meets%20the%20contract%3A%20no%20failures.**%20Every%20rule%20holds%2C%20for%20every%20row%2C%20because%20of%20code%20that%20will%20run%20again%20on%20the%20next%20round.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(%5Blook(%22The%20clean%20round%20still%20breaks%20the%20contract%22%2C%20f%22%7Blen(_failures)%7D%20failure(s).%20Look%20at%20them%20before%20going%20on%3A%22)%2C%20_failures.head(20)%5D)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Run%20it%20from%20the%20terminal%0A%0A%20%20%20%20%60run.py%60%20now%20runs%20every%20stage%20you%20have%3A%20**Get**%2C%20then%20**Check**%20(the%20contract%20on%20the%20staged%20round%2C%20the%20warnings%2C%20the%20cleaning%2C%20and%20the%20contract%20again%20on%20the%20clean%20round)%2C%20then%20**Store**%20once%20this%20afternoon's%20notebook%20is%20done.%20In%20the%20course%20folder%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20uv%20run%20python%20run.py%20--rounds%202023Q4%2C2024Q1%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20If%20the%20contract%20fails%2C%20it%20prints%20the%20short%20report%20from%20your%20%60failure_summary%60%20and%20stops%3A%20nothing%20wrong%20reaches%20the%20database.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**Recap**%0A%0A%20%20%20%20-%20Clean%20with%20code%20that%20runs%20again%20on%20every%20round%2C%20never%20by%20editing%20the%20file.%0A%20%20%20%20-%20Exact%20copies%20go%3B%20for%20different%20rows%20of%20the%20same%20person%2C%20a%20written%20rule%20decides.%0A%20%20%20%20-%20Codes%20that%20mean%20*not%20stated*%20become%20missing%2C%20compared%20by%20their%20type%3A%20numbers%20as%20numbers%2C%20text%20codes%20as%20text.%0A%20%20%20%20-%20Every%20step%20is%20counted%20in%20the%20cleaning%20report%2C%20so%20a%20step%20that%20changes%20too%20much%20stands%20out.%0A%0A%20%20%20%20Key%20terms%20(%5Bglossary%5D(%2F%3Ffile%3D91_glossary.py))%3A%20cleaning%20as%20code%2C%20duplicate%20rule%2C%20missing-value%20code%2C%20lookup%20table%2C%20cleaning%20report.%0A%0A%20%20%20%20**Next%3A**%20the%20debugging%20drill%20(%5B21_debugging_drill.py%5D(%2F%3Ffile%3D21_debugging_drill.py))%2C%20then%20storing%20the%20clean%20rounds%20(%5B22_store_round.py%5D(%2F%3Ffile%3D22_store_round.py)).%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%223.5%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
f85831bf915e83520019eed7c2a35cce