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%22Match%20the%20keys%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20shutil%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20CHILD_PREFIX%2C%20NOT_YET%2C%20PATHS%2C%20UNIT_KEY%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20KEY%20%3D%20UNIT_KEY%20%20%23%20one%20fishing%20unit%2C%20across%20regions%3A%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%0A%20%20%20%20HOUSEHOLD%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20one%20household%2C%20across%20regions%0A%20%20%20%20PARTS%20%3D%20%5B%22operator_line%22%2C%20%22unit_no%22%5D%20%20%23%20the%20key's%20two%20parts%20below%20the%20household%0A%20%20%20%20OPERATOR%20%3D%20HOUSEHOLD%20%2B%20%5B%22operator_line%22%5D%20%20%23%20one%20operator%3A%20a%20member%20who%20runs%20two%20units%20has%20one%20roster%20line%0A%20%20%20%20CATCH%20%3D%20%22catch%22%20%20%23%20the%20catch%20table%3B%20its%20key%20parts%20carry%20the%20prefix%20catch_%0A%20%20%20%20AUDIT%20%3D%20%22key_corrected%22%20%20%23%20the%20audit%20flag%20the%20write-back%20adds%3A%201%20on%20every%20row%20whose%20key%20it%20changed%0A%20%20%20%20SCRATCH%20%3D%20PATHS.root%20%2F%20%22scratch%22%20%20%23%20where%20this%20module%20writes%20its%20copies%3B%20the%20files%20in%20data%2F%20are%20only%20ever%20read%0A%20%20%20%20SCRATCH.mkdir(exist_ok%3DTrue)%0A%0A%20%20%20%20%23%20The%20four%20rules%20of%20section%204%2C%20by%20the%20reason%20each%20gives%0A%20%20%20%20PAIRED%20%3D%20%22only%20stray%20key%2C%20only%20unit%20without%20catch%22%0A%20%20%20%20ONE_NEAR%20%3D%20%22one%20part%20from%20one%20unit%22%0A%20%20%20%20SEVERAL_NEAR%20%3D%20%22one%20part%20from%20two%20or%20more%20units%22%0A%20%20%20%20NONE_NEAR%20%3D%20%22both%20parts%20differ%22%0A%0A%20%20%20%20%23%20The%20survey%20as%20module%202%20leaves%20it%3A%20every%20region's%20rows%2C%20region_code%20on%20each%2C%20removed%20households%20and%20voided%20rows%20left%20out%0A%20%20%20%20units%20%3D%20course.units()%20%20%23%20one%20row%20per%20fishing%20unit%0A%20%20%20%20catch%20%3D%20course.table(CATCH)%20%20%23%20one%20row%20per%20species%20a%20unit%20caught%2C%20as%20stored%3A%20key%20parts%20catch_operator_line%2C%20catch_unit_no%0A%20%20%20%20members%20%3D%20course.members()%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%20Match%20the%20keys%0A%0A%20%20%20%20Module%202%20brought%20every%20region's%20tables%20into%20one%20place.%20This%20module%20checks%20that%20the%20tables%20agree%20with%20each%20other.%20In%20processing%20a%20survey%2C%20this%20is%20the%20review%20and%20validation%20of%20the%20data%20between%20tables%2C%20and%20it%20comes%20before%20the%20values%20inside%20each%20table%20are%20checked%20(module%204).%20The%20survey%20keeps%20each%20fishing%20unit%20in%20one%20table%2C%20%60fishing_unit%60%2C%20and%20its%20crew%2C%20boats%2C%20gear%2C%20catch%2C%20landing%20sites%20and%20assistance%20in%20six%20child%20tables.%20The%20roster%2C%20%60member%60%2C%20lists%20the%20household's%20members%20and%20says%20which%20of%20them%20runs%20a%20unit.%20A%20child%20row%20belongs%20to%20its%20unit%20only%20through%20the%20key%20it%20carries.%20When%20one%20part%20of%20that%20key%20is%20typed%20wrong%20at%20data%20entry%2C%20the%20row%20becomes%20an%20**orphan**%3A%20a%20catch%20with%20no%20unit%2C%20or%20a%20unit%20with%20no%20catch.%20Every%20table%20built%20by%20unit%20then%20miscounts%20it%2C%20and%20nothing%20raises%20an%20error.%0A%0A%20%20%20%20The%20check%20has%20four%20steps%2C%20and%20each%20has%20its%20section%3A%0A%0A%20%20%20%20%7C%20step%20%7C%20what%20it%20does%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%20List%20the%20rows%20of%20every%20child%20table%20that%20match%20no%20unit%2C%20and%20check%20each%20operator%20against%20the%20roster%20%7C%20section%201%20%7C%0A%20%20%20%20%7C%202%20%7C%20Find%20the%20unmatched%20keys%20both%20ways%3A%20catch%20with%20no%20unit%2C%20units%20with%20no%20catch%20%7C%20section%202%20%7C%0A%20%20%20%20%7C%203%20%7C%20Find%20which%20part%20of%20a%20stray%20key%20differs%2C%20and%20decide%20by%20rule%20which%20keys%20a%20program%20corrects%20and%20which%20go%20to%20a%20person%20%7C%20sections%203%20and%204%20%7C%0A%20%20%20%20%7C%204%20%7C%20Write%20the%20corrections%20into%20the%20database%2C%20with%20an%20audit%20flag%20%7C%20section%205%20%7C%0A%0A%20%20%20%20One%20way%20to%20write%20these%20steps%20in%20R%20pastes%20the%20key%20parts%20into%20one%20text%2C%20anti-joins%20both%20ways%2C%20pairs%20the%20leftover%20rows%20by%20row%20number%20within%20the%20household%2C%20labels%20each%20pair%2C%20finds%20the%20part%20that%20caused%20the%20mismatch%2C%20and%20writes%20the%20corrected%20values%20with%20their%20row%20ids%20into%20the%20database%20in%20a%20transaction%20that%20sets%20a%20flag.%20This%20module%20does%20each%20step%20in%20pandas%2C%20with%20polars%20and%20DuckDB%20SQL%20beside%20it%2C%20and%20changes%20two%20of%20them.%20A%20stray%20row%20is%20compared%20with%20**every**%20unit%20of%20its%20household%2C%20not%20only%20the%20one%20that%20shares%20its%20position.%20And%20the%20corrections%20become%20a%20table%20a%20reviewer%20reads%20before%20anything%20is%20written.%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20you%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20Orphans%20across%20the%20tables%20%7C%20find%20the%20orphans%20of%20every%20child%20table%20with%20one%20function%2C%20and%20check%20each%20operator's%20roster%20line%20against%20the%20units%20that%20name%20it%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Orphan%20rows%20both%20ways%20%7C%20list%20the%20catch%20keys%20with%20no%20unit%20and%20the%20units%20with%20no%20catch%3A%20semi-joins%2C%20%60merge%60's%20%60indicator%3D%60%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Keys%20of%20several%20parts%20%7C%20pair%20the%20leftovers%20within%20a%20household%2C%20label%20each%20pair%2C%20and%20find%20which%20part%20of%20the%20key%20differs%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20The%20automated%20case%20and%20the%20manual%20case%20%7C%20decide%20by%20rule%20which%20keys%20a%20program%20may%20correct%20and%20which%20go%20to%20a%20person%2C%20and%20turn%20the%20corrections%20into%20an%20edits%20table%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20Edits%20written%20back%20%7C%20write%20the%20corrections%20into%20a%20copy%20of%20a%20regional%20database%3A%20a%20dry%20run%20first%2C%20a%20transaction%2C%20an%20audit%20flag%2C%20and%20a%20second%20run%20that%20changes%20nothing%20%7C%0A%0A%20%20%20%20**The%20tables.**%20Every%20cell%20starts%20from%20three%20tables%20the%20setup%20cell%20reads%3A%20%60units%60%20(%60course.units()%60%2C%20one%20row%20per%20fishing%20unit)%2C%20%60catch%60%20(%60course.table(%22catch%22)%60%2C%20one%20row%20per%20species%20a%20unit%20caught)%20and%20%60members%60%20(%60course.members()%60%2C%20one%20row%20per%20household%20member).%20All%20three%20carry%20%60region_code%60%2C%20and%20all%20three%20leave%20out%20the%20removed%20households%20and%20voided%20rows%2C%20as%20module%202%20taught.%20The%20catch%20table%20comes%20as%20the%20database%20stores%20it%3A%20its%20key%20parts%20start%20with%20%60catch_%60%2C%20the%20prefix%20every%20child%20table%20puts%20in%20front%20of%20its%20own%20copy%20of%20the%20key.%20Section%201%20reads%20the%20other%20child%20tables%20the%20same%20way.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20modules%20before%3A%20write%20the%20function%2C%20delete%20its%20%60raise%20NotImplementedError%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20and%20read%20the%20check.%20When%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%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.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Orphans%20across%20the%20tables%3A%20every%20child%20table%20by%20its%20prefix%2C%20operators%20by%20their%20roster%20line%0A%0A%20%20%20%20**The%20problem.**%20The%20first%20step%20looks%20at%20every%20table%20before%20anything%20is%20corrected.%20A%20catch%20row%20keyed%20astray%20is%20the%20orphan%20everyone%20knows%2C%20but%20a%20crew%20member%2C%20a%20boat%20or%20a%20gear%20can%20be%20keyed%20to%20a%20unit%20that%20does%20not%20exist%2C%20and%20the%20roster%20can%20disagree%20with%20the%20units%20about%20who%20runs%20one.%20Each%20one%20miscounts%20a%20different%20table%3A%20crew%20by%20sex%2C%20boats%20by%20tonnage%2C%20operators%20by%20age%20and%20education.%20So%20the%20first%20report%20has%20one%20line%20per%20table%2C%20with%20how%20many%20of%20its%20rows%20have%20no%20unit.%0A%0A%20%20%20%20**The%20keys%2C%20table%20by%20table.**%20Each%20child%20table%20carries%20the%20unit's%20two%20key%20parts%20with%20its%20own%20short%20name%20in%20front%2C%20and%20that%20name%20can%20be%20read%20from%20its%20column%20names%3A%20the%20table%20with%20a%20column%20%60boat_unit_no%60%20has%20the%20prefix%20%60boat%60.%20The%20roster%20carries%20the%20operator's%20side%20of%20the%20key%3A%20the%20member's%20line%20number%20%60mem_line%60%2C%20the%20flag%20%60mem_role_operator%60%20(1%20for%20a%20member%20who%20runs%20a%20unit)%20and%20%60mem_units_run%60%2C%20how%20many%20units%20that%20member%20runs.%20The%20cell%20below%20finds%20every%20child%20table%20and%20its%20prefix%20in%20the%20survey's%20own%20columns%2C%20so%20no%20table%20is%20left%20out%20because%20a%20list%20was%20typed%20by%20hand.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20child_prefixes()%20-%3E%20dict%5Bstr%2C%20str%5D%3A%0A%20%20%20%20%22%22%22Every%20child%20table%20of%20fishing_unit%20and%20its%20prefix%2C%20read%20from%20the%20tables'%20own%20columns%20(the%20table%20that%20has%20a%20column%0A%20%20%20%20boat_unit_no%20has%20the%20prefix%20boat)%2C%20in%20table%20order.%22%22%22%0A%20%20%20%20with%20course.connect()%20as%20con%3A%0A%20%20%20%20%20%20%20%20found%20%3D%20con.execute(f%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20DISTINCT%20table_name%2C%20column_name%20FROM%20duckdb_columns()%0A%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20database_name%20%3D%20'%7Bcourse.SURVEY%7D_r%7Bcourse.REGIONS%5B0%5D%7D'%20AND%20regexp_matches(column_name%2C%20'%5E%5Ba-z%5D%2B_unit_no%24')%22%22%22).fetchall()%0A%20%20%20%20return%20dict(sorted((table%2C%20column.removesuffix(%22_unit_no%22))%20for%20table%2C%20column%20in%20found))%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Every%20child%20table%20and%20its%20prefix%2C%20from%20the%20survey's%20columns%3B%20each%20table%20as%20the%20database%20stores%20it%0A%20%20%20%20CHILDREN%20%3D%20child_prefixes()%0A%20%20%20%20children%20%3D%20%7Bname%3A%20course.table(name)%20for%20name%20in%20CHILDREN%7D%0A%0A%20%20%20%20def%20_keys_of(columns%2C%20wanted)%3A%0A%20%20%20%20%20%20%20%20return%20%22%2C%20%22.join(f%22%60%7Bc%7D%60%22%20for%20c%20in%20columns%20if%20c%20in%20wanted)%0A%0A%20%20%20%20_rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%22table%22%3A%20%22member%22%2C%20%22prefix%22%3A%20%22%22%2C%20%22key%20columns%22%3A%20_keys_of(members.columns%2C%20%5B%22hh_id%22%2C%20%22mem_line%22%2C%20%22mem_role_operator%22%2C%20%22mem_units_run%22%5D)%2C%20%22matched%20against%22%3A%20%22the%20units%20that%20name%20each%20%60operator_line%60%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22table%22%3A%20%22fishing_unit%22%2C%20%22prefix%22%3A%20%22%22%2C%20%22key%20columns%22%3A%20_keys_of(units.columns%2C%20%5B%22hh_id%22%2C%20*PARTS%5D)%2C%20%22matched%20against%22%3A%20%22every%20table%20on%20this%20list%22%7D%2C%0A%20%20%20%20%20%20%20%20*(%7B%22table%22%3A%20name%2C%20%22prefix%22%3A%20prefix%2C%0A%20%20%20%20%20%20%20%20%20%20%20%22key%20columns%22%3A%20_keys_of(children%5Bname%5D.columns%2C%20%5B%22hh_id%22%2C%20*(f%22%7Bprefix%7D_%7Bpart%7D%22%20for%20part%20in%20PARTS)%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%22matched%20against%22%3A%20%22the%20unit's%20key%22%7D%20for%20name%2C%20prefix%20in%20CHILDREN.items())%2C%0A%20%20%20%20%5D%0A%20%20%20%20_table%20%3D%20%22%7C%20table%20%7C%20prefix%20%7C%20key%20columns%20%7C%20matched%20against%20%7C%5Cn%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%5Cn%22%20%2B%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20f%22%7C%20%60%7Brow%5B'table'%5D%7D%60%20%7C%20%7B'%60'%20%2B%20row%5B'prefix'%5D%20%2B%20'%60'%20if%20row%5B'prefix'%5D%20else%20''%7D%20%7C%20%7Brow%5B'key%20columns'%5D%7D%20%7C%20%7Brow%5B'matched%20against'%5D%7D%20%7C%22%20for%20row%20in%20_rows)%0A%20%20%20%20mo.md(_table%20%2B%20f%22%5Cn%5CnEvery%20key%20is%20read%20with%20%60region_code%60%20in%20front%3A%20a%20household%20id%20is%20unique%20only%20within%20its%20region.%20%22%0A%20%20%20%20%20%20%20%20%20%20f%22The%20prefixes%20read%20from%20the%20columns%20are%20the%20course's%20own%20list%2C%20%60course.CHILD_PREFIX%60%3A%20**%7BCHILDREN%20%3D%3D%20CHILD_PREFIX%7D**.%22)%0A%20%20%20%20return%20CHILDREN%2C%20children%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%20Here%20is%20the%20key%20on%20each%20side%20of%20the%20commonest%20match%2C%20the%20unit%20and%20its%20catch.%20A%20unit%20is%20named%20by%20four%20values%3A%20the%20region%2C%20the%20household%20(%60hh_id%60)%2C%20the%20roster%20line%20of%20the%20member%20who%20runs%20it%20(%60operator_line%60)%20and%20the%20unit's%20number%20among%20that%20member's%20units%20(%60unit_no%60).%20A%20catch%20row%20carries%20the%20same%20four%20values%20to%20say%20which%20unit%20it%20belongs%20to.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20key%20on%20both%20sides%3A%20the%20same%20parts%2C%20under%20the%20catch%20table's%20own%20names%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**units**%22)%2C%20units%5BKEY%5D.head(4)%5D)%2C%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**catch**%22)%2C%20catch%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%2C%20%22catch_species%22%5D%5D.head(4)%5D)%2C%0A%20%20%20%20%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Why%20region_code%20belongs%20in%20the%20key%3A%20the%20same%20household%20id%20is%20used%20in%20more%20than%20one%20region's%20database%0A%20%20%20%20_households%20%3D%20course.table(%22household%22)%0A%20%20%20%20_shared%20%3D%20(_households.groupby(%22hh_id%22)%5B%22region_code%22%5D.nunique()%20%3E%201).sum()%0A%20%20%20%20_distinct%20%3D%20catch%5BHOUSEHOLD%20%2B%20%5B%22catch_operator_line%22%2C%20%22catch_unit_no%22%5D%5D.drop_duplicates()%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20-%20**%7B_shared%3A%2C%7D%20of%20the%20%7B_households%5B'hh_id'%5D.nunique()%3A%2C%7D%20household%20ids**%20are%20used%20in%20more%20than%20one%20region.%20A%20key%20without%20%60region_code%60%20matches%20a%20household%20of%20region%2001%20to%20a%20household%20of%20region%2004%20that%20shares%20its%20number.%0A%20%20%20%20-%20The%20catch%20table%20has%20**%7Blen(catch)%3A%2C%7D%20rows**%20but%20**%7Blen(_distinct)%3A%2C%7D%20distinct%20keys**%3A%20a%20unit%20with%20five%20species%20has%20five%20catch%20rows%20with%20one%20key.%20An%20orphan%20is%20a%20key%2C%20so%20count%20keys%2C%20and%20report%20how%20many%20rows%20each%20carries.%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%20Two%20errors%20you%20will%20meet%20first%0A%0A%20%20%20%20A%20join%20needs%20the%20same%20column%20names%20and%20the%20same%20types%20on%20both%20sides.%20The%20catch%20table's%20key%20parts%20start%20with%20%60catch_%60%2C%20so%20a%20join%20on%20the%20unit's%20names%20cannot%20find%20them.%20And%20a%20table%20read%20straight%20from%20a%20CSV%20file%2C%20such%20as%20%60planted_defects.csv%60%2C%20stores%20%60region_code%60%20as%20a%20number%2C%20while%20the%20survey's%20tables%20keep%20it%20as%20zero-padded%20text.%20Both%20stop%20the%20join%3B%20read%20each%20message%20from%20its%20last%20line.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Two%20joins%20that%20stop%20with%20an%20error%2C%20and%20what%20each%20error's%20last%20line%20says%0A%20%20%20%20def%20_error(join)%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20join()%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22no%20error%22%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20shown%20on%20purpose%3A%20the%20message%20is%20the%20lesson%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20f%22%7Btype(error).__name__%7D%3A%20%7Berror%7D%22%0A%0A%20%20%20%20_prefix%20%3D%20_error(lambda%3A%20catch.merge(units%2C%20on%3DKEY))%0A%20%20%20%20_types%20%3D%20_error(lambda%3A%20units.merge(pd.read_csv(PATHS.planted_defects)%2C%20on%3DHOUSEHOLD))%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20the%20join%20%7C%20the%20error's%20last%20line%20%7C%20what%20it%20means%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60catch.merge(units%2C%20on%3DKEY)%60%20%7C%20%60%7B_prefix%7D%60%20%7C%20%60catch%60%20has%20no%20column%20%60operator_line%60%3A%20its%20part%20is%20%60catch_operator_line%60.%20Rename%20first.%20%7C%0A%20%20%20%20%7C%20%60units.merge(pd.read_csv(...planted_defects...)%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D)%60%20%7C%20%60%7B_types%7D%60%20%7C%20%60region_code%60%20is%20text%20on%20one%20side%20and%20a%20number%20on%20the%20other.%20%60course.planted_defects()%60%20reads%20it%20as%20text.%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**The%20tool.**%20Four%20kinds%20of%20join%20answer%20the%20questions%20a%20key%20check%20asks.%20An%20**anti-join**%20keeps%20the%20rows%20of%20one%20table%20whose%20key%20is%20**not**%20in%20the%20other%3A%20the%20orphans.%20A%20**semi-join**%20keeps%20the%20rows%20whose%20key%20**is**%20in%20the%20other%2C%20without%20adding%20the%20other's%20columns%20or%20repeating%20rows.%20An%20**outer%20join%20with%20an%20indicator**%20keeps%20every%20key%20of%20both%20tables%20and%20says%20where%20each%20came%20from%2C%20so%20one%20join%20answers%20both%20directions.%0A%0A%20%20%20%20%7C%20question%20%7C%20R%20(dplyr)%20%7C%20pandas%203%20%7C%20polars%20%7C%20DuckDB%20SQL%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20catch%20keys%20with%20no%20unit%20%7C%20%60anti_join(catch%2C%20units%2C%20by%20%3D%20...)%60%20%7C%20%60catch.merge(units%2C%20on%3DKEY%2C%20how%3D%22left_anti%22)%60%20%7C%20%60catch.join(units%2C%20on%3DKEY%2C%20how%3D%22anti%22)%60%20%7C%20%60FROM%20catch%20ANTI%20JOIN%20units%20USING%20(...)%60%20%7C%0A%20%20%20%20%7C%20units%20that%20have%20catch%20%7C%20%60semi_join(units%2C%20catch%2C%20by%20%3D%20...)%60%20%7C%20%60units.merge(catch%5BKEY%5D.drop_duplicates()%2C%20on%3DKEY)%60%20%7C%20%60units.join(catch%2C%20on%3DKEY%2C%20how%3D%22semi%22)%60%20%7C%20%60FROM%20units%20SEMI%20JOIN%20catch%20USING%20(...)%60%20%7C%0A%20%20%20%20%7C%20both%20directions%20at%20once%20%7C%20%60full_join(...)%60%2C%20then%20%60is.na()%60%20on%20each%20side%20%7C%20%60units.merge(catch%2C%20on%3DKEY%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%60%20%7C%20%60units.join(catch%2C%20on%3DKEY%2C%20how%3D%22full%22)%60%20%7C%20%60FULL%20JOIN%60%2C%20then%20%60IS%20NULL%60%20on%20each%20side%20%7C%0A%0A%20%20%20%20In%20R%2C%20%60by%20%3D%20c(%22catch_operator_line%22%20%3D%20%22operator_line%22%2C%20...)%60%20pairs%20a%20column%20of%20the%20left%20table%20with%20a%20column%20of%20the%20right%20one.%20pandas%20does%20the%20same%20with%20%60left_on%3D%60%20and%20%60right_on%3D%60%2C%20or%20you%20rename%20the%20catch%20table's%20parts%20once%20and%20join%20%60on%3DKEY%60%2C%20which%20is%20what%20this%20module%20does.%0A%0A%20%20%20%20The%20cell%20below%20finds%20the%20orphan%20catch%20keys%20three%20ways%2C%20from%20the%20same%20two%20tables.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20catch's%20distinct%20keys%20under%20the%20unit's%20names%2C%20then%20the%20same%20anti-join%20in%20pandas%2C%20polars%20and%20DuckDB%20SQL%0A%20%20%20%20_keys%20%3D%20catch.rename(columns%3D%7B%22catch_operator_line%22%3A%20%22operator_line%22%2C%20%22catch_unit_no%22%3A%20%22unit_no%22%7D)%5BKEY%5D.drop_duplicates()%0A%20%20%20%20_in_pandas%20%3D%20_keys.merge(units%5BKEY%5D%2C%20on%3DKEY%2C%20how%3D%22left_anti%22)%0A%20%20%20%20_in_polars%20%3D%20pl.from_pandas(_keys).join(pl.from_pandas(units%5BKEY%5D)%2C%20on%3DKEY%2C%20how%3D%22anti%22)%0A%20%20%20%20_con%20%3D%20duckdb.connect()%0A%20%20%20%20_con.register(%22catch_keys%22%2C%20_keys)%0A%20%20%20%20_con.register(%22unit_keys%22%2C%20units%5BKEY%5D)%0A%20%20%20%20_in_sql%20%3D%20_con.execute(%22SELECT%20*%20FROM%20catch_keys%20ANTI%20JOIN%20unit_keys%20USING%20(region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no)%22).df()%0A%20%20%20%20_con.close()%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20tool%20%7C%20catch%20keys%20with%20no%20unit%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20pandas%2C%20%60how%3D%22left_anti%22%60%20%7C%20**%7Blen(_in_pandas)%7D**%20%7C%0A%20%20%20%20%7C%20polars%2C%20%60how%3D%22anti%22%60%20%7C%20**%7B_in_polars.height%7D**%20%7C%0A%20%20%20%20%7C%20DuckDB%2C%20%60ANTI%20JOIN%60%20%7C%20**%7Blen(_in_sql)%7D**%20%7C%0A%0A%20%20%20%20Three%20tools%2C%20one%20answer.%20The%20rest%20of%20the%20module%20works%20in%20pandas%3B%20module%202's%20DuckDB%20and%20polars%20habits%20carry%20over%20line%20for%20line.%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%20One%20function%20for%20every%20table%0A%0A%20%20%20%20The%20child%20tables%20differ%20only%20in%20their%20prefix%3A%20the%20boats'%20%60boat_operator_line%60%20is%20the%20catch's%20%60catch_operator_line%60.%20A%20function%20that%20takes%20the%20prefix%20can%20rename%20the%20two%20parts%20to%20the%20unit's%20names%2C%20and%20then%20one%20anti-join%20finds%20the%20orphans%20of%20any%20table.%20The%20R%20way%20builds%20the%20prefixed%20names%20with%20%60paste0()%60%20and%20hands%20them%20to%20%60by%20%3D%60.%20Python%20builds%20the%20renaming%20as%20a%20dictionary%2C%20with%20a%20dict%20comprehension%20over%20the%20two%20parts%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20prefix%20%3C-%20%22boat%22%0A%20%20%20%20by_key%20%3C-%20c(%22region_code%22%2C%20%22hh_id%22%2C%20setNames(c(%22operator_line%22%2C%20%22unit_no%22)%2C%20paste0(prefix%2C%20%22_%22%2C%20c(%22operator_line%22%2C%20%22unit_no%22))))%0A%20%20%20%20anti_join(boats%2C%20units%2C%20by%20%3D%20by_key)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20prefix%20%3D%20%22boat%22%0A%20%20%20%20%7Bf%22%7Bprefix%7D_%7Bpart%7D%22%3A%20part%20for%20part%20in%20PARTS%7D%20%20%20%23%20%7B%22boat_operator_line%22%3A%20%22operator_line%22%2C%20%22boat_unit_no%22%3A%20%22unit_no%22%7D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Build%20the%20names%20from%20the%20prefix%20and%20the%20two%20parts%2C%20and%20rename%20only%20those.%20Two%20shortcuts%20break.%20The%20table's%20name%20is%20not%20always%20its%20prefix%3A%20the%20landing%20sites%20are%20%60land_%60%20and%20the%20assistance%20is%20%60aid_%60%2C%20so%20pass%20the%20prefix%2C%20not%20the%20table's%20name.%20And%20stripping%20the%20prefix%20from%20every%20column%20that%20has%20it%20renames%20the%20table's%20other%20columns%20too%3A%20%60aid_kind%60%20would%20become%20%60kind%60.%20The%20course's%20own%20%60course.child()%60%20does%20this%20rename%20for%20the%20modules%20after%20this%20one%2C%20so%20they%20join%20any%20child%20table%20on%20%60UNIT_KEY%60%20directly.%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%203.1.%20The%20orphans%20of%20any%20child%20table%2C%20by%20its%20prefix%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20first%20step%20for%20every%20child%20table%20at%20once.%20One%20function%2C%20given%20a%20table's%20prefix%2C%20lists%20the%20rows%20the%20unit%20table%20does%20not%20know%2C%20so%20no%20table%20is%20skipped%20because%20its%20column%20names%20differ.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20child%20table%20as%20the%20database%20stores%20it%20(%60children%5B%22boat%22%5D%60)%2C%20the%20units%2C%20and%20the%20table's%20prefix%20(%60%22boat%22%60)%20%E2%86%92%20**the%20child%20table's%20rows%20whose%20key%20matches%20no%20unit**%2C%20every%20column%20kept%2C%20the%20two%20key%20parts%20under%20the%20unit's%20names.%20For%20a%20small%20assistance%20table%20(prefix%20%60aid%60)%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20row_id%20%7C%20hh_id%20%7C%20operator_line%20%7C%20unit_no%20%7C%20aid_line%20%7C%20aid_kind%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2004%20%7C%201%20%7C%205101%20%7C%201%20%7C%201%20%7C%201%20%7C%203%20%7C%0A%20%20%20%20%7C%2001%20%7C%203%20%7C%205103%20%7C%201%20%7C%209%20%7C%201%20%7C%204%20%7C%0A%20%20%20%20%7C%2001%20%7C%204%20%7C%205107%20%7C%201%20%7C%201%20%7C%201%20%7C%202%20%7C%0A%0A%20%20%20%20The%20first%20row%20is%20the%20region%20trap%3A%20region%2001's%20household%205101%20has%20a%20unit%20with%20the%20key%201%2C%201%2C%20and%20region%2004's%20household%205101%20does%20not.%20The%20second%20names%20a%20unit%20number%20its%20operator%20does%20not%20have.%20The%20last%20is%20a%20household%20with%20no%20unit%20at%20all.%0A%0A%20%20%20%20**Tools%3A**%20a%20dict%20comprehension%20inside%20%60rename(columns%3D...)%60%2C%20and%20%60merge(...%2C%20how%3D%22left_anti%22)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20orphan_rows(child%3A%20pd.DataFrame%2C%20units%3A%20pd.DataFrame%2C%20prefix%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20rows%20of%20a%20child%20table%20whose%20key%20matches%20no%20unit%3A%20every%20column%20kept%2C%20the%20key%20parts%20renamed%20to%20the%20unit's%0A%20%20%20%20names.%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%20rename%20the%20table's%20two%20key%20parts%20to%20the%20unit's%20names%3A%20%7Bf%22%7Bprefix%7D_%7Bpart%7D%22%3A%20part%20for%20part%20in%20PARTS%7D%0A%20%20%20%20%23%20Step%202%3A%20keep%20the%20rows%20whose%20key%20matches%20no%20unit%3A%20merge%20with%20units%5BKEY%5D%2C%20on%3DKEY%2C%20how%3D%22left_anti%22%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.1%22)%0A%0A%0A%40app.function%0Adef%20small_survey()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22A%20small%20survey%20made%20by%20hand%20for%20the%20checks%3A%20(units%2C%20catch).%20Region%2004's%20household%205101%20shares%20its%20id%20with%20region%0A%20%20%20%2001's%3B%20household%205102's%20only%20unit%20has%20no%20catch%20of%20its%20own%20and%20one%20stray%20key%3B%20in%205103%20a%20member%20runs%20two%20units%20and%20a%0A%20%20%20%20catch%20row%20is%20numbered%20as%20a%20third%3B%205105's%20unit%20is%20entered%20twice%3B%205107%20has%20catch%20and%20no%20unit%3B%205108%20has%20two%20units%2C%0A%20%20%20%20neither%20with%20catch%3B%20in%205109%20a%20catch%20key%20differs%20from%20the%20unit%20in%20both%20parts.%22%22%22%0A%20%20%20%20found_units%20%3D%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205101%2C%201%2C%201)%2C%20(%2204%22%2C%205101%2C%202%2C%201)%2C%20(%2201%22%2C%205102%2C%201%2C%201)%2C%20(%2201%22%2C%205103%2C%201%2C%201)%2C%20(%2201%22%2C%205103%2C%201%2C%202)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205104%2C%203%2C%201)%2C%20(%2201%22%2C%205105%2C%201%2C%201)%2C%20(%2201%22%2C%205105%2C%201%2C%201)%2C%20(%2204%22%2C%205106%2C%201%2C%201)%2C%20(%2201%22%2C%205108%2C%201%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205108%2C%202%2C%201)%2C%20(%2201%22%2C%205109%2C%201%2C%201)%2C%0A%20%20%20%20%5D%2C%20columns%3DKEY)%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20region_code%2C%20catch%20row%20id%2C%20hh_id%2C%20catch_operator_line%2C%20catch_unit_no%2C%20catch_line%2C%20catch_species%0A%20%20%20%20%20%20%20%20(%2201%22%2C%201%2C%205101%2C%201%2C%201%2C%201%2C%20101)%2C%20(%2201%22%2C%202%2C%205101%2C%201%2C%201%2C%202%2C%20104)%2C%20(%2201%22%2C%203%2C%205101%2C%202%2C%201%2C%203%2C%20201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%204%2C%205102%2C%202%2C%203%2C%201%2C%20101)%2C%20(%2201%22%2C%205%2C%205102%2C%202%2C%203%2C%202%2C%20104)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%206%2C%205103%2C%201%2C%201%2C%201%2C%20101)%2C%20(%2201%22%2C%207%2C%205103%2C%201%2C%202%2C%201%2C%20201)%2C%20(%2201%22%2C%208%2C%205103%2C%201%2C%203%2C%201%2C%20104)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%209%2C%205105%2C%201%2C%201%2C%201%2C%20101)%2C%20(%2201%22%2C%2010%2C%205105%2C%201%2C%202%2C%201%2C%20201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%2011%2C%205107%2C%201%2C%201%2C%201%2C%20101)%2C%20(%2201%22%2C%2012%2C%205108%2C%201%2C%203%2C%201%2C%20104)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%2013%2C%205109%2C%201%2C%201%2C%201%2C%20101)%2C%20(%2201%22%2C%2014%2C%205109%2C%203%2C%202%2C%201%2C%20201)%2C%0A%20%20%20%20%20%20%20%20(%2204%22%2C%201%2C%205101%2C%202%2C%201%2C%201%2C%20101)%2C%20(%2204%22%2C%202%2C%205101%2C%202%2C%201%2C%202%2C%20201)%2C%20(%2204%22%2C%203%2C%205101%2C%201%2C%201%2C%203%2C%20104)%2C%0A%20%20%20%20%20%20%20%20(%2204%22%2C%204%2C%205106%2C%201%2C%201%2C%201%2C%20101)%2C%0A%20%20%20%20%5D%0A%20%20%20%20found%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22region_code%22%2C%20%22row_id%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%2C%20%22catch_line%22%2C%20%22catch_species%22%5D)%0A%20%20%20%20return%20found_units%2C%20found%0A%0A%0A%40app.function%0Adef%20small_assistance()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20small%20survey's%20assistance%20table%20(prefix%20aid)%2C%20for%20the%20check%20of%20Exercise%203.1%3A%20a%20row%20of%20its%20unit%2C%20a%20row%20of%0A%20%20%20%20region%2004's%20household%205101%20keyed%20to%20the%20unit%20of%20region%2001's%2C%20a%20unit%20number%20the%20operator%20does%20not%20have%2C%20and%20a%0A%20%20%20%20household%20with%20no%20unit.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20region_code%2C%20assistance%20row%20id%2C%20hh_id%2C%20aid_operator_line%2C%20aid_unit_no%2C%20aid_line%2C%20kind%20of%20assistance%0A%20%20%20%20%20%20%20%20(%2201%22%2C%201%2C%205101%2C%201%2C%201%2C%201%2C%202)%2C%20(%2204%22%2C%201%2C%205101%2C%201%2C%201%2C%201%2C%203)%2C%20(%2201%22%2C%202%2C%205103%2C%201%2C%202%2C%201%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%203%2C%205103%2C%201%2C%209%2C%201%2C%204)%2C%20(%2201%22%2C%204%2C%205107%2C%201%2C%201%2C%201%2C%202)%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22region_code%22%2C%20%22row_id%22%2C%20%22hh_id%22%2C%20%22aid_operator_line%22%2C%20%22aid_unit_no%22%2C%20%22aid_line%22%2C%20%22aid_kind%22%5D)%0A%0A%0A%40app.function%0Adef%20as_rows(result%2C%20keys%2C%20columns%3DNone%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20result%20table%20in%20one%20shape%20for%20the%20checks%3A%20keys%20as%20columns%2C%20the%20named%20columns%20only%2C%20rows%20sorted%20by%20the%20keys%2C%0A%20%20%20%20columns%20in%20name%20order%2C%20numbers%20rounded.%20So%20a%20right%20answer%20passes%20whatever%20its%20row%20order%20or%20column%20order.%22%22%22%0A%20%20%20%20table%20%3D%20result.to_frame()%20if%20isinstance(result%2C%20pd.Series)%20else%20result%0A%20%20%20%20keys%20%3D%20%5Bkeys%5D%20if%20isinstance(keys%2C%20str)%20else%20list(keys)%0A%20%20%20%20if%20any(name%20in%20keys%20for%20name%20in%20table.index.names)%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20table.reset_index()%0A%20%20%20%20if%20columns%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20table%5Blist(columns)%5D%0A%20%20%20%20table%20%3D%20table.sort_values(keys%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%5Bsorted(table.columns)%5D%0A%20%20%20%20numbers%20%3D%20table.select_dtypes(%22number%22).columns%0A%20%20%20%20table%5Bnumbers%5D%20%3D%20table%5Bnumbers%5D.round(digits)%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20in_region(table%3A%20pd.DataFrame%2C%20region%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20rows%20of%20one%20region.%22%22%22%0A%20%20%20%20return%20table%5Btable%5B%22region_code%22%5D%20%3D%3D%20region%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(children)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.1%22%2C%20orphan_rows%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20assistance%20table%2C%20prefix%20aid%3A%20a%20household%20id%20shared%20by%20regions%2001%20and%2004%2C%20a%20household%20with%20no%20unit%22%2C%20(small_assistance()%2C%20small_survey()%5B0%5D%2C%20%22aid%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20crew%2C%20prefix%20crew%22%2C%20(children%5B%22crew%22%5D%2C%20units%2C%20%22crew%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20catch%2C%20prefix%20catch%22%2C%20(catch%2C%20units%2C%20%22catch%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20landing%20sites%2C%20prefix%20land%22%2C%20(children%5B%22landing%22%5D%2C%20units%2C%20%22land%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20sorted(table.columns))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22For%20any%20table%3A%20rename%20the%20two%20key%20parts%20that%20start%20with%20%3Cprefix%3E_%20to%20the%20unit's%20names%2C%20then%20keep%20the%20rows%20an%20anti-join%20on%20KEY%2C%20the%20region%20included%2C%20finds%20in%20no%20unit.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(children)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20crew%20rows%20keyed%20to%20no%20unit%0A%20%20%20%20try_it(orphan_rows%2C%20children%5B%22crew%22%5D%2C%20units%2C%20%22crew%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%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%20*KeyError%3A%20'operator_line'*%20on%20the%20crew%2C%20the%20catch%20table%20passes%20%7C%20The%20key%20parts%20still%20carry%20their%20prefix%3A%20the%20renaming%20was%20built%20for%20%60catch_%60%20whatever%20the%20table.%20%7C%20%60f%22%7Bprefix%7D_%7Bpart%7D%22%60%20%7C%0A%20%20%20%20%7C%20Columns%20such%20as%20%60kind%60%2C%20%60line%60%20or%20%60species%60%20in%20the%20result%20%7C%20The%20prefix%20was%20stripped%20from%20every%20column%20that%20has%20it%2C%20not%20only%20from%20the%20two%20key%20parts.%20%7C%20rename%20only%20the%20two%20parts%20%7C%0A%20%20%20%20%7C%20The%20small%20table's%20region%2004%20row%20is%20missing%20%7C%20The%20key%20left%20out%20%60region_code%60%2C%20so%20region%2004's%20row%20found%20region%2001's%20unit.%20%7C%20%60on%3DKEY%60%20%7C%0A%20%20%20%20%7C%20Every%20row%20of%20the%20table%20comes%20back%20%7C%20A%20left%20or%20outer%20join%2C%20which%20keeps%20the%20matched%20rows%20too.%20%7C%20%60how%3D%22left_anti%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%20Every%20table%20at%20once%0A%0A%20%20%20%20The%20same%20step%20in%20DuckDB%20SQL%2C%20for%20tables%20that%20stay%20in%20a%20database%3A%20%60SELECT%20*%20RENAME%20(...)%60%20gives%20the%20parts%20the%20unit's%20names%2C%20and%20%60ANTI%20JOIN%20...%20ON%60%20pairs%20each%20prefixed%20part%20with%20its%20own.%20The%20cell%20after%20it%20runs%20the%20function%20over%20every%20child%20table%2C%20so%20this%20report%20does%20not%20wait%20for%20your%20answer%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20orphans_in_sql(child%3A%20pd.DataFrame%2C%20units%3A%20pd.DataFrame%2C%20prefix%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%203.1%20in%20DuckDB%20SQL.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.register(%22child%22%2C%20child)%0A%20%20%20%20con.register(%22units%22%2C%20units)%0A%20%20%20%20out%20%3D%20con.execute(f%22%22%22%0A%20%20%20%20%20%20%20%20SELECT%20*%20RENAME%20(%7Bprefix%7D_operator_line%20AS%20operator_line%2C%20%7Bprefix%7D_unit_no%20AS%20unit_no)%0A%20%20%20%20%20%20%20%20FROM%20child%20c%20ANTI%20JOIN%20units%20u%0A%20%20%20%20%20%20%20%20%20%20%20%20ON%20u.region_code%20%3D%20c.region_code%20AND%20u.hh_id%20%3D%20c.hh_id%0A%20%20%20%20%20%20%20%20%20%20%20AND%20u.operator_line%20%3D%20c.%7Bprefix%7D_operator_line%20AND%20u.unit_no%20%3D%20c.%7Bprefix%7D_unit_no%0A%20%20%20%20%20%20%20%20ORDER%20BY%20ALL%22%22%22).df()%0A%20%20%20%20con.close()%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell%0Adef%20_(CHILDREN%2C%20children)%3A%0A%20%20%20%20%23%20The%20first%20step%3A%20one%20line%20per%20child%20table%2C%20its%20rows%20and%20its%20orphans%0A%20%20%20%20_lines%20%3D%20%5B%5D%0A%20%20%20%20for%20_name%2C%20_prefix%20in%20CHILDREN.items()%3A%0A%20%20%20%20%20%20%20%20_found%20%3D%20orphans_in_sql(children%5B_name%5D%2C%20units%2C%20_prefix)%0A%20%20%20%20%20%20%20%20_lines.append(%7B%22table%22%3A%20_name%2C%20%22prefix%22%3A%20_prefix%2C%20%22rows%22%3A%20len(children%5B_name%5D)%2C%20%22orphan%20rows%22%3A%20len(_found)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22orphan%20keys%22%3A%20len(_found%5BKEY%5D.drop_duplicates())%2C%20%22households%22%3A%20len(_found%5BHOUSEHOLD%5D.drop_duplicates())%7D)%0A%20%20%20%20table_orphans%20%3D%20pd.DataFrame(_lines)%0A%20%20%20%20_with%20%3D%20table_orphans%5Btable_orphans%5B%22orphan%20rows%22%5D%20%3E%200%5D%5B%22table%22%5D.tolist()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20table_orphans%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(_with)%7D%20of%20the%20%7Blen(table_orphans)%7D%20child%20tables%20have%20orphans%3A**%20%7B'%2C%20'.join(_with)%20or%20'none'%7D.%20Each%20line%20of%20the%20report%20is%20one%20anti-join%3B%20a%20table%20with%20no%20orphans%20is%20reported%20too%2C%20so%20the%20reader%20sees%20it%20was%20checked.%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%20The%20first%20step%20runs%20from%20the%20child%20to%20the%20unit.%20The%20other%20direction%2C%20a%20unit%20with%20no%20row%20in%20a%20child%20table%2C%20is%20an%20error%20only%20where%20the%20survey's%20rules%20say%20a%20unit%20must%20have%20rows%3A%20the%20catch%20(rule%20C3%2C%20which%20section%202%20checks)%20and%20the%20crew%20of%20a%20unit%20that%20hired%20crew%20(rule%20U6%2C%20in%20module%204).%20A%20unit%20with%20no%20boat%20row%20may%20fish%20without%20a%20boat.%0A%0A%20%20%20%20%23%23%23%20Operators%20against%20the%20roster%0A%0A%20%20%20%20**The%20problem.**%20The%20roster%20and%20the%20unit%20table%20name%20the%20operator%20twice.%20The%20unit%20names%20the%20roster%20line%20of%20the%20member%20who%20runs%20it%20(%60operator_line%60).%20The%20roster%20marks%20that%20member%20as%20an%20operator%20(%60mem_role_operator%60%20%3D%201)%20and%20says%20how%20many%20units%20the%20member%20runs%20(%60mem_units_run%60).%20When%20the%20two%20disagree%2C%20a%20table%20of%20operators%20by%20age%2C%20sex%20and%20education%20counts%20a%20member%20who%20runs%20nothing%2C%20or%20misses%20one%20who%20does.%20The%20survey's%20rule%2C%20from%20%60codebook_rules.csv%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20survey's%20rule%20on%20the%20operator%2C%20as%20the%20codebook%20writes%20it%0A%20%20%20%20_rules%20%3D%20pd.read_csv(PATHS.rules)%0A%20%20%20%20_rule%20%3D%20_rules%5B_rules%5B%22rule%22%5D%20%3D%3D%20%22U2%22%5D.iloc%5B0%5D%0A%20%20%20%20mo.md(f%22%3E%20**%7B_rule%5B'rule'%5D%7D**%20(%7B_rule%5B'table'%5D%7D)%3A%20%7B_rule%5B'rule_in_words'%5D%7D%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%20The%20check%20runs%20both%20ways%2C%20as%20every%20key%20check%20does%3A%0A%0A%20%20%20%20-%20**from%20each%20operator%20to%20the%20roster%3A**%20is%20there%20a%20line%20%60mem_line%60%20equal%20to%20the%20unit's%20%60operator_line%60%20in%20its%20household%3B%20is%20that%20member%20flagged%20as%20an%20operator%3B%20and%20does%20the%20member's%20%60mem_units_run%60%20equal%20the%20number%20of%20units%20that%20name%20the%20line%3F%0A%20%20%20%20-%20**from%20each%20roster%20line%20flagged%20as%20an%20operator%20to%20the%20units%3A**%20does%20any%20unit%20of%20its%20household%20name%20that%20line%3F%0A%0A%20%20%20%20A%20member%20who%20runs%20two%20units%20(%60unit_no%60%201%20and%202)%20has%20one%20roster%20line%2C%20so%20the%20first%20direction%20starts%20from%20each%20operator's%20number%20of%20distinct%20units%3A%20the%20household%2C%20%60operator_line%60%2C%20and%20a%20count.%20The%20setup%20cell%20names%20that%20key%20%60OPERATOR%60.%20A%20unit%20entered%20twice%20(one%20of%20module%204's%20duplicates)%20is%20still%20one%20unit%2C%20so%20take%20the%20units'%20distinct%20keys%20before%20counting.%0A%0A%20%20%20%20**The%20tool**%20is%20a%20join%20on%20columns%20of%20different%20names%2C%20R's%20%60by%20%3D%20c(%22operator_line%22%20%3D%20%22mem_line%22)%60.%20pandas%20takes%20the%20two%20lists%20as%20%60left_on%3D%60%20and%20%60right_on%3D%60%2C%20matched%20in%20order%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20operators.merge(members%2C%20left_on%3DOPERATOR%2C%20right_on%3DHOUSEHOLD%20%2B%20%5B%22mem_line%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60how%3D%22left%22%60%20keeps%20an%20operator%20whose%20line%20is%20missing%2C%20with%20the%20roster's%20columns%20blank.%20%60validate%3D%22many_to_one%22%60%20says%20each%20household%20has%20a%20line%20number%20once.%20The%20roster's%20flag%20and%20count%20are%20blank%20for%20members%20under%2010%2C%20and%20%60mem_units_run%60%20is%20blank%20for%20a%20member%20who%20runs%20nothing.%20A%20blank%20is%20%60%3CNA%3E%60%2C%20and%20a%20comparison%20with%20%60%3CNA%3E%60%20gives%20%60%3CNA%3E%60%2C%20neither%20True%20nor%20False.%20So%20fill%20the%20blank%20before%20you%20compare%3A%20%60mem_role_operator.fillna(0)%20!%3D%201%60%20is%20True%20for%20a%20blank%20flag%2C%20and%20%60mem_units_run.fillna(0)%20!%3D%20units%60%20is%20True%20for%20a%20blank%20count.%20Here%20is%20one%20household's%20unit%20beside%20its%20roster%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20One%20household's%20unit%20beside%20its%20roster%3A%20operator_line%20names%20a%20line%20mem_line%2C%20and%20that%20line%20is%20flagged%20and%20counts%20the%20units%0A%20%20%20%20_unit%20%3D%20units.iloc%5B0%5D%0A%20%20%20%20_roster%20%3D%20members%5B(members%5B%22region_code%22%5D%20%3D%3D%20_unit%5B%22region_code%22%5D)%20%26%20(members%5B%22hh_id%22%5D%20%3D%3D%20_unit%5B%22hh_id%22%5D)%5D%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**the%20unit**%22)%2C%20units.loc%5B%5Bunits.index%5B0%5D%5D%2C%20KEY%5D%5D)%2C%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**its%20household's%20roster**%22)%2C%20_roster%5BHOUSEHOLD%20%2B%20%5B%22mem_line%22%2C%20%22mem_relation%22%2C%20%22mem_age%22%2C%20%22mem_role_operator%22%2C%20%22mem_units_run%22%5D%5D%5D)%2C%0A%20%20%20%20%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%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%203.2.%20Operators%20against%20the%20roster%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20roster%20that%20disagrees%20with%20the%20units%20is%20an%20orphan%20of%20another%20kind.%20The%20unit%20looks%20run%20by%20nobody%2C%20or%20the%20member's%20line%20by%20someone%20who%20runs%20another%20number%20of%20units.%20The%20first%20step%20lists%20it%20beside%20the%20child%20tables.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20members%20(%60members%60)%20and%20the%20units%20%E2%86%92%20**one%20row%20per%20mismatch%2C%20from%20either%20side**%2C%20with%20the%20household%2C%20the%20unit%20side's%20%60operator_line%60%20and%20%60units%60%20(how%20many%20distinct%20units%20name%20the%20line%3B%20blank%20for%20a%20roster%20line%20no%20unit%20names)%2C%20the%20roster's%20%60mem_line%60%2C%20%60mem_role_operator%60%20and%20%60mem_units_run%60%20(blank%20where%20there%20is%20no%20line)%2C%20and%20%60status%60%3A%0A%0A%20%20%20%20-%20%60%22no%20roster%20line%22%60%3A%20no%20line%20of%20the%20household%20has%20the%20unit's%20%60operator_line%60%3B%0A%20%20%20%20-%20%60%22operator%20not%20flagged%22%60%3A%20the%20line%20is%20there%2C%20and%20its%20%60mem_role_operator%60%20is%20not%201%20(0%20or%20blank)%3B%0A%20%20%20%20-%20%60%22count%20differs%22%60%3A%20the%20line%20is%20flagged%2C%20and%20its%20%60mem_units_run%60%20is%20not%20the%20number%20of%20units%20that%20name%20it%20(a%20blank%20count%20differs%20too)%3B%0A%20%20%20%20-%20%60%22flag%20names%20no%20unit%22%60%3A%20a%20roster%20line%20flagged%20as%20an%20operator%20that%20no%20unit%20of%20its%20household%20names.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20units%20%7C%20mem_line%20%7C%20mem_role_operator%20%7C%20mem_units_run%20%7C%20status%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%205202%20%7C%201%20%7C%201%20%7C%201%20%7C%201%20%7C%202%20%7C%20count%20differs%20%7C%0A%20%20%20%20%7C%2001%20%7C%205203%20%7C%203%20%7C%201%20%7C%20%7C%20%7C%20%7C%20no%20roster%20line%20%7C%0A%20%20%20%20%7C%2001%20%7C%205204%20%7C%202%20%7C%201%20%7C%202%20%7C%200%20%7C%20%7C%20operator%20not%20flagged%20%7C%0A%20%20%20%20%7C%2001%20%7C%205204%20%7C%20%7C%20%7C%201%20%7C%201%20%7C%201%20%7C%20flag%20names%20no%20unit%20%7C%0A%20%20%20%20%7C%2001%20%7C%205205%20%7C%20%7C%20%7C%202%20%7C%201%20%7C%201%20%7C%20flag%20names%20no%20unit%20%7C%0A%20%20%20%20%7C%2007%20%7C%205206%20%7C%201%20%7C%201%20%7C%201%20%7C%201%20%7C%20%7C%20count%20differs%20%7C%0A%20%20%20%20%7C%2007%20%7C%205207%20%7C%20%7C%20%7C%201%20%7C%201%20%7C%201%20%7C%20flag%20names%20no%20unit%20%7C%0A%0A%20%20%20%20A%20unit%20that%20names%20the%20wrong%20line%20shows%20from%20both%20sides%2C%20as%20household%205204%20does%3A%20the%20unit%20finds%20a%20line%20that%20is%20not%20flagged%2C%20and%20the%20flagged%20line%20finds%20no%20unit.%20In%20household%205205%20a%20second%20member%20is%20flagged%3B%20household%205207%20has%20a%20flagged%20line%20and%20no%20unit%20at%20all.%20A%20matched%20operator%20has%20no%20row%2C%20and%20neither%20has%20a%20member%20who%20runs%20nothing.%0A%0A%20%20%20%20**Tools%3A**%20%60drop_duplicates()%60%2C%20%60groupby().size()%60%2C%20%60merge(...%2C%20left_on%3D...%2C%20right_on%3D...%2C%20how%3D%22left%22)%60%2C%20%60fillna%60%2C%20%60np.select%60%2C%20%60merge(...%2C%20how%3D%22left_anti%22)%60%20and%20%60pd.concat%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20match_operators(members%3A%20pd.DataFrame%2C%20units%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20operator%20whose%20roster%20line%20does%20not%20match%20the%20units%20that%20name%20it%2C%20and%20every%20roster%20line%20flagged%20as%20an%0A%20%20%20%20operator%20that%20no%20unit%20names%3A%20the%20household%2C%20operator_line%2C%20units%2C%20mem_line%2C%20mem_role_operator%2C%20mem_units_run%20and%0A%20%20%20%20status.%22%22%22%0A%20%20%20%20%23%20Exercise%203.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20operators%3A%20units%5BKEY%5D.drop_duplicates()%2C%20then%20groupby(OPERATOR).size()%20named%20%22units%22%2C%20reset_index()%0A%20%20%20%20%23%20Step%202%3A%20each%20operator's%20roster%20line%3A%20merge%20with%20members%5BHOUSEHOLD%20%2B%20%5B%22mem_line%22%2C%20%22mem_role_operator%22%2C%20%22mem_units_run%22%5D%5D%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20left_on%3DOPERATOR%2C%20right_on%3DHOUSEHOLD%20%2B%20%5B%22mem_line%22%5D%2C%20how%3D%22left%22%0A%20%20%20%20%23%20Step%203%3A%20status%20with%20np.select%2C%20in%20this%20order%3A%20mem_line%20blank%20-%3E%20%22no%20roster%20line%22%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20mem_role_operator.fillna(0)%20!%3D%201%20-%3E%20%22operator%20not%20flagged%22%3B%20mem_units_run.fillna(0)%20!%3D%20units%20-%3E%20%22count%20differs%22%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20default%20%22matched%22%3B%20keep%20the%20rows%20not%20matched%0A%20%20%20%20%23%20Step%204%3A%20the%20roster%20lines%20whose%20mem_role_operator%20is%201%2C%20anti-joined%20to%20the%20operators%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20(left_on%3DHOUSEHOLD%20%2B%20%5B%22mem_line%22%5D%2C%20right_on%3DOPERATOR)%3B%20status%20%22flag%20names%20no%20unit%22%0A%20%20%20%20%23%20Step%205%3A%20pd.concat%20the%20two%2C%20with%20the%20columns%20OPERATOR%20%2B%20units%2C%20mem_line%2C%20mem_role_operator%2C%20mem_units_run%2C%20status%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.2%22)%0A%0A%0A%40app.function%0Adef%20operator_columns()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20columns%20Exercise%203.2%20returns.%22%22%22%0A%20%20%20%20return%20OPERATOR%20%2B%20%5B%22units%22%2C%20%22mem_line%22%2C%20%22mem_role_operator%22%2C%20%22mem_units_run%22%2C%20%22status%22%5D%0A%0A%0A%40app.function%0Adef%20small_roster()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22A%20small%20survey's%20roster%20and%20units%2C%20for%20the%20check%20of%20Exercise%203.2%3A%20(members%2C%20units).%20In%20household%205201%20one%20member%0A%20%20%20%20runs%20two%20units%3B%20region%2004's%20household%205201%20shares%20its%20id%20with%20region%2001's%3B%205202's%20operator%20counts%20two%20units%20and%20runs%0A%20%20%20%20one%3B%205203%20has%20no%20line%203%3B%205204's%20unit%20names%20line%202%2C%20and%20line%201%20is%20the%20one%20flagged%3B%20in%205205%20a%20second%20member%20is%0A%20%20%20%20flagged%3B%205206's%20operator%20has%20a%20blank%20count%2C%20and%20a%20child%20whose%20roles%20are%20blank%3B%205207%20has%20a%20flagged%20line%20and%20no%20unit%3B%0A%20%20%20%205208's%20unit%20is%20entered%20twice.%22%22%22%0A%20%20%20%20found_units%20%3D%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205201%2C%201%2C%201)%2C%20(%2201%22%2C%205201%2C%201%2C%202)%2C%20(%2204%22%2C%205201%2C%202%2C%201)%2C%20(%2201%22%2C%205202%2C%201%2C%201)%2C%20(%2201%22%2C%205203%2C%203%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205204%2C%202%2C%201)%2C%20(%2201%22%2C%205205%2C%201%2C%201)%2C%20(%2207%22%2C%205206%2C%201%2C%201)%2C%20(%2201%22%2C%205208%2C%201%2C%201)%2C%20(%2201%22%2C%205208%2C%201%2C%201)%2C%0A%20%20%20%20%5D%2C%20columns%3DKEY)%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20region_code%2C%20hh_id%2C%20mem_line%2C%20mem_role_operator%2C%20mem_units_run%20(blank%20unless%20the%20member%20runs%20a%20unit)%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205201%2C%201%2C%201%2C%202)%2C%20(%2201%22%2C%205201%2C%202%2C%200%2C%20None)%2C%20(%2204%22%2C%205201%2C%201%2C%200%2C%20None)%2C%20(%2204%22%2C%205201%2C%202%2C%201%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205202%2C%201%2C%201%2C%202)%2C%20(%2201%22%2C%205202%2C%202%2C%200%2C%20None)%2C%20(%2201%22%2C%205203%2C%201%2C%200%2C%20None)%2C%20(%2201%22%2C%205203%2C%202%2C%200%2C%20None)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%205204%2C%201%2C%201%2C%201)%2C%20(%2201%22%2C%205204%2C%202%2C%200%2C%20None)%2C%20(%2201%22%2C%205205%2C%201%2C%201%2C%201)%2C%20(%2201%22%2C%205205%2C%202%2C%201%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%205206%2C%201%2C%201%2C%20None)%2C%20(%2207%22%2C%205206%2C%202%2C%20None%2C%20None)%2C%20(%2207%22%2C%205207%2C%201%2C%201%2C%201)%2C%20(%2201%22%2C%205208%2C%201%2C%201%2C%201)%2C%0A%20%20%20%20%5D%0A%20%20%20%20roster%20%3D%20pd.DataFrame(rows%2C%20columns%3DHOUSEHOLD%20%2B%20%5B%22mem_line%22%2C%20%22mem_role_operator%22%2C%20%22mem_units_run%22%5D)%0A%20%20%20%20return%20roster.astype(%7B%22mem_role_operator%22%3A%20%22Int64%22%2C%20%22mem_units_run%22%3A%20%22Int64%22%7D)%2C%20found_units%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.2%22%2C%20match_operators%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20survey%3A%20a%20member%20who%20runs%20two%20units%2C%20a%20household%20id%20shared%20by%20regions%2001%20and%2004%2C%20a%20unit%20entered%20twice%2C%20blank%20flags%20and%20counts%22%2C%20small_roster())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%22%2C%20(members%2C%20units))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20members%20and%20units%22%2C%20(in_region(members%2C%20%2207%22)%2C%20in_region(units%2C%20%2207%22)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20HOUSEHOLD%20%2B%20%5B%22status%22%2C%20%22mem_line%22%2C%20%22operator_line%22%5D%2C%20operator_columns())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20each%20operator's%20distinct%20units%2C%20join%20each%20operator%20to%20its%20household's%20mem_line%2C%20test%20the%20line%2C%20then%20the%20flag%2C%20then%20the%20count%20(blanks%20filled%20with%200%20first)%3B%20then%20each%20flagged%20line%20against%20the%20operators.%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%20survey's%20operators%20that%20do%20not%20match%2C%20counted%20by%20status%0A%20%20%20%20def%20_try_operators()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20found%20%3D%20match_operators(members%2C%20units)%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%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bfound%5B%22status%22%5D.value_counts()%2C%20found%5D)%0A%0A%20%20%20%20_try_operators()%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.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%20*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20right%20dataset%3B%20not%20a%20many-to-one%20merge*%20%7C%20The%20join%20to%20the%20roster%20left%20out%20%60region_code%60%2C%20so%20regions%2001%20and%2004%20share%20household%205201's%20lines.%20%7C%20%60HOUSEHOLD%60%20in%20%60left_on%60%20and%20%60right_on%60%20%7C%0A%20%20%20%20%7C%20The%20small%20survey's%20household%205208%20is%20%60count%20differs%60%20%7C%20The%20unit%20entered%20twice%20was%20counted%20twice.%20%7C%20step%201%2C%20%60drop_duplicates()%60%20before%20%60size()%60%20%7C%0A%20%20%20%20%7C%20A%20mismatch%20listed%20twice%20with%20the%20same%20status%20%7C%20One%20row%20per%20unit%2C%20not%20per%20operator%3A%20the%20units%20were%20not%20counted%20per%20line.%20%7C%20step%201%2C%20%60groupby(OPERATOR)%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20invalid%20entry%201%20in%20condlist%3A%20should%20be%20boolean%20ndarray*%20(the%20number%20is%20the%20condition's%20place%20in%20the%20list%2C%20from%200)%20%7C%20A%20comparison%20with%20a%20blank%20flag%20or%20count%20gave%20%60%3CNA%3E%60%2C%20which%20%60np.select%60%20can't%20take%20as%20true%20or%20false.%20%7C%20%60fillna(0)%60%20before%20%60!%3D%60%20%7C%0A%20%20%20%20%7C%20Household%205206%20has%20no%20row%20%7C%20The%20blank%20count%20was%20left%20out%20of%20the%20comparison%20instead%20of%20counted%20as%20differing.%20%7C%20%60mem_units_run.fillna(0)%60%20%7C%0A%20%20%20%20%7C%20Members%20who%20run%20nothing%20are%20listed%20%7C%20The%20roster%20side%20kept%20every%20line%2C%20not%20only%20the%20flagged%20lines.%20%7C%20step%204%2C%20%60mem_role_operator%20%3D%3D%201%60%20%7C%0A%20%20%20%20%7C%20No%20%60flag%20names%20no%20unit%60%20rows%20%7C%20Only%20the%20first%20direction%20was%20run.%20%7C%20step%204%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%20The%20same%20step%20in%20DuckDB%20SQL%3A%20a%20%60LEFT%20JOIN%60%20and%20a%20%60CASE%20WHEN%60%20for%20the%20operators%2C%20an%20%60ANTI%20JOIN%60%20for%20the%20roster%20lines%2C%20and%20%60UNION%20ALL%20BY%20NAME%60%20to%20stack%20the%20two%2C%20which%20fills%20the%20columns%20a%20side%20lacks%20with%20%60NULL%60.%20SQL%20tests%20a%20blank%20with%20%60IS%20DISTINCT%20FROM%60%20and%20%60coalesce()%60%2C%20its%20%60fillna%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20operators_in_sql(members%3A%20pd.DataFrame%2C%20units%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%203.2%20in%20DuckDB%20SQL.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.register(%22members%22%2C%20members)%0A%20%20%20%20con.register(%22units%22%2C%20units)%0A%20%20%20%20out%20%3D%20con.execute(%22%22%22%0A%20%20%20%20%20%20%20%20WITH%20operators%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20region_code%2C%20hh_id%2C%20operator_line%2C%20count(*)%20AS%20units%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20(SELECT%20DISTINCT%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%20FROM%20units)%20GROUP%20BY%20ALL)%2C%0A%20%20%20%20%20%20%20%20by_operator%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20o.*%2C%20m.mem_line%2C%20m.mem_role_operator%2C%20m.mem_units_run%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CASE%20WHEN%20m.mem_line%20IS%20NULL%20THEN%20'no%20roster%20line'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHEN%20m.mem_role_operator%20IS%20DISTINCT%20FROM%201%20THEN%20'operator%20not%20flagged'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHEN%20coalesce(m.mem_units_run%2C%200)%20%3C%3E%20o.units%20THEN%20'count%20differs'%20END%20AS%20status%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20operators%20o%20LEFT%20JOIN%20members%20m%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20ON%20m.region_code%20%3D%20o.region_code%20AND%20m.hh_id%20%3D%20o.hh_id%20AND%20m.mem_line%20%3D%20o.operator_line)%2C%0A%20%20%20%20%20%20%20%20by_line%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20m.region_code%2C%20m.hh_id%2C%20m.mem_line%2C%20m.mem_role_operator%2C%20m.mem_units_run%2C%20'flag%20names%20no%20unit'%20AS%20status%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20members%20m%20ANTI%20JOIN%20operators%20o%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20ON%20o.region_code%20%3D%20m.region_code%20AND%20o.hh_id%20%3D%20m.hh_id%20AND%20o.operator_line%20%3D%20m.mem_line%0A%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20m.mem_role_operator%20%3D%201)%0A%20%20%20%20%20%20%20%20SELECT%20*%20FROM%20by_operator%20WHERE%20status%20IS%20NOT%20NULL%0A%20%20%20%20%20%20%20%20UNION%20ALL%20BY%20NAME%0A%20%20%20%20%20%20%20%20SELECT%20*%20FROM%20by_line%0A%20%20%20%20%20%20%20%20ORDER%20BY%20ALL%22%22%22).df()%0A%20%20%20%20con.close()%0A%20%20%20%20return%20out%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%20Did%20the%20first%20step%20find%20what%20was%20planted%3F%0A%0A%20%20%20%20A%20real%20survey%20comes%20with%20no%20list%20of%20its%20errors.%20This%20course's%20survey%20does%3A%20%60planted_defects.csv%60%20lists%20every%20defect%20the%20course%20planted%2C%20among%20them%20eight%20kinds%20of%20key%20error.%20The%20first%20step%20should%20find%20each%20one%20in%20the%20table%20it%20was%20planted%20in%3A%20a%20crew%20row%20keyed%20to%20a%20unit%20that%20does%20not%20exist%2C%20catch%20keyed%20astray%20(four%20kinds)%2C%20and%20an%20operator%20whose%20roster%20line%20is%20not%20flagged%20or%20counts%20another%20number%20of%20units.%20The%20eighth%2C%20a%20unit%20with%20no%20catch%2C%20is%20the%20other%20direction%20of%20the%20catch%2C%20which%20section%202%20lists%3B%20the%20cell%20below%20counts%20it%20with%20the%20same%20anti-join%2C%20turned%20around%2C%20so%20the%20table%20holds%20every%20kind.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(CHILDREN%2C%20children)%3A%0A%20%20%20%20%23%20Every%20household%20the%20first%20step%20flags%2C%20and%20the%20defects%20the%20course%20planted%20there%0A%20%20%20%20_key_kinds%20%3D%20%5B%22crew_key_astray%22%2C%20%22catch_key_one_part_off%22%2C%20%22catch_rows_all_astray%22%2C%20%22catch_key_both_parts%22%2C%20%22catch_key_two_candidates%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22unit_without_catch%22%2C%20%22operator_count_differs%22%2C%20%22operator_flag_missing%22%5D%0A%20%20%20%20_findings%20%3D%20%5Borphans_in_sql(children%5B_name%5D%2C%20units%2C%20_prefix)%5BHOUSEHOLD%5D.assign(finding%3Df%22orphan%20in%20%7B_name%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20_name%2C%20_prefix%20in%20CHILDREN.items()%5D%0A%20%20%20%20_operators%20%3D%20operators_in_sql(members%2C%20units)%0A%20%20%20%20_findings.append(_operators%5BHOUSEHOLD%5D.assign(finding%3D%22operator%3A%20%22%20%2B%20_operators%5B%22status%22%5D))%0A%20%20%20%20_catch_keys%20%3D%20catch.rename(columns%3D%7Bf%22catch_%7Bpart%7D%22%3A%20part%20for%20part%20in%20PARTS%7D)%5BKEY%5D%0A%20%20%20%20_findings.append(units%5BKEY%5D.merge(_catch_keys%2C%20on%3DKEY%2C%20how%3D%22left_anti%22)%5BHOUSEHOLD%5D.assign(finding%3D%22unit%20with%20no%20catch%22))%0A%20%20%20%20_found%20%3D%20pd.concat(_findings).drop_duplicates()%0A%20%20%20%20_planted%20%3D%20course.planted_defects()%0A%20%20%20%20_planted%20%3D%20_planted%5B_planted%5B%22kind%22%5D.isin(_key_kinds)%5D%5BHOUSEHOLD%20%2B%20%5B%22kind%22%5D%5D.drop_duplicates()%0A%20%20%20%20_both%20%3D%20_planted.merge(_found%2C%20on%3DHOUSEHOLD%2C%20how%3D%22outer%22)%0A%20%20%20%20_both%5B%22kind%22%5D%20%3D%20_both%5B%22kind%22%5D.fillna(%22not%20planted%22)%0A%20%20%20%20_both%5B%22finding%22%5D%20%3D%20_both%5B%22finding%22%5D.fillna(%22not%20found%22)%0A%20%20%20%20_cases%20%3D%20_planted.merge(_found%5BHOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20_missed_kinds%20%3D%20sorted(set(_key_kinds)%20-%20set(_cases.loc%5B_cases%5B%22_merge%22%5D%20%3D%3D%20%22both%22%2C%20%22kind%22%5D))%0A%20%20%20%20_unplanted%20%3D%20_both.loc%5B_both%5B%22kind%22%5D%20%3D%3D%20%22not%20planted%22%2C%20HOUSEHOLD%5D.drop_duplicates()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.crosstab(_both%5B%22kind%22%5D%2C%20_both%5B%22finding%22%5D)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7B(_cases%5B'_merge'%5D%20%3D%3D%20'both').sum()%7D%20of%20the%20%7Blen(_cases)%7D%20planted%20key%20errors%20found**%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B'every%20one%20of%20the%20eight%20kinds%20among%20them'%20if%20not%20_missed_kinds%20else%20'kinds%20missed%3A%20'%20%2B%20'%2C%20'.join(_missed_kinds)%7D%3B%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7Blen(_unplanted)%7D%20households%20flagged**%20that%20the%20list%20does%20not%20name.%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%20Each%20kind%20lands%20in%20the%20column%20of%20the%20check%20meant%20for%20it.%20A%20household%20whose%20every%20catch%20row%20points%20elsewhere%20shows%20twice%2C%20as%20a%20catch%20orphan%20and%20as%20a%20unit%20with%20no%20catch%3A%20the%20two%20sides%20of%20one%20mismatch%2C%20which%20section%203%20pairs.%0A%0A%20%20%20%20%23%23%202%20%C2%B7%20Orphan%20rows%20both%20ways%3A%20anti-joins%2C%20semi-joins%2C%20merge's%20indicator%0A%0A%20%20%20%20**The%20problem.**%20The%20second%20step%20takes%20the%20catch%2C%20the%20table%20every%20unit%20must%20have%20rows%20in%20(rule%20C3)%2C%20and%20runs%20the%20anti-join%20both%20ways.%20A%20table%20of%20units%20by%20species%2C%20such%20as%20module%205's%20top%20species%2C%20joins%20the%20catch%20to%20the%20units%20on%20the%20key.%20A%20catch%20row%20whose%20key%20matches%20no%20unit%20drops%20out%20of%20that%20join%2C%20so%20its%20species%20is%20counted%20for%20nobody.%20A%20unit%20whose%20key%20matches%20no%20catch%20row%20looks%20like%20a%20unit%20that%20caught%20nothing.%20Neither%20raises%20an%20error.%20This%20section%20lists%20both%20kinds%2C%20with%20the%20counts%20an%20editor%20needs%20to%20judge%20them.%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%20A%20semi-join%20is%20not%20an%20inner%20join%0A%0A%20%20%20%20%22Which%20units%20have%20at%20least%20one%20catch%20row%3F%22%20sounds%20like%20an%20inner%20join.%20It%20isn't%20one.%20An%20inner%20join%20repeats%20each%20unit%20once%20for%20every%20catch%20row%20that%20matches%20it%2C%20so%20a%20unit%20with%20six%20species%20comes%20back%20six%20times.%20A%20semi-join%20keeps%20each%20unit%20once.%20pandas%20has%20no%20%60how%3D%22semi%22%60%3B%20join%20to%20the%20other%20table's%20**distinct**%20keys%2C%20so%20that%20every%20key%20matches%20at%20most%20once.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Units%20that%20have%20catch%3A%20an%20inner%20join%20on%20every%20catch%20row%2C%20then%20on%20the%20distinct%20catch%20keys%0A%20%20%20%20_catch_keys%20%3D%20catch.rename(columns%3D%7B%22catch_operator_line%22%3A%20%22operator_line%22%2C%20%22catch_unit_no%22%3A%20%22unit_no%22%7D)%5BKEY%5D%0A%20%20%20%20_inner%20%3D%20units.merge(_catch_keys%2C%20on%3DKEY)%0A%20%20%20%20_semi%20%3D%20units.merge(_catch_keys.drop_duplicates()%2C%20on%3DKEY%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20%7C%20rows%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20units%20%7C%20%7Blen(units)%3A%2C%7D%20%7C%0A%20%20%20%20%7C%20inner%20join%20to%20every%20catch%20row%20%7C%20**%7Blen(_inner)%3A%2C%7D**%3A%20each%20unit%20repeated%20once%20per%20species%20%7C%0A%20%20%20%20%7C%20join%20to%20the%20distinct%20catch%20keys%20(a%20semi-join)%20%7C%20**%7Blen(_semi)%3A%2C%7D**%3A%20each%20unit%20with%20catch%2C%20once%20%7C%0A%0A%20%20%20%20%60validate%3D%22many_to_one%22%60%20writes%20the%20expectation%20into%20the%20second%20join%3A%20each%20catch%20key%20at%20most%20once%20on%20the%20right.%20If%20one%20were%20still%20repeated%2C%20pandas%20would%20stop%20with%20a%20*MergeError*%20instead%20of%20multiplying%20rows.%20(%60%22one_to_one%22%60%20would%20stop%20too%2C%20for%20another%20reason%3A%20module%204's%20duplicates%20include%20a%20unit%20entered%20twice%2C%20so%20a%20key%20can%20repeat%20on%20the%20left.)%20The%20difference%20between%20the%20first%20row%20and%20the%20last%2C%20%7Blen(units)%20-%20len(_semi)%3A%2C%7D%2C%20is%20the%20units%20with%20no%20catch%20row%2C%20the%20other%20kind%20of%20orphan.%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%20Both%20directions%20in%20one%20join%3A%20%60indicator%3DTrue%60%0A%0A%20%20%20%20An%20outer%20join%20of%20the%20two%20tables'%20distinct%20keys%20keeps%20every%20key%20of%20either%20side.%20%60indicator%3DTrue%60%20adds%20a%20column%20%60_merge%60%20that%20says%20where%20each%20key%20was%20found%3A%20%60both%60%2C%20%60left_only%60%20(in%20the%20left%20table%20only)%20or%20%60right_only%60.%20With%20the%20units%20on%20the%20left%2C%20%60left_only%60%20is%20a%20unit%20with%20no%20catch%20and%20%60right_only%60%20a%20catch%20key%20with%20no%20unit.%20One%20join%20and%20one%20%60value_counts()%60%20give%20the%20whole%20picture.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Every%20distinct%20key%20of%20either%20table%2C%20and%20where%20it%20was%20found%0A%20%20%20%20_catch_keys%20%3D%20catch.rename(columns%3D%7B%22catch_operator_line%22%3A%20%22operator_line%22%2C%20%22catch_unit_no%22%3A%20%22unit_no%22%7D)%5BKEY%5D.drop_duplicates()%0A%20%20%20%20_both%20%3D%20units%5BKEY%5D.drop_duplicates().merge(_catch_keys%2C%20on%3DKEY%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%0A%20%20%20%20_both%5B%22_merge%22%5D.value_counts()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20you%20know%20pandas%202%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20-%20**Anti-joins%20are%20new%20in%20pandas%203%3A**%20%60how%3D%22left_anti%22%60%20keeps%20the%20left%20rows%20with%20no%20match%20and%20%60how%3D%22right_anti%22%60%20the%20right%20ones.%20In%20pandas%202%20the%20same%20took%20%60indicator%3DTrue%60%20and%20a%20filter%20on%20%60_merge%20%3D%3D%20%22left_only%22%60%2C%20which%20still%20works.%0A%20%20%20%20-%20**%60_merge%60%20is%20a%20categorical%20column**%20with%20the%20three%20categories%20%60left_only%60%2C%20%60right_only%60%20and%20%60both%60%2C%20in%20pandas%202%20and%203%20alike.%20%60.map(%7B...%7D)%60%20turns%20it%20into%20words%20of%20your%20own%3B%20%60.astype(str)%60%20into%20plain%20text.%0A%20%20%20%20-%20**Text%20columns%20are%20%60str%60%20now.**%20Two%20key%20columns%20joined%20must%20have%20the%20same%20type%3A%20text%20with%20text%2C%20numbers%20with%20numbers.%20pandas%203%20refuses%20a%20text%20key%20against%20a%20number%20key%20with%20the%20*ValueError*%20above%2C%20as%20pandas%202%20did.%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%203.3.%20Catch%20keys%20with%20no%20unit%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20first%20list%20of%20the%20second%20step.%20Each%20line%20is%20a%20catch%20key%20the%20survey's%20units%20do%20not%20have%2C%20with%20how%20many%20catch%20rows%20carry%20it%2C%20so%20the%20editor%20sees%20whether%20one%20species%20or%20a%20whole%20unit's%20catch%20is%20astray.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20catch%20table%20as%20stored%20(key%20parts%20%60catch_operator_line%60%2C%20%60catch_unit_no%60)%20and%20the%20units%20%E2%86%92%20**one%20row%20per%20distinct%20catch%20key%20that%20matches%20no%20unit**%2C%20with%20the%20key%20under%20the%20unit's%20names%20and%20%60catch_rows%60%2C%20the%20number%20of%20catch%20rows%20that%20carry%20it%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20unit_no%20%7C%20catch_rows%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%202%20%7C%201%20%7C%201%20%7C%0A%20%20%20%20%7C%2001%20%7C%205102%20%7C%202%20%7C%203%20%7C%202%20%7C%0A%20%20%20%20%7C%2004%20%7C%205101%20%7C%201%20%7C%201%20%7C%201%20%7C%0A%0A%20%20%20%20The%20last%20row%20is%20the%20region%20trap%3A%20region%2001's%20household%205101%20has%20a%20unit%20with%20the%20key%201%2C%201%2C%20and%20region%2004's%20household%205101%20does%20not.%20Without%20%60region_code%60%20in%20the%20key%2C%20that%20catch%20row%20would%20find%20region%2001's%20unit.%0A%0A%20%20%20%20**Tools%3A**%20%60rename(columns%3D%7B...%7D)%60%2C%20%60groupby(KEY).size()%60%2C%20and%20%60merge(...%2C%20how%3D%22left_anti%22)%60%2C%20all%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20orphan_catch(catch%3A%20pd.DataFrame%2C%20units%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20distinct%20catch%20key%20that%20matches%20no%20unit%3A%20the%20key%20(unit's%20names)%20and%20catch_rows.%22%22%22%0A%20%20%20%20%23%20Exercise%203.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20rename%20the%20catch%20table's%20key%20parts%20to%20the%20unit's%20names%20(catch_operator_line%20-%3E%20operator_line%2C%20and%20catch_unit_no)%0A%20%20%20%20%23%20Step%202%3A%20count%20the%20catch%20rows%20of%20each%20key%3A%20groupby(KEY).size()%2C%20named%20%22catch_rows%22%2C%20reset_index()%0A%20%20%20%20%23%20Step%203%3A%20keep%20the%20keys%20that%20match%20no%20unit%3A%20merge%20with%20units%5BKEY%5D%2C%20how%3D%22left_anti%22%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_units%2C%20_catch%20%3D%20small_survey()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.3%22%2C%20orphan_catch%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20survey%3A%20a%20household%20id%20shared%20by%20regions%2001%20and%2004%2C%20a%20key%20on%20two%20catch%20rows%22%2C%20(_catch%2C%20_units))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%22%2C%20(catch%2C%20units))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20catch%20and%20units%22%2C%20(in_region(catch%2C%20%2204%22)%2C%20in_region(units%2C%20%2204%22)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20KEY%2C%20KEY%20%2B%20%5B%22catch_rows%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Rename%20the%20two%20catch_%20parts%2C%20count%20rows%20per%20key%2C%20then%20keep%20the%20keys%20an%20anti-join%20finds%20in%20no%20unit%2C%20region_code%20included.%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%20survey's%20catch%20keys%20with%20no%20unit%0A%20%20%20%20try_it(orphan_catch%2C%20catch%2C%20units)%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.3%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%20*KeyError%3A%20'operator_line'*%20%7C%20The%20catch%20table's%20parts%20still%20start%20with%20%60catch_%60.%20%7C%20step%201%2C%20%60rename%60%20%7C%0A%20%20%20%20%7C%20A%20key%20repeated%2C%20or%20no%20%60catch_rows%60%20column%20%7C%20Catch%20rows%20kept%2C%20not%20counted%20per%20key.%20%7C%20step%202%2C%20%60groupby(KEY).size()%60%20%7C%0A%20%20%20%20%7C%20The%20small%20survey's%20region%2004%20row%20is%20missing%20%7C%20The%20key%20left%20out%20%60region_code%60%2C%20so%20region%2004's%20catch%20found%20region%2001's%20unit.%20%7C%20%60KEY%60%20includes%20%60region_code%60%20%7C%0A%20%20%20%20%7C%20Every%20catch%20key%20comes%20back%20%7C%20An%20anti-join%20on%20the%20wrong%20columns%2C%20or%20an%20outer%20join%20without%20a%20filter.%20%7C%20%60how%3D%22left_anti%22%60%2C%20%60on%3DKEY%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20You%20are%20trying%20to%20merge%20on%20str%20and%20int64%20columns%20for%20key%20'region_code'*%20%7C%20%60region_code%60%20is%20text%20on%20one%20side%20and%20a%20number%20on%20the%20other.%20%7C%20where%20the%20tables%20were%20read%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%203.4.%20Every%20key%20and%20where%20it%20was%20found%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20key%20check%20reports%20both%20directions%20at%20once%3A%20how%20many%20keys%20matched%2C%20how%20many%20units%20have%20no%20catch%2C%20how%20many%20catch%20keys%20have%20no%20unit.%20One%20outer%20join%20with%20%60indicator%3DTrue%60%20gives%20all%20three%2C%20and%20the%20units%20with%20no%20catch%20are%20the%20list%20the%20next%20section%20starts%20from.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20units%20and%20the%20catch%20table%20as%20stored%20%E2%86%92%20**one%20row%20per%20distinct%20key%20of%20either%20table**%2C%20with%20the%20key%20and%20%60status%60%2C%20one%20of%20three%20words%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20unit_no%20%7C%20status%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%201%20%7C%201%20%7C%20matched%20%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%202%20%7C%201%20%7C%20catch%20only%20%7C%0A%20%20%20%20%7C%2001%20%7C%205104%20%7C%203%20%7C%201%20%7C%20unit%20only%20%7C%0A%0A%20%20%20%20A%20unit%20entered%20twice%20is%20one%20key%2C%20so%20it%20is%20one%20row.%0A%0A%20%20%20%20**Tools%3A**%20%60drop_duplicates()%60%2C%20%60merge(...%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%60%20and%20%60.map(%7B...%7D)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20key_status(units%3A%20pd.DataFrame%2C%20catch%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20distinct%20key%20of%20either%20table%3A%20the%20key%20and%20status%20(matched%2C%20unit%20only%2C%20catch%20only).%22%22%22%0A%20%20%20%20%23%20Exercise%203.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20units'%20distinct%20keys%3A%20units%5BKEY%5D.drop_duplicates()%0A%20%20%20%20%23%20Step%202%3A%20the%20catch%20table's%20distinct%20keys%2C%20its%20catch_%20parts%20renamed%20to%20the%20unit's%20names%0A%20%20%20%20%23%20Step%203%3A%20an%20outer%20merge%20of%20the%20two%20on%20KEY%20with%20indicator%3DTrue%0A%20%20%20%20%23%20Step%204%3A%20status%20from%20_merge%3A%20both%20-%3E%20%22matched%22%2C%20left_only%20-%3E%20%22unit%20only%22%2C%20right_only%20-%3E%20%22catch%20only%22%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20drop%20_merge%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.4%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_units%2C%20_catch%20%3D%20small_survey()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.4%22%2C%20key_status%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20survey%3A%20a%20unit%20entered%20twice%2C%20a%20household%20with%20catch%20and%20no%20unit%22%2C%20(_units%2C%20_catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%22%2C%20(units%2C%20catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20units%20and%20catch%22%2C%20(in_region(units%2C%20%2201%22)%2C%20in_region(catch%2C%20%2201%22)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20KEY%2C%20KEY%20%2B%20%5B%22status%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Distinct%20keys%20on%20both%20sides%20(catch%20renamed)%2C%20an%20outer%20merge%20with%20indicator%3DTrue%2C%20then%20_merge%20mapped%20to%20matched%2C%20unit%20only%2C%20catch%20only.%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%20how%20many%20keys%20of%20each%20kind%20the%20survey%20has%0A%20%20%20%20def%20_try_status()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20status%20%3D%20key_status(units%2C%20catch)%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%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bstatus%5B%22status%22%5D.value_counts()%2C%20status%5Bstatus%5B%22status%22%5D%20!%3D%20%22matched%22%5D.head(6)%5D)%0A%0A%20%20%20%20_try_status()%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.4%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%20Thousands%20of%20%60matched%60%20rows%20more%20than%20there%20are%20units%20%7C%20The%20catch%20keys%20were%20not%20made%20distinct%2C%20so%20each%20species%20is%20a%20row.%20%7C%20step%202%2C%20%60drop_duplicates()%60%20%7C%0A%20%20%20%20%7C%20The%20small%20survey's%20household%205105%20appears%20twice%20%7C%20The%20unit%20entered%20twice%20was%20not%20made%20distinct.%20%7C%20step%201%20%7C%0A%20%20%20%20%7C%20%60left_only%60%20and%20%60right_only%60%20in%20the%20result%20%7C%20%60_merge%60%20not%20mapped%20to%20the%20three%20words.%20%7C%20step%204%2C%20%60.map(%7B...%7D)%60%20%7C%0A%20%20%20%20%7C%20%60unit%20only%60%20and%20%60catch%20only%60%20swapped%20%7C%20The%20tables%20in%20the%20other%20order%3A%20the%20left%20table%20is%20the%20units.%20%7C%20%60unit_keys.merge(catch_keys%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20A%20column%20named%20%60_merge%60%20in%20the%20result%20%7C%20It%20was%20kept%20after%20%60status%60%20was%20made.%20%7C%20%60drop(columns%3D%22_merge%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%203%20%C2%B7%20Keys%20of%20several%20parts%3A%20which%20part%20differs%0A%0A%20%20%20%20**The%20problem.**%20An%20orphan%20says%20*that*%20a%20key%20is%20wrong%2C%20not%20*which%20part*%20is%20wrong%20or%20what%20the%20right%20value%20is.%20The%20region%20and%20the%20household%20are%20rarely%20the%20culprits%3A%20the%20catch%20row%20sits%20in%20the%20household's%20own%20pages%20of%20the%20questionnaire.%20One%20of%20the%20two%20parts%20below%20them%20was%20keyed%20wrong.%20To%20correct%20it%2C%20the%20editor%20needs%20the%20part%2C%20and%20the%20value%20it%20should%20have.%0A%0A%20%20%20%20**The%20R%20way%20pastes%20the%20key%20into%20one%20text**%20(%60paste(hh_id%2C%20operator_line%2C%20unit_no)%60)%20and%20compares%20texts.%20That%20hides%20which%20part%20differs%2C%20and%20a%20paste%20without%20a%20separator%20can%20make%20two%20different%20keys%20into%20one%20text%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Two%20different%20keys%20(line%201%2C%20unit%2012%20and%20line%2011%2C%20unit%202)%20pasted%20with%20no%20separator%20become%20one%20text%0A%20%20%20%20_two%20%3D%20pd.DataFrame(%7B%22operator_line%22%3A%20%5B1%2C%2011%5D%2C%20%22unit_no%22%3A%20%5B12%2C%202%5D%7D)%0A%20%20%20%20_two%5B%22pasted%22%5D%20%3D%20_two%5B%22operator_line%22%5D.astype(str)%20%2B%20_two%5B%22unit_no%22%5D.astype(str)%0A%20%20%20%20mo.hstack(%5B_two%2C%20mo.md(f%22Two%20keys%2C%20**%7B_two%5B'pasted'%5D.nunique()%7D%20pasted%20text**.%20A%20join%20on%20the%20text%20would%20match%20them.%20A%20join%20on%20the%20list%20of%20columns%2C%20%60on%3DKEY%60%2C%20compares%20part%20by%20part%20and%20never%20needs%20the%20paste.%22)%5D%2C%20justify%3D%22start%22%2C%20gap%3D2%2C%20align%3D%22center%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%20pandas%2C%20polars%20and%20SQL%20all%20join%20on%20a%20list%20of%20columns%2C%20so%20the%20parts%20stay%20apart%2C%20keep%20their%20types%2C%20and%20can%20be%20compared%20one%20by%20one.%0A%0A%20%20%20%20%23%23%23%20The%20leftovers%20this%20section%20starts%20from%0A%0A%20%20%20%20Exercises%203.3%20and%203.4%20found%20the%20two%20lists%20of%20leftovers%3A%20the%20catch%20keys%20with%20no%20unit%20(%60orphans%60)%20and%20the%20units%20with%20no%20catch%20(%60without_catch%60).%20The%20function%20below%20finds%20them%20again%20in%20DuckDB%20SQL%2C%20so%20this%20section%20starts%20from%20the%20same%20two%20tables%20whatever%20your%20answers%20above.%20%60ANTI%20JOIN%20...%20ON%60%20lets%20the%20catch%20table%20keep%20its%20own%20column%20names.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20leftovers_in_sql(units%3A%20pd.DataFrame%2C%20catch%3A%20pd.DataFrame)%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Exercises%203.3%20and%203.4%20in%20DuckDB%20SQL%3A%20(orphans%2C%20without_catch).%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.register(%22units%22%2C%20units)%0A%20%20%20%20con.register(%22catch%22%2C%20catch)%0A%20%20%20%20orphans%20%3D%20con.execute(%22%22%22%0A%20%20%20%20%20%20%20%20WITH%20catch_keys%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20region_code%2C%20hh_id%2C%20catch_operator_line%20AS%20operator_line%2C%20catch_unit_no%20AS%20unit_no%2C%20count(*)%20AS%20catch_rows%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20catch%20GROUP%20BY%20ALL)%0A%20%20%20%20%20%20%20%20SELECT%20*%20FROM%20catch_keys%20ANTI%20JOIN%20units%20USING%20(region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no)%0A%20%20%20%20%20%20%20%20ORDER%20BY%20ALL%22%22%22).df()%0A%20%20%20%20without_catch%20%3D%20con.execute(%22%22%22%0A%20%20%20%20%20%20%20%20SELECT%20DISTINCT%20u.region_code%2C%20u.hh_id%2C%20u.operator_line%2C%20u.unit_no%0A%20%20%20%20%20%20%20%20FROM%20units%20u%20ANTI%20JOIN%20catch%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20ON%20c.region_code%20%3D%20u.region_code%20AND%20c.hh_id%20%3D%20u.hh_id%0A%20%20%20%20%20%20%20%20%20%20%20AND%20c.catch_operator_line%20%3D%20u.operator_line%20AND%20c.catch_unit_no%20%3D%20u.unit_no%0A%20%20%20%20%20%20%20%20ORDER%20BY%20ALL%22%22%22).df()%0A%20%20%20%20con.close()%0A%20%20%20%20return%20orphans%2C%20without_catch%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20survey's%20two%20lists%20of%20leftovers%0A%20%20%20%20orphans%2C%20without_catch%20%3D%20leftovers_in_sql(units%2C%20catch)%0A%20%20%20%20mo.md(f%22**%7Blen(orphans)%7D%20catch%20keys**%20match%20no%20unit%2C%20over%20%7Borphans%5B'catch_rows'%5D.sum()%7D%20catch%20rows%3B%20**%7Blen(without_catch)%7D%20units**%20have%20no%20catch.%22)%0A%20%20%20%20return%20orphans%2C%20without_catch%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%20Pairing%20leftovers%20within%20a%20household%0A%0A%20%20%20%20The%20R%20way%20lines%20the%20two%20lists%20up%20household%20by%20household.%20It%20numbers%20each%20side's%20leftover%20rows%20within%20the%20household%20(%60group_by()%60%20and%20%60row_number()%60)%2C%20joins%20the%20two%20sides%20on%20the%20household%20and%20that%20number%20(%60full_join()%60)%2C%20and%20labels%20each%20pair%20by%20which%20sides%20are%20there%3A%0A%0A%20%20%20%20%7C%20label%20%7C%20what%20it%20says%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60unit%20and%20catch%60%20%7C%20a%20unit%20with%20no%20catch%2C%20and%20a%20stray%20catch%20key%20in%20the%20same%20household%3A%20most%20likely%20the%20same%20unit%2C%20keyed%20differently%20%7C%0A%20%20%20%20%7C%20%60catch%20only%60%20%7C%20a%20stray%20catch%20key%2C%20and%20no%20unit%20left%20over%3A%20the%20household's%20units%20all%20have%20catch%2C%20and%20this%20key%20points%20elsewhere%20%7C%0A%20%20%20%20%7C%20%60unit%20only%60%20%7C%20a%20unit%20with%20no%20catch%2C%20and%20nothing%20stray%20to%20pair%20it%20with%3A%20its%20catch%20was%20never%20entered%2C%20or%20was%20deleted%20%7C%0A%0A%20%20%20%20In%20pandas%2C%20%60groupby(...).cumcount()%60%20numbers%20rows%20within%20each%20group%20from%200%2C%20so%20%60%2B%201%60%20gives%20R's%20%60row_number()%60.%20Sort%20first%3A%20the%20numbers%20follow%20the%20rows'%20order.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20side%20%3D%20without_catch%5BKEY%5D.sort_values(KEY)%0A%20%20%20%20side%5B%22pair%22%5D%20%3D%20side.groupby(HOUSEHOLD).cumcount()%20%2B%201%20%20%20%20%20%23%201%2C%202%2C%20...%20within%20each%20household%0A%20%20%20%20%60%60%60%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%203.5.%20Leftovers%20paired%2C%20with%20their%20label%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20pairs%20are%20how%20an%20editor%20reads%20a%20key%20check%20household%20by%20household%2C%20and%20%60unit%20only%60%2C%20a%20unit%20whose%20catch%20is%20missing%2C%20is%20found%20nowhere%20else.%20The%20label%20in%20words%20reads%20without%20a%20legend.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20orphan%20catch%20keys%20(Exercise%203.3's%20table)%20and%20the%20units%20with%20no%20catch%20(Exercise%203.4's%20%60unit%20only%60%20keys)%20%E2%86%92%20one%20row%20per%20pair%2C%20with%20the%20household%2C%20%60pair%60%2C%20the%20unit's%20two%20parts%20(suffix%20%60_unit%60)%2C%20the%20catch's%20two%20parts%20(suffix%20%60_catch%60)%2C%20and%20%60label%60%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20pair%20%7C%20operator_line_unit%20%7C%20unit_no_unit%20%7C%20operator_line_catch%20%7C%20unit_no_catch%20%7C%20label%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%201%20%7C%20%7C%20%7C%202%20%7C%201%20%7C%20catch%20only%20%7C%0A%20%20%20%20%7C%2001%20%7C%205102%20%7C%201%20%7C%201%20%7C%201%20%7C%202%20%7C%203%20%7C%20unit%20and%20catch%20%7C%0A%20%20%20%20%7C%2001%20%7C%205104%20%7C%201%20%7C%203%20%7C%201%20%7C%20%7C%20%7C%20unit%20only%20%7C%0A%0A%20%20%20%20Number%20each%20side's%20rows%20within%20its%20household%20**in%20key%20order**%2C%20from%201.%0A%0A%20%20%20%20**Tools%3A**%20%60sort_values%60%2C%20%60groupby(HOUSEHOLD).cumcount()%60%2C%20%60merge(...%2C%20how%3D%22outer%22%2C%20suffixes%3D(...))%60%2C%20and%20%60np.where%60%2C%20which%20picks%20a%20value%20by%20a%20condition%3B%20one%20%60np.where%60%20inside%20another%20picks%20from%20three.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20pair_leftovers(orphans%3A%20pd.DataFrame%2C%20without_catch%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Leftovers%20paired%20by%20position%20within%20each%20household%3A%20the%20household%2C%20pair%2C%20the%20unit's%20and%20the%20catch's%20parts%0A%20%20%20%20(suffixes%20_unit%20and%20_catch)%2C%20and%20the%20label%20(unit%20and%20catch%2C%20unit%20only%2C%20catch%20only).%22%22%22%0A%20%20%20%20%23%20Exercise%203.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20unit%20side%3A%20without_catch%5BKEY%5D%20sorted%20by%20KEY%2C%20with%20pair%20%3D%20cumcount()%20within%20HOUSEHOLD%2C%20%2B%201%0A%20%20%20%20%23%20Step%202%3A%20the%20catch%20side%3A%20orphans%5BKEY%5D%20sorted%20by%20KEY%2C%20numbered%20the%20same%20way%0A%20%20%20%20%23%20Step%203%3A%20an%20outer%20merge%20of%20the%20two%20sides%20on%20HOUSEHOLD%20%2B%20%5B%22pair%22%5D%2C%20suffixes%3D(%22_unit%22%2C%20%22_catch%22)%0A%20%20%20%20%23%20Step%204%3A%20label%3A%20%22unit%20and%20catch%22%20where%20both%20sides%20are%20present%2C%20else%20%22unit%20only%22%20where%20the%20unit%20side%20is%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20else%20%22catch%20only%22%20(a%20side%20is%20present%20where%20its%20unit_no%20is%20not%20blank)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.5%22)%0A%0A%0A%40app.function%0Adef%20paired_columns()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20columns%20Exercise%203.5%20returns.%22%22%22%0A%20%20%20%20return%20HOUSEHOLD%20%2B%20%5B%22pair%22%5D%20%2B%20%5Bf%22%7Bpart%7D_unit%22%20for%20part%20in%20PARTS%5D%20%2B%20%5Bf%22%7Bpart%7D_catch%22%20for%20part%20in%20PARTS%5D%20%2B%20%5B%22label%22%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(orphans%2C%20without_catch)%3A%0A%20%20%20%20_small_orphans%2C%20_small_without%20%3D%20leftovers_in_sql(*small_survey())%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.5%22%2C%20pair_leftovers%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20survey%3A%20a%20household%20with%20two%20units%20without%20catch%20and%20one%20stray%20catch%20key%22%2C%20(_small_orphans%2C%20_small_without))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20leftovers%22%2C%20(orphans%2C%20without_catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20leftovers%22%2C%20(in_region(orphans%2C%20%2207%22)%2C%20in_region(without_catch%2C%20%2207%22)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20lists%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20HOUSEHOLD%20%2B%20%5B%22pair%22%5D%2C%20paired_columns())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Number%20each%20side%20within%20the%20household%20after%20sorting%20by%20KEY%2C%20outer-merge%20on%20HOUSEHOLD%20%2B%20pair%2C%20and%20label%20each%20pair%20by%20which%20side%20is%20present.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(orphans%2C%20without_catch)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20pairs%2C%20and%20how%20many%20of%20each%20label%0A%20%20%20%20def%20_try_pairs()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20pairs%20%3D%20pair_leftovers(orphans%2C%20without_catch)%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%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bpairs%5B%22label%22%5D.value_counts()%2C%20pairs.head(8)%5D)%0A%0A%20%20%20%20_try_pairs()%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.5%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%20No%20%60catch%20only%60%20or%20%60unit%20only%60%20rows%20%7C%20An%20inner%20join%20keeps%20only%20the%20pairs%20found%20on%20both%20sides.%20%7C%20%60how%3D%22outer%22%60%20%7C%0A%20%20%20%20%7C%20%60pair%60%20starts%20at%200%20%7C%20%60cumcount()%60%20counts%20from%200.%20%7C%20%60%2B%201%60%20%7C%0A%20%20%20%20%7C%20%60pair%60%20keeps%20growing%20across%20households%20%7C%20Numbered%20over%20the%20whole%20table%2C%20not%20within%20each%20household.%20%7C%20%60groupby(HOUSEHOLD).cumcount()%60%20%7C%0A%20%20%20%20%7C%20The%20small%20survey's%20household%205108%20pairs%20the%20other%20unit%20%7C%20The%20sides%20were%20not%20sorted%20by%20key%20before%20numbering.%20%7C%20%60sort_values(KEY)%60%20%7C%0A%20%20%20%20%7C%20Columns%20%60operator_line_x%60%20and%20%60operator_line_y%60%20%7C%20The%20default%20suffixes.%20%7C%20%60suffixes%3D(%22_unit%22%2C%20%22_catch%22)%60%20%7C%0A%20%20%20%20%7C%20Every%20one-sided%20pair%20is%20%60catch%20only%60%20%7C%20The%20inner%20%60np.where%60%20tested%20the%20catch%20side%2C%20which%20is%20blank%20on%20those%20rows%2C%20instead%20of%20the%20unit%20side.%20%7C%20step%204%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%20What%20position%20cannot%20tell%20you%0A%0A%20%20%20%20Pairing%20by%20position%20works%20when%20a%20household%20has%20at%20most%20one%20leftover%20on%20each%20side.%20With%20two%2C%20it%20pairs%20the%20first%20with%20the%20first%2C%20whether%20they%20belong%20together%20or%20not%3A%20in%20the%20small%20survey%2C%20household%205108's%20stray%20catch%20key%20has%20%60operator_line%20%3D%201%60%2C%20and%20position%20pairs%20it%20with%20unit%20(1%2C%201)%20only%20because%20that%20unit%20sorts%20first.%20And%20a%20%60catch%20only%60%20row%20has%20no%20counterpart%20at%20all%2C%20so%20the%20label%20cannot%20say%20which%20part%20of%20its%20key%20is%20wrong.%0A%0A%20%20%20%20**The%20better%20tool%20compares%20each%20stray%20catch%20key%20with%20every%20unit%20of%20its%20household.**%20That%20is%20a%20join%20on%20the%20household%20alone%2C%20so%20each%20orphan%20meets%20each%20of%20its%20household's%20units%2C%20and%20a%20household%20with%20three%20units%20gives%20three%20rows%20per%20orphan.%20A%20join%20that%20repeats%20rows%20on%20purpose%20says%20so%3A%20%60validate%3D%22many_to_many%22%60%20records%20the%20intent%2C%20where%20a%20join%20that%20should%20not%20repeat%20rows%20says%20%60%22many_to_one%22%60.%20Then%20each%20part%20is%20compared%20on%20its%20own.%20A%20unit%20one%20part%20away%20from%20the%20stray%20key%20is%20a%20likely%20home%20for%20it%3A%20a%20typo%20in%20one%20part.%20A%20key%20two%20parts%20away%20from%20every%20unit%20may%20be%20another%20unit%20altogether.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20both%20%3D%20orphans%5BKEY%5D.merge(unit_keys%2C%20on%3DHOUSEHOLD%2C%20suffixes%3D(%22%5C%22%2C%20%22_unit%22)%2C%20validate%3D%22many_to_many%22)%0A%20%20%20%20both%5B%22unit_no%22%5D%20!%3D%20both%5B%22unit_no_unit%22%5D%20%20%20%20%20%20%20%20%20%20%23%20True%20where%20this%20part%20differs%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20One%20more%20trap%3A%20a%20unit%20entered%20twice%20(module%204's%20duplicates)%20is%20still%20one%20unit.%20Take%20the%20units'%20**distinct**%20keys%20first%2C%20or%20a%20household%20with%20one%20unit%20looks%20like%20a%20household%20with%20two.%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%203.6.%20Which%20parts%20differ%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20step%20that%20turns%20%22no%20match%22%20into%20a%20correction.%20Knowing%20the%20part%2C%20the%20unit's%20value%20for%20it%2C%20and%20how%20many%20units%20are%20one%20part%20away%20is%20what%20lets%20a%20rule%20correct%20a%20typo%20and%20lets%20a%20person%20see%20at%20a%20glance%20why%20a%20key%20was%20referred.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20orphan%20catch%20keys%20and%20the%20units%20%E2%86%92%20**one%20row%20per%20orphan%20catch%20key%20and%20each%20distinct%20unit%20of%20its%20household**%2C%20with%20the%20orphan's%20key%2C%20the%20unit's%20two%20parts%20(suffix%20%60_unit%60)%2C%20and%3A%0A%0A%20%20%20%20-%20%60differs%60%3A%20the%20parts%20that%20differ%2C%20in%20key%20order%2C%20joined%20by%20%60%22%2C%20%22%60%20(%60%22unit_no%22%60%2C%20or%20%60%22operator_line%2C%20unit_no%22%60)%3B%0A%20%20%20%20-%20%60n_differ%60%3A%20how%20many%20parts%20differ%3B%0A%20%20%20%20-%20%60n_units%60%3A%20how%20many%20units%20the%20household%20has%20(the%20rows%20this%20orphan%20has%20in%20the%20result)%3B%0A%20%20%20%20-%20%60n_near%60%3A%20how%20many%20of%20them%20are%20one%20part%20away%20from%20the%20orphan.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20unit_no%20%7C%20operator_line_unit%20%7C%20unit_no_unit%20%7C%20differs%20%7C%20n_differ%20%7C%20n_units%20%7C%20n_near%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%202%20%7C%201%20%7C%201%20%7C%201%20%7C%20operator_line%20%7C%201%20%7C%201%20%7C%201%20%7C%0A%20%20%20%20%7C%2001%20%7C%205102%20%7C%202%20%7C%203%20%7C%201%20%7C%201%20%7C%20operator_line%2C%20unit_no%20%7C%202%20%7C%201%20%7C%200%20%7C%0A%20%20%20%20%7C%2001%20%7C%205103%20%7C%201%20%7C%203%20%7C%201%20%7C%201%20%7C%20unit_no%20%7C%201%20%7C%202%20%7C%202%20%7C%0A%20%20%20%20%7C%2001%20%7C%205103%20%7C%201%20%7C%203%20%7C%201%20%7C%202%20%7C%20unit_no%20%7C%201%20%7C%202%20%7C%202%20%7C%0A%0A%20%20%20%20An%20orphan%20whose%20household%20has%20no%20unit%20at%20all%20has%20no%20row%3A%20section%204%20refers%20it%20to%20a%20person.%0A%0A%20%20%20%20**Tools%3A**%20%60drop_duplicates()%60%2C%20%60merge(...%2C%20on%3DHOUSEHOLD%2C%20suffixes%3D(%22%5C%22%2C%20%22_unit%22)%2C%20validate%3D%22many_to_many%22)%60%2C%20comparisons%20with%20%60!%3D%60%2C%20%60.sum(axis%3D1)%60%20across%20columns%2C%20and%20%60groupby(KEY)%5B...%5D.transform(%22size%22)%60%20and%20%60transform(%22sum%22)%60%20from%20Arrays%20%26%20DataFrames.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20compare_keys(orphans%3A%20pd.DataFrame%2C%20units%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20orphan%20catch%20key%20beside%20each%20distinct%20unit%20of%20its%20household%3A%20the%20unit's%20parts%20(_unit)%2C%20differs%2C%20n_differ%2C%0A%20%20%20%20n_units%20and%20n_near.%22%22%22%0A%20%20%20%20%23%20Exercise%203.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20units'%20distinct%20keys%0A%20%20%20%20%23%20Step%202%3A%20merge%20orphans%5BKEY%5D%20with%20them%20on%20HOUSEHOLD%2C%20suffixes%3D(%22%22%2C%20%22_unit%22)%2C%20validate%3D%22many_to_many%22%0A%20%20%20%20%23%20Step%203%3A%20for%20each%20part%20in%20PARTS%2C%20whether%20it%20differs%20from%20its%20_unit%20counterpart%3B%20n_differ%20%3D%20how%20many%20differ%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20differs%20%3D%20the%20names%20of%20those%20that%20differ%2C%20joined%20by%20%22%2C%20%22%0A%20%20%20%20%23%20Step%204%3A%20per%20orphan%20key%20(groupby(KEY))%3A%20n_units%20%3D%20its%20number%20of%20rows%2C%20transform(%22size%22)%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20n_near%20%3D%20its%20rows%20with%20n_differ%20%3D%3D%201%2C%20transform(%22sum%22)%20on%20that%20True%20or%20False%20column%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.6%22)%0A%0A%0A%40app.function%0Adef%20compared_columns()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20columns%20Exercise%203.6%20returns.%22%22%22%0A%20%20%20%20return%20KEY%20%2B%20%5Bf%22%7Bpart%7D_unit%22%20for%20part%20in%20PARTS%5D%20%2B%20%5B%22differs%22%2C%20%22n_differ%22%2C%20%22n_units%22%2C%20%22n_near%22%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(orphans)%3A%0A%20%20%20%20_small_units%2C%20_small_catch%20%3D%20small_survey()%0A%20%20%20%20_small_orphans%2C%20_%20%3D%20leftovers_in_sql(_small_units%2C%20_small_catch)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.6%22%2C%20compare_keys%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20survey%3A%20a%20unit%20entered%20twice%2C%20households%20of%20one%2C%20two%20and%20no%20units%2C%20a%20member%20who%20runs%20two%20units%22%2C%20(_small_orphans%2C%20_small_units))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20orphan%20catch%20keys%22%2C%20(orphans%2C%20units))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20orphan%20catch%20keys%22%2C%20(in_region(orphans%2C%20%2204%22)%2C%20in_region(units%2C%20%2204%22)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20KEY%20%2B%20%5Bf%22%7Bpart%7D_unit%22%20for%20part%20in%20PARTS%5D%2C%20compared_columns())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Distinct%20unit%20keys%2C%20a%20merge%20on%20HOUSEHOLD%20with%20suffixes%20(''%2C%20'_unit')%2C%20parts%20compared%20one%20by%20one%2C%20then%20n_units%20and%20n_near%20per%20orphan%20key%20with%20transform.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(orphans)%3A%0A%20%20%20%20%23%20Try%20it%3A%20every%20orphan%20beside%20its%20household's%20units%0A%20%20%20%20try_it(compare_keys%2C%20orphans%2C%20units)%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.6%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%20%60n_units%60%20and%20%60n_near%60%20are%202%20for%20the%20small%20survey's%20household%205105%20%7C%20The%20unit%20entered%20twice%20was%20joined%20twice.%20%7C%20step%201%2C%20%60drop_duplicates()%60%20%7C%0A%20%20%20%20%7C%20One%20row%20per%20orphan%2C%20every%20%60n_differ%60%202%20or%20missing%20%7C%20Joined%20on%20the%20whole%20key%2C%20which%20an%20orphan%20never%20matches.%20%7C%20%60on%3DHOUSEHOLD%60%20%7C%0A%20%20%20%20%7C%20Columns%20%60operator_line_x%60%20and%20%60operator_line_y%60%20%7C%20The%20default%20suffixes.%20%7C%20%60suffixes%3D(%22%22%2C%20%22_unit%22)%60%20%7C%0A%20%20%20%20%7C%20%60differs%60%20reads%20%60unit_no%2C%20operator_line%60%20%7C%20The%20names%20in%20another%20order%20than%20the%20key's.%20%7C%20loop%20over%20%60PARTS%60%20%7C%0A%20%20%20%20%7C%20%60n_units%60%20is%20the%20number%20of%20units%20in%20the%20survey%20%7C%20%60transform(%22size%22)%60%20without%20the%20group.%20%7C%20%60groupby(KEY)%60%20%7C%0A%20%20%20%20%7C%20%60n_near%60%20equals%20%60n_units%60%20%7C%20The%20rows%20were%20counted%2C%20not%20the%20rows%20one%20part%20away.%20%7C%20%60transform(%22sum%22)%60%20on%20%60n_differ%20%3D%3D%201%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%20comparison%20in%20SQL%0A%0A%20%20%20%20The%20same%20step%20in%20DuckDB%20SQL%3A%20a%20join%20on%20the%20household%2C%20one%20%60CASE%60%20per%20part%2C%20and%20two%20window%20figures%2C%20%60count(*)%20OVER%20(...)%60%2C%20which%20is%20SQL's%20%60transform(%22size%22)%60%2C%20and%20%60sum(...)%20OVER%20(...)%60%2C%20its%20%60transform(%22sum%22)%60.%20The%20next%20section%20starts%20from%20its%20result%2C%20%60comparison%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20compare_in_sql(orphans%3A%20pd.DataFrame%2C%20units%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%203.6%20in%20DuckDB%20SQL.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.register(%22orphans%22%2C%20orphans)%0A%20%20%20%20con.register(%22units%22%2C%20units)%0A%20%20%20%20out%20%3D%20con.execute(%22%22%22%0A%20%20%20%20%20%20%20%20WITH%20compared%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20o.region_code%2C%20o.hh_id%2C%20o.operator_line%2C%20o.unit_no%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20u.operator_line%20AS%20operator_line_unit%2C%20u.unit_no%20AS%20unit_no_unit%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20concat_ws('%2C%20'%2C%20CASE%20WHEN%20o.operator_line%20%3C%3E%20u.operator_line%20THEN%20'operator_line'%20END%2C%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%20%20%20%20CASE%20WHEN%20o.unit_no%20%3C%3E%20u.unit_no%20THEN%20'unit_no'%20END)%20AS%20differs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(o.operator_line%20%3C%3E%20u.operator_line)%3A%3AINTEGER%20%2B%20(o.unit_no%20%3C%3E%20u.unit_no)%3A%3AINTEGER%20AS%20n_differ%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20orphans%20o%0A%20%20%20%20%20%20%20%20%20%20%20%20JOIN%20(SELECT%20DISTINCT%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%20FROM%20units)%20u%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20USING%20(region_code%2C%20hh_id))%0A%20%20%20%20%20%20%20%20SELECT%20*%2C%20count(*)%20OVER%20orphan%20AS%20n_units%2C%20sum((n_differ%20%3D%201)%3A%3AINTEGER)%20OVER%20orphan%20AS%20n_near%0A%20%20%20%20%20%20%20%20FROM%20compared%0A%20%20%20%20%20%20%20%20WINDOW%20orphan%20AS%20(PARTITION%20BY%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no)%0A%20%20%20%20%20%20%20%20ORDER%20BY%20ALL%22%22%22).df()%0A%20%20%20%20con.close()%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell%0Adef%20_(orphans)%3A%0A%20%20%20%20%23%20The%20survey's%20orphans%20beside%20their%20households'%20units%2C%20and%20how%20many%20units%20each%20is%20one%20part%20from%0A%20%20%20%20comparison%20%3D%20compare_in_sql(orphans%2C%20units)%0A%20%20%20%20_per_orphan%20%3D%20comparison.drop_duplicates(KEY)%0A%20%20%20%20_near%20%3D%20_per_orphan%5B%22n_near%22%5D.value_counts().sort_index()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.hstack(%5Bcomparison%5B%22differs%22%5D.value_counts()%2C%20_per_orphan%5B%22n_units%22%5D.value_counts().sort_index().rename(%22orphans%20by%20units%20in%20the%20household%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_near.rename(%22orphans%20by%20units%20one%20part%20away%22)%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Of%20the%20survey's%20%7Blen(_per_orphan)%7D%20orphan%20catch%20keys%2C%20**%7B_near.get(1%2C%200)%7D**%20are%20one%20part%20away%20from%20exactly%20one%20unit%20of%20their%20household%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7B_near%5B_near.index%20%3E%3D%202%5D.sum()%7D**%20from%20two%20or%20more%2C%20and%20**%7B_near.get(0%2C%200)%7D**%20from%20none%3A%20both%20parts%20differ%20from%20every%20unit.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(comparison%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%204%20%C2%B7%20The%20automated%20case%20and%20the%20manual%20case%0A%0A%20%20%20%20**The%20problem.**%20An%20editor%20could%20open%20each%20orphan%20and%20correct%20it%20by%20hand.%20The%20obvious%20ones%20would%20be%20corrected%20the%20same%20way%20every%20time%2C%20at%20the%20cost%20of%20an%20editor's%20day%3B%20the%20hard%20ones%20would%20be%20corrected%20the%20way%20that%20editor%20happened%20to%20see%20them.%20A%20rule%20corrects%20the%20obvious%20ones%20the%20same%20way%20in%20every%20region%20and%20every%20survey%20round%2C%20and%20sends%20the%20rest%20to%20a%20person%20with%20the%20reason.%0A%0A%20%20%20%20**The%20rules**%2C%20checked%20in%20this%20order%3B%20the%20first%20that%20holds%20decides%3A%0A%0A%20%20%20%20%7C%20%23%20%7C%20when%20%7C%20case%20%7C%20reason%20%7C%20the%20correction%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%20the%20stray%20key%20is%20its%20household's%20only%20one%2C%20and%20the%20household%20has%20exactly%20one%20unit%20without%20catch%20%7C%20automated%20%7C%20%60only%20stray%20key%2C%20only%20unit%20without%20catch%60%20%7C%20that%20unit's%20key%3A%20the%20catch%20with%20no%20unit%20and%20the%20unit%20with%20no%20catch%20are%20the%20two%20halves%20of%20one%20mismatch%20%7C%0A%20%20%20%20%7C%202%20%7C%20the%20stray%20key%20is%20one%20part%20away%20from%20exactly%20one%20unit%20of%20its%20household%20%7C%20automated%20%7C%20%60one%20part%20from%20one%20unit%60%20%7C%20that%20unit's%20value%20for%20the%20part%3A%20a%20typo%20%7C%0A%20%20%20%20%7C%203%20%7C%20it%20is%20one%20part%20away%20from%20two%20or%20more%20units%20%7C%20manual%20%7C%20%60one%20part%20from%20two%20or%20more%20units%60%20%7C%20none%3A%20which%20of%20them%20caught%20this%20species%20is%20a%20judgment%20%7C%0A%20%20%20%20%7C%204%20%7C%20otherwise%3A%20both%20parts%20differ%20from%20every%20unit%20%7C%20manual%20%7C%20%60both%20parts%20differ%60%20%7C%20none%3A%20this%20may%20be%20the%20catch%20of%20a%20unit%20nobody%20entered%20%7C%0A%0A%20%20%20%20Order%20matters.%20Rule%201%20comes%20first%3A%20when%20a%20household%20has%20one%20stray%20key%20and%20one%20unit%20with%20nothing%2C%20they%20belong%20together%20even%20when%20both%20parts%20differ%2C%20a%20key%20rule%204%20would%20send%20to%20a%20person.%20Rule%202%20needs%20exactly%20one%20near%20unit%3A%20a%20key%20one%20part%20away%20from%20two%20units%20of%20the%20same%20operator%20could%20belong%20to%20either%2C%20so%20rule%203%20sends%20it%20to%20a%20person.%0A%0A%20%20%20%20**The%20tool.**%20R%20writes%20rules%20like%20these%20with%20%60case_when()%60%3B%20SQL%20with%20%60CASE%20WHEN%60%3B%20numpy%20with%20%60np.select(conditions%2C%20choices%2C%20default%3D...)%60.%20All%20three%20test%20the%20conditions%20in%20order%20and%20take%20the%20first%20that%20is%20true.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20reason%20%3D%20np.select(%0A%20%20%20%20%20%20%20%20%5Btable%5B%22paired%22%5D%2C%20table%5B%22n_near%22%5D%20%3D%3D%201%2C%20table%5B%22n_near%22%5D%20%3E%3D%202%5D%2C%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%23%20the%20conditions%2C%20in%20order%0A%20%20%20%20%20%20%20%20%5B%22only%20stray%20key%2C%20only%20unit%20without%20catch%22%2C%20%22one%20part%20from%20one%20unit%22%2C%20%22one%20part%20from%20two%20or%20more%20units%22%5D%2C%0A%20%20%20%20%20%20%20%20default%3D%22both%20parts%20differ%22%2C%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%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%23%20when%20none%20holds%0A%20%20%20%20)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Rule%201%20needs%20three%20facts.%20Whether%20the%20unit%20on%20this%20row%20of%20the%20comparison%20has%20catch%20of%20its%20own%3A%20whether%20its%20key%20is%20among%20%60without_catch%60%2C%20a%20left%20join%20with%20%60indicator%3DTrue%60.%20How%20many%20stray%20keys%20the%20household%20has%2C%20and%20how%20many%20units%20without%20catch%3A%20each%20a%20%60groupby(HOUSEHOLD).size()%60%20merged%20back%20on%20the%20household.%20A%20row%20where%20all%20three%20hold%20pairs%20the%20stray%20key%20with%20its%20unit%2C%20and%20%60groupby(KEY)%5B%22...%22%5D.transform(%22any%22)%60%20carries%20that%20to%20every%20row%20of%20the%20stray%20key.%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%203.7.%20Automated%20or%20manual%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20table%20is%20the%20decision%2C%20one%20line%20per%20orphan.%20A%20reviewer%20reads%20its%20reasons%3B%20the%20next%20exercise%20turns%20its%20automated%20lines%20into%20edits.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20comparison%20(Exercise%203.6's%20table)%20and%20the%20units%20with%20no%20catch%20%E2%86%92%20**one%20row%20per%20orphan%20catch%20key**%2C%20with%20its%20key%20and%3A%0A%0A%20%20%20%20-%20%60case%60%3A%20%60%22automated%22%60%20or%20%60%22manual%22%60%3B%0A%20%20%20%20-%20%60reason%60%3A%20one%20of%20the%20four%20reasons%20in%20the%20rules%20above%2C%20as%20written%20there%3B%0A%20%20%20%20-%20%60operator_line_new%60%2C%20%60unit_no_new%60%3A%20the%20parts%20of%20the%20unit%20the%20rule%20chose%2C%20for%20an%20automated%20case%3B%20missing%20for%20a%20manual%20one.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20unit_no%20%7C%20case%20%7C%20reason%20%7C%20operator_line_new%20%7C%20unit_no_new%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%202%20%7C%201%20%7C%20automated%20%7C%20one%20part%20from%20one%20unit%20%7C%201%20%7C%201%20%7C%0A%20%20%20%20%7C%2001%20%7C%205102%20%7C%202%20%7C%203%20%7C%20automated%20%7C%20only%20stray%20key%2C%20only%20unit%20without%20catch%20%7C%201%20%7C%201%20%7C%0A%20%20%20%20%7C%2001%20%7C%205103%20%7C%201%20%7C%203%20%7C%20manual%20%7C%20one%20part%20from%20two%20or%20more%20units%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%2001%20%7C%205109%20%7C%203%20%7C%202%20%7C%20manual%20%7C%20both%20parts%20differ%20%7C%20%7C%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60merge(...%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%60%2C%20%60groupby().size()%60%2C%20%60transform(%22any%22)%60%2C%20%60np.select%60%2C%20%60np.where%60%2C%20%60drop_duplicates()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20decide(comparison%3A%20pd.DataFrame%2C%20without_catch%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20orphan%20catch%20key%3A%20case%20(automated%20or%20manual)%2C%20reason%2C%20and%20the%20new%20parts%20for%20an%20automated%20case.%22%22%22%0A%20%20%20%20%23%20Exercise%203.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20mark%20the%20rows%20whose%20unit%20has%20no%20catch%3A%20rename%20without_catch's%20parts%20to%20operator_line_unit%20and%20unit_no_unit%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20merge%20onto%20comparison%20(how%3D%22left%22%2C%20indicator%3DTrue)%20on%20HOUSEHOLD%20%2B%20the%20_unit%20parts%3B%20no_catch%20%3D%20_merge%20%3D%3D%20%22both%22%0A%20%20%20%20%23%20Step%202%3A%20per%20household%2C%20the%20stray%20keys%20(comparison%5BKEY%5D.drop_duplicates())%20and%20the%20units%20without%20catch%2C%20each%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20counted%20with%20groupby(HOUSEHOLD).size()%20and%20merged%20back%20on%20HOUSEHOLD%20(how%3D%22left%22)%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20pair_row%3A%20one%20stray%20key%2C%20one%20unit%20without%20catch%2C%20and%20no_catch%20on%20this%20row%0A%20%20%20%20%23%20Step%203%3A%20paired%3A%20whether%20any%20row%20of%20the%20orphan%20key%20is%20a%20pair_row%2C%20groupby(KEY)%5B%22pair_row%22%5D.transform(%22any%22)%0A%20%20%20%20%23%20Step%204%3A%20reason%20with%20np.select%20in%20the%20rules'%20order%20(paired%3B%20n_near%20%3D%3D%201%3B%20n_near%20%3E%3D%202)%2C%20default%20NONE_NEAR%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20case%20%22automated%22%20for%20PAIRED%20and%20ONE_NEAR%2C%20else%20%22manual%22%0A%20%20%20%20%23%20Step%205%3A%20the%20correction%20comes%20from%20one%20row%3A%20the%20pair_row%20under%20rule%201%2C%20the%20row%20with%20n_differ%20%3D%3D%201%20under%20rule%202%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20keep%20those%20rows'%20KEY%20and%20_unit%20parts%2C%20renamed%20to%20operator_line_new%20and%20unit_no_new%0A%20%20%20%20%23%20Step%206%3A%20one%20row%20per%20orphan%20key%20(drop_duplicates(KEY))%20with%20KEY%2C%20case%20and%20reason%2C%20and%20the%20corrections%20merged%20on%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20(how%3D%22left%22)%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.7%22)%0A%0A%0A%40app.function%0Adef%20decided_columns()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20columns%20Exercise%203.7%20returns.%22%22%22%0A%20%20%20%20return%20KEY%20%2B%20%5B%22case%22%2C%20%22reason%22%5D%20%2B%20%5Bf%22%7Bpart%7D_new%22%20for%20part%20in%20PARTS%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(comparison%2C%20without_catch)%3A%0A%20%20%20%20_small_units%2C%20_small_catch%20%3D%20small_survey()%0A%20%20%20%20_small_orphans%2C%20_small_without%20%3D%20leftovers_in_sql(_small_units%2C%20_small_catch)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.7%22%2C%20decide%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20survey%3A%20each%20of%20the%20four%20rules%2C%20a%20unit%20without%20catch%20two%20parts%20away%2C%20a%20household%20of%20two%20units%20without%20catch%22%2C%20(compare_in_sql(_small_orphans%2C%20_small_units)%2C%20_small_without))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20comparison%22%2C%20(comparison%2C%20without_catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20comparison%22%2C%20(in_region(comparison%2C%20%2201%22)%2C%20in_region(without_catch%2C%20%2201%22)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20KEY%2C%20decided_columns())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Mark%20units%20with%20no%20catch%20(left%20merge%2C%20indicator)%2C%20count%20stray%20keys%20and%20units%20without%20catch%20per%20household%2C%20then%20np.select%20in%20the%20rules'%20order%3B%20new%20parts%20only%20from%20the%20row%20the%20rule%20chose%3B%20one%20row%20per%20orphan%20key.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(comparison%2C%20without_catch)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20decisions%2C%20counted%20by%20case%20and%20reason%0A%20%20%20%20def%20_try_decide()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20decided%20%3D%20decide(comparison%2C%20without_catch)%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%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bdecided.groupby(%5B%22case%22%2C%20%22reason%22%5D).size().rename(%22orphan%20keys%22)%2C%20decided.head(8)%5D)%0A%0A%20%20%20%20_try_decide()%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.7%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%20Household%205102%20of%20the%20small%20survey%20is%20manual%2C%20%60both%20parts%20differ%60%20%7C%20Rule%201%20was%20not%20tested%2C%20or%20tested%20after%20the%20default%20case.%20%7C%20steps%201%20to%203%2C%20and%20rule%201%20first%20%7C%0A%20%20%20%20%7C%20Household%205108%20of%20the%20small%20survey%20is%20%60only%20stray%20key%2C%20only%20unit%20without%20catch%60%20%7C%20The%20units%20without%20catch%20were%20not%20counted%3A%205108%20has%20two%2C%20so%20rule%201%20does%20not%20hold.%20%7C%20step%202%2C%20%60idle%20%3D%3D%201%60%20%7C%0A%20%20%20%20%7C%20Two%20rows%20for%20the%20same%20orphan%20key%20%7C%20The%20rows%20of%20a%20household%20of%20several%20units%20were%20not%20reduced%20to%20one.%20%7C%20step%206%2C%20%60drop_duplicates(KEY)%60%20%7C%0A%20%20%20%20%7C%20Household%205103's%20orphan%20is%20automated%20%7C%20%60n_differ%20%3D%3D%201%60%20on%20the%20row%20tested%20instead%20of%20%60n_near%60%2C%20the%20units%20one%20part%20away.%20%7C%20rules%202%20and%203%20use%20%60n_near%60%20%7C%0A%20%20%20%20%7C%20An%20automated%20row%20with%20missing%20new%20parts%2C%20or%20the%20parts%20of%20the%20wrong%20unit%20%7C%20The%20correction%20taken%20from%20the%20first%20row%20of%20the%20orphan%2C%20not%20from%20the%20row%20the%20rule%20chose.%20%7C%20step%205%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20list%20of%20cases%20must%20be%20same%20length%20as%20list%20of%20conditions*%20%7C%20Three%20conditions%2C%20but%20two%20or%20four%20choices.%20%7C%20%60np.select%60's%20two%20lists%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%20rules%20in%20SQL%0A%0A%20%20%20%20%60CASE%20WHEN%60%20is%20SQL's%20%60np.select%60%3A%20the%20conditions%20in%20order%2C%20the%20first%20that%20holds%20wins.%20%60bool_or(...)%20OVER%20(...)%60%20is%20its%20%60transform(%22any%22)%60.%20The%20survey's%20decisions%20below%20come%20from%20it%2C%20so%20the%20rest%20of%20the%20module%20starts%20from%20the%20same%20table%2C%20%60decisions%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20decide_in_sql(comparison%3A%20pd.DataFrame%2C%20without_catch%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%203.7%20in%20DuckDB%20SQL.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.register(%22comparison%22%2C%20comparison)%0A%20%20%20%20con.register(%22without_catch%22%2C%20without_catch)%0A%20%20%20%20out%20%3D%20con.execute(f%22%22%22%0A%20%20%20%20%20%20%20%20WITH%20strays%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20region_code%2C%20hh_id%2C%20count(*)%20AS%20strays%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20(SELECT%20DISTINCT%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%20FROM%20comparison)%20GROUP%20BY%20ALL)%2C%0A%20%20%20%20%20%20%20%20idle%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20region_code%2C%20hh_id%2C%20count(*)%20AS%20idle%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20(SELECT%20DISTINCT%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%20FROM%20without_catch)%20GROUP%20BY%20ALL)%2C%0A%20%20%20%20%20%20%20%20marked%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20c.*%2C%20coalesce(s.strays%20%3D%201%20AND%20i.idle%20%3D%201%20AND%20w.unit_no%20IS%20NOT%20NULL%2C%20false)%20AS%20pair_row%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20comparison%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20LEFT%20JOIN%20without_catch%20w%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20ON%20w.region_code%20%3D%20c.region_code%20AND%20w.hh_id%20%3D%20c.hh_id%0A%20%20%20%20%20%20%20%20%20%20%20%20%20AND%20w.operator_line%20%3D%20c.operator_line_unit%20AND%20w.unit_no%20%3D%20c.unit_no_unit%0A%20%20%20%20%20%20%20%20%20%20%20%20LEFT%20JOIN%20strays%20s%20ON%20s.region_code%20%3D%20c.region_code%20AND%20s.hh_id%20%3D%20c.hh_id%0A%20%20%20%20%20%20%20%20%20%20%20%20LEFT%20JOIN%20idle%20i%20ON%20i.region_code%20%3D%20c.region_code%20AND%20i.hh_id%20%3D%20c.hh_id)%2C%0A%20%20%20%20%20%20%20%20ruled%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20*%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CASE%20WHEN%20bool_or(pair_row)%20OVER%20(PARTITION%20BY%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no)%20THEN%20'%7BPAIRED%7D'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHEN%20n_near%20%3D%201%20THEN%20'%7BONE_NEAR%7D'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHEN%20n_near%20%3E%3D%202%20THEN%20'%7BSEVERAL_NEAR%7D'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ELSE%20'%7BNONE_NEAR%7D'%20END%20AS%20reason%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20marked)%2C%0A%20%20%20%20%20%20%20%20picked%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20*%2C%20(reason%20%3D%20'%7BPAIRED%7D'%20AND%20pair_row)%20OR%20(reason%20%3D%20'%7BONE_NEAR%7D'%20AND%20n_differ%20%3D%201)%20AS%20chosen%20FROM%20ruled)%0A%20%20%20%20%20%20%20%20SELECT%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CASE%20WHEN%20reason%20IN%20('%7BPAIRED%7D'%2C%20'%7BONE_NEAR%7D')%20THEN%20'automated'%20ELSE%20'manual'%20END%20AS%20%22case%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reason%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max(CASE%20WHEN%20chosen%20THEN%20operator_line_unit%20END)%20AS%20operator_line_new%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max(CASE%20WHEN%20chosen%20THEN%20unit_no_unit%20END)%20AS%20unit_no_new%0A%20%20%20%20%20%20%20%20FROM%20picked%0A%20%20%20%20%20%20%20%20GROUP%20BY%20ALL%0A%20%20%20%20%20%20%20%20ORDER%20BY%20ALL%22%22%22).df()%0A%20%20%20%20con.close()%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell%0Adef%20_(comparison%2C%20without_catch)%3A%0A%20%20%20%20%23%20The%20survey's%20decisions%0A%20%20%20%20decisions%20%3D%20decide_in_sql(comparison%2C%20without_catch)%0A%20%20%20%20decisions.groupby(%5B%22case%22%2C%20%22reason%22%5D).size().rename(%22orphan%20keys%22).reset_index()%0A%20%20%20%20return%20(decisions%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%20Did%20the%20method%20find%20what%20was%20planted%3F%0A%0A%20%20%20%20%60planted_defects.csv%60%20names%20five%20kinds%20of%20error%20between%20the%20catch%20and%20the%20units.%20A%20method%20that%20is%20right%20finds%20each%20of%20them%2C%20decides%20each%20the%20way%20its%20kind%20calls%20for%2C%20and%20flags%20nothing%20else.%20The%20cell%20below%20joins%20the%20households%20the%20method%20flagged%20to%20the%20list's%20households%20of%20those%20five%20kinds.%20The%20units%20with%20no%20catch%20whose%20household%20has%20no%20stray%20catch%20key%20are%20the%20%60unit%20only%60%20label%3A%20referred%2C%20since%20no%20key%20correction%20can%20make%20up%20a%20catch%20nobody%20entered.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(decisions%2C%20orphans%2C%20without_catch)%3A%0A%20%20%20%20%23%20What%20the%20method%20found%2C%20household%20by%20household%2C%20against%20what%20the%20course%20planted%0A%20%20%20%20_kinds%20%3D%20%5B%22catch_key_one_part_off%22%2C%20%22catch_rows_all_astray%22%2C%20%22catch_key_both_parts%22%2C%20%22catch_key_two_candidates%22%2C%20%22unit_without_catch%22%5D%0A%20%20%20%20_planted%20%3D%20course.planted_defects()%0A%20%20%20%20_planted%20%3D%20_planted%5B_planted%5B%22kind%22%5D.isin(_kinds)%5D%5BHOUSEHOLD%20%2B%20%5B%22kind%22%5D%5D%0A%20%20%20%20_no_unit%20%3D%20orphans%5BHOUSEHOLD%5D.drop_duplicates().merge(units%5BHOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left_anti%22)%0A%20%20%20%20_missing_catch%20%3D%20without_catch%5BHOUSEHOLD%5D.drop_duplicates().merge(orphans%5BHOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left_anti%22)%0A%20%20%20%20_found%20%3D%20pd.concat(%5B%0A%20%20%20%20%20%20%20%20decisions%5BHOUSEHOLD%20%2B%20%5B%22case%22%2C%20%22reason%22%5D%5D.assign(outcome%3Ddecisions%5B%22case%22%5D%20%2B%20%22%3A%20%22%20%2B%20decisions%5B%22reason%22%5D)%5BHOUSEHOLD%20%2B%20%5B%22outcome%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20_no_unit.assign(outcome%3D%22manual%3A%20no%20unit%20in%20the%20household%22)%2C%0A%20%20%20%20%20%20%20%20_missing_catch.assign(outcome%3D%22referred%3A%20unit%20with%20no%20catch%22)%2C%0A%20%20%20%20%5D).drop_duplicates(HOUSEHOLD)%0A%20%20%20%20_both%20%3D%20_planted.merge(_found%2C%20on%3DHOUSEHOLD%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.crosstab(_both%5B%22kind%22%5D.fillna(%22not%20planted%22)%2C%20_both%5B%22outcome%22%5D.fillna(%22not%20found%22))%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7B(_both%5B'_merge'%5D%20%3D%3D%20'both').sum()%7D%20of%20the%20%7Blen(_planted)%7D%20planted%20key%20errors%20found**%3B%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B(_both%5B'_merge'%5D%20%3D%3D%20'right_only').sum()%7D%20households%20flagged%20that%20the%20list%20does%20not%20name%3B%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B(_both%5B'_merge'%5D%20%3D%3D%20'left_only').sum()%7D%20planted%20errors%20missed.%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%20Every%20one-part%20typo%20and%20every%20unit%20whose%20catch%20all%20points%20elsewhere%20is%20corrected%20by%20rule%3B%20every%20key%20that%20differs%20from%20its%20household's%20units%20in%20both%20parts%2C%20and%20every%20key%20one%20part%20from%20two%20units%2C%20goes%20to%20a%20person%3B%20every%20unit%20with%20no%20catch%20and%20no%20stray%20key%20is%20referred.%20The%20table%20is%20the%20evidence%20that%20the%20rules%20do%20what%20their%20reasons%20say%2C%20on%20this%20survey.%20On%20a%20real%20survey%20the%20evidence%20is%20a%20sample%20of%20the%20automated%20corrections%20read%20by%20an%20editor.%0A%0A%20%20%20%20%23%23%23%20Referred%20to%20a%20person%0A%0A%20%20%20%20The%20manual%20cases%20need%20a%20person%2C%20and%20a%20person%20needs%20more%20than%20%22manual%22%3A%20what%20to%20look%20at%2C%20and%20where.%20The%20referral%20list%20below%20carries%20the%20reason%20and%20the%20question%20to%20answer%20for%20each.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(decisions%2C%20orphans%2C%20without_catch)%3A%0A%20%20%20%20%23%20Everything%20a%20person%20must%20look%20at%3A%20the%20manual%20keys%2C%20catch%20with%20no%20unit%20in%20its%20household%2C%20units%20with%20no%20catch%0A%20%20%20%20_question%20%3D%20%7B%0A%20%20%20%20%20%20%20%20SEVERAL_NEAR%3A%20%22Which%20of%20the%20operator's%20units%20caught%20this%20species%3F%20Each%20unit%20has%20its%20own%20pages%20in%20the%20questionnaire.%22%2C%0A%20%20%20%20%20%20%20%20NONE_NEAR%3A%20%22Is%20this%20the%20catch%20of%20a%20unit%20nobody%20entered%3F%20Compare%20the%20operator's%20line%20and%20the%20unit's%20number%20with%20the%20questionnaire.%22%2C%0A%20%20%20%20%20%20%20%20%22no%20unit%20in%20the%20household%22%3A%20%22Was%20the%20household's%20unit%20deleted%2C%20or%20never%20entered%3F%22%2C%0A%20%20%20%20%20%20%20%20%22unit%20has%20no%20catch%20rows%22%3A%20%22Were%20the%20unit's%20species%20left%20blank%2C%20or%20their%20rows%20deleted%3F%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20_manual%20%3D%20decisions%5Bdecisions%5B%22case%22%5D%20%3D%3D%20%22manual%22%5D%5BKEY%20%2B%20%5B%22reason%22%5D%5D.assign(what%3D%22catch%20key%22)%0A%20%20%20%20_no_unit%20%3D%20orphans%5BKEY%5D.merge(units%5BHOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left_anti%22).assign(what%3D%22catch%20key%22%2C%20reason%3D%22no%20unit%20in%20the%20household%22)%0A%20%20%20%20_missing_catch%20%3D%20without_catch%5BKEY%5D.merge(orphans%5BHOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left_anti%22).assign(what%3D%22unit%22%2C%20reason%3D%22unit%20has%20no%20catch%20rows%22)%0A%20%20%20%20referrals%20%3D%20pd.concat(%5B_manual%2C%20_no_unit%2C%20_missing_catch%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20referrals%5B%22question%22%5D%20%3D%20referrals%5B%22reason%22%5D.map(_question)%0A%20%20%20%20referrals%20%3D%20referrals.sort_values(KEY).reset_index(drop%3DTrue)%5BKEY%20%2B%20%5B%22what%22%2C%20%22reason%22%2C%20%22question%22%5D%5D%0A%20%20%20%20referrals%0A%20%20%20%20return%20(referrals%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%20From%20decisions%20to%20edits%0A%0A%20%20%20%20A%20decision%20is%20about%20a%20key%3B%20an%20edit%20is%20about%20a%20row%20of%20the%20database.%20One%20orphan%20key%20can%20stand%20for%20several%20catch%20rows%20(a%20unit%20whose%20every%20catch%20row%20points%20elsewhere)%2C%20and%20a%20row%20can%20need%20both%20of%20its%20parts%20changed.%20The%20database%20also%20has%20its%20own%20names%3A%20in%20the%20catch%20table%20the%20parts%20are%20%60catch_operator_line%60%20and%20%60catch_unit_no%60%2C%20and%20each%20row%20has%20its%20id%2C%20%60row_id%60.%20An%20edit%20a%20program%20can%20apply%2C%20and%20a%20person%20can%20check%2C%20names%20all%20of%20it%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60region_code%60%2C%20%60hh_id%60%20%7C%20the%20household%2C%20so%20a%20reviewer%20can%20find%20its%20questionnaire%20%7C%0A%20%20%20%20%7C%20%60table%60%2C%20%60row_id%60%20%7C%20the%20table%20and%20the%20row's%20id%20in%20that%20region's%20database%20%7C%0A%20%20%20%20%7C%20%60column%60%20%7C%20the%20database's%20name%20for%20the%20part%3A%20%60catch_unit_no%60%20%7C%0A%20%20%20%20%7C%20%60old%60%2C%20%60new%60%20%7C%20the%20value%20now%2C%20and%20the%20value%20after%20the%20edit%20%7C%0A%20%20%20%20%7C%20%60reason%60%20%7C%20the%20rule%20that%20made%20the%20edit%20%7C%0A%0A%20%20%20%20One%20row%20per%20value%20that%20changes%3A%20a%20part%20that%20already%20has%20the%20unit's%20value%20is%20no%20edit.%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%203.8.%20The%20edits%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20table%20a%20reviewer%20signs%20off%20and%20the%20write-back%20applies.%20If%20it%20names%20the%20unit's%20columns%20instead%20of%20the%20catch%20table's%2C%20or%20one%20row%20per%20key%20instead%20of%20one%20per%20catch%20row%2C%20the%20write-back%20changes%20the%20wrong%20thing%20or%20too%20little.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20decisions%20(Exercise%203.7's%20table)%20and%20the%20catch%20table%20as%20stored%20%E2%86%92%20**one%20row%20per%20catch%20row%20and%20part%20that%20changes**%2C%20for%20the%20automated%20cases%20only%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20table%20%7C%20row_id%20%7C%20column%20%7C%20old%20%7C%20new%20%7C%20reason%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%3A--%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%20catch%20%7C%203%20%7C%20catch_operator_line%20%7C%202%20%7C%201%20%7C%20one%20part%20from%20one%20unit%20%7C%0A%20%20%20%20%7C%2001%20%7C%205102%20%7C%20catch%20%7C%204%20%7C%20catch_operator_line%20%7C%202%20%7C%201%20%7C%20only%20stray%20key%2C%20only%20unit%20without%20catch%20%7C%0A%20%20%20%20%7C%2001%20%7C%205102%20%7C%20catch%20%7C%204%20%7C%20catch_unit_no%20%7C%203%20%7C%201%20%7C%20only%20stray%20key%2C%20only%20unit%20without%20catch%20%7C%0A%20%20%20%20%7C%2001%20%7C%205102%20%7C%20catch%20%7C%205%20%7C%20catch_operator_line%20%7C%202%20%7C%201%20%7C%20only%20stray%20key%2C%20only%20unit%20without%20catch%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%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%20%60table%60%20is%20%60CATCH%60%3B%20%60row_id%60%20is%20the%20catch%20row's%20%60row_id%60.%0A%0A%20%20%20%20**Tools%3A**%20a%20boolean%20filter%2C%20%60merge(left_on%3D...%2C%20right_on%3D...)%60%2C%20a%20loop%20over%20%60PARTS%60%2C%20and%20%60pd.concat%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20edits_table(decisions%3A%20pd.DataFrame%2C%20catch%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20catch%20row%20and%20key%20part%20an%20automated%20decision%20changes%3A%20region_code%2C%20hh_id%2C%20table%2C%20row_id%2C%20column%2C%0A%20%20%20%20old%2C%20new%20and%20reason.%22%22%22%0A%20%20%20%20%23%20Exercise%203.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20automated%20decisions%0A%20%20%20%20%23%20Step%202%3A%20join%20them%20to%20the%20catch%20rows%20they%20stand%20for%3A%20left_on%3DKEY%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20right_on%3DHOUSEHOLD%20%2B%20the%20catch's%20own%20parts%20(catch_operator_line%2C%20catch_unit_no)%0A%20%20%20%20%23%20Step%203%3A%20for%20each%20part%2C%20keep%20the%20rows%20where%20the%20catch's%20value%20differs%20from%20the%20decision's%20_new%20value%2C%20as%20a%20table%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20region_code%2C%20hh_id%2C%20table%20(CATCH)%2C%20row_id%2C%20column%20(%22catch_%22%20%2B%20the%20part)%2C%20old%2C%20new%2C%20reason%0A%20%20%20%20%23%20Step%204%3A%20pd.concat%20the%20two%20tables%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.8%22)%0A%0A%0A%40app.function%0Adef%20edit_columns()%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20columns%20of%20an%20edits%20table.%22%22%22%0A%20%20%20%20return%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22table%22%2C%20%22row_id%22%2C%20%22column%22%2C%20%22old%22%2C%20%22new%22%2C%20%22reason%22%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(decisions)%3A%0A%20%20%20%20_small_units%2C%20_small_catch%20%3D%20small_survey()%0A%20%20%20%20_small_orphans%2C%20_small_without%20%3D%20leftovers_in_sql(_small_units%2C%20_small_catch)%0A%20%20%20%20_small_decisions%20%3D%20decide_in_sql(compare_in_sql(_small_orphans%2C%20_small_units)%2C%20_small_without)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.8%22%2C%20edits_table%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20survey%3A%20a%20key%20on%20two%20catch%20rows%2C%20both%20parts%20to%20change%20on%20each%22%2C%20(_small_decisions%2C%20_small_catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20decisions%22%2C%20(decisions%2C%20catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20decisions%20and%20catch%22%2C%20(in_region(decisions%2C%20%2207%22)%2C%20in_region(catch%2C%20%2207%22)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%5B%22region_code%22%2C%20%22row_id%22%2C%20%22column%22%5D%2C%20edit_columns())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Automated%20decisions%20only%2C%20joined%20to%20every%20catch%20row%20that%20carries%20the%20key%3B%20one%20row%20per%20part%20whose%20value%20changes%2C%20named%20with%20the%20catch_%20prefix.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(decisions)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20edits.%20The%20cells%20of%20section%205%20use%20your%20table.%0A%20%20%20%20all_edits%20%3D%20attempt(edits_table%2C%20decisions%2C%20catch)%0A%20%20%20%20try_it(edits_table%2C%20decisions%2C%20catch)%0A%20%20%20%20return%20(all_edits%2C)%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.8%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%20%60column%60%20reads%20%60unit_no%60%20%7C%20The%20unit's%20name%2C%20which%20the%20catch%20table%20does%20not%20have.%20%7C%20%60f%22catch_%7Bpart%7D%22%60%20%7C%0A%20%20%20%20%7C%20One%20edit%20per%20key%2C%20not%20per%20catch%20row%20%7C%20The%20decisions%20were%20not%20joined%20back%20to%20the%20catch%20rows.%20%7C%20step%202%20%7C%0A%20%20%20%20%7C%20Edits%20for%20manual%20keys%2C%20with%20missing%20%60new%60%20%7C%20The%20automated%20cases%20were%20not%20kept%20first.%20%7C%20step%201%20%7C%0A%20%20%20%20%7C%20Rows%20where%20%60old%60%20equals%20%60new%60%20%7C%20Every%20part%20listed%2C%20not%20only%20the%20ones%20that%20change.%20%7C%20%60!%3D%60%20in%20step%203%20%7C%0A%20%20%20%20%7C%20%60row_id%60%20repeats%20with%20different%20households%20%7C%20Rows%20from%20two%20regions%3A%20each%20region's%20database%20numbers%20its%20rows%20from%201.%20%7C%20keep%20%60region_code%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'catch_operator_line'*%20%7C%20The%20catch%20table%20was%20renamed%20before%20the%20join.%20%7C%20use%20the%20catch%20table%20as%20stored%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%205%20%C2%B7%20Edits%20written%20back%20in%20a%20transaction%20with%20an%20audit%20flag%0A%0A%20%20%20%20**The%20problem.**%20The%20edits%20table%20is%20a%20plan.%20Applying%20it%20changes%20a%20database%20other%20people%20read%2C%20and%20several%20things%20can%20go%20wrong.%20The%20edits%20can%20be%20written%20before%20anyone%20has%20read%20them.%20A%20failure%20halfway%20can%20leave%20half%20of%20them%20written.%20A%20second%20run%20can%20stamp%20them%20again%2C%20or%20a%20run%20on%20last%20week's%20table%20can%20overwrite%20a%20value%20a%20colleague%20has%20since%20corrected.%20And%20because%20household%20ids%20and%20row%20ids%20repeat%20between%20regions%2C%20region%2004's%20edits%20written%20into%20region%2001's%20file%20would%20change%20other%20households'%20rows%20without%20an%20error.%0A%0A%20%20%20%20**The%20tool**%20is%20the%20way%20a%20careful%20script%20makes%20any%20change%20with%20a%20wide%20reach.%20Each%20safeguard%20answers%20one%20of%20those%20failures%3A%0A%0A%20%20%20%20%7C%20what%20can%20go%20wrong%20%7C%20the%20safeguard%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20survey's%20own%20file%20is%20changed%20%7C%20work%20on%20a%20copy%20%7C%20%60shutil.copyfile%60%20into%20%60scratch%2F%60%3B%20%60data%2F%60%20is%20only%20read%20%7C%0A%20%20%20%20%7C%20edits%20written%20before%20anyone%20read%20them%20%7C%20a%20**dry%20run**%20by%20default%3B%20the%20write%20needs%20an%20explicit%20%60apply%3DTrue%60%20%7C%20%60write_back(copy%2C%20edits)%60%20reports%2C%20%60write_back(copy%2C%20edits%2C%20apply%3DTrue)%60%20writes%20%7C%0A%20%20%20%20%7C%20half%20the%20edits%20written%2C%20then%20an%20error%20%7C%20one%20**transaction**%3A%20every%20edit%20is%20kept%2C%20or%20none%20is%20%7C%20%60BEGIN%20TRANSACTION%60%20%E2%80%A6%20%60COMMIT%60%2C%20and%20%60ROLLBACK%60%20on%20any%20error%20%7C%0A%20%20%20%20%7C%20a%20second%20run%20stamps%20the%20edits%20again%20%7C%20an%20**audit%20flag**%20on%20each%20edited%20row%3B%20a%20flagged%20row%20is%20skipped%20%7C%20the%20column%20%60key_corrected%60%2C%201%20on%20every%20row%20the%20write%20changed%20%7C%0A%20%20%20%20%7C%20the%20database%20changed%20since%20the%20edits%20were%20made%20%7C%20each%20value%20must%20still%20be%20the%20edit's%20%60old%60%3B%20if%20one%20is%20not%2C%20nothing%20is%20written%20%7C%20status%20%60conflict%60%2C%20and%20a%20*ValueError*%20%7C%0A%20%20%20%20%7C%20one%20region's%20edits%20in%20another%20region's%20file%20%7C%20check%20the%20file's%20region%20before%20reading%20a%20row%20%7C%20the%20region%20in%20%60household%60%2C%20compared%20with%20the%20edits'%20%60region_code%60%20%7C%0A%0A%20%20%20%20Together%20they%20make%20the%20write%20**idempotent**%3A%20running%20it%20twice%20leaves%20the%20database%20as%20running%20it%20once%20does%2C%20and%20the%20second%20run%20reports%20that%20it%20changed%20nothing.%20A%20refusal%20is%20an%20exception%20(%60raise%20ValueError(...)%60)%2C%20never%20an%20%60assert%60%3A%20Python%20skips%20every%20%60assert%60%20when%20it%20runs%20with%20%60-O%60%2C%20and%20a%20check%20that%20can%20be%20switched%20off%20is%20no%20check.%0A%0A%20%20%20%20**R's%20DBI**%20does%20the%20same%20steps%3A%20%60dbBegin(con)%60%2C%20%60dbExecute(con%2C%20%22UPDATE%20...%22)%60%2C%20%60dbCommit(con)%60%2C%20and%20%60dbRollback(con)%60%20in%20the%20error%20branch%20(or%20%60dbWithTransaction()%60%2C%20which%20rolls%20back%20by%20itself).%20DuckDB%20takes%20the%20same%20SQL%20from%20Python%3A%20%60con.execute(%22BEGIN%20TRANSACTION%22)%60%2C%20%60con.execute(%22COMMIT%22)%60%2C%20%60con.execute(%22ROLLBACK%22)%60.%0A%0A%20%20%20%20An%20%60UPDATE%60%20with%20%60%3F%60%20placeholders%20takes%20its%20values%20from%20a%20list%2C%20so%20a%20value%20can%20never%20change%20the%20statement.%20The%20table's%20and%20the%20column's%20names%20are%20written%20in%20double%20quotes%3A%20none%20of%20this%20survey's%20names%20needs%20them%2C%20and%20the%20habit%20costs%20nothing%20where%20one%20does.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20con.execute('UPDATE%20%22catch%22%20SET%20%22catch_operator_line%22%20%3D%20%3F%2C%20key_corrected%20%3D%201%20WHERE%20row_id%20%3D%20%3F'%2C%20%5B1%2C%203%5D)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20A%20table's%20columns%20come%20from%20DuckDB's%20%60DESCRIBE%60%3B%20the%20helper%20below%20returns%20their%20names%2C%20so%20a%20function%20can%20ask%20whether%20a%20table%20has%20the%20audit%20column%20yet.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20columns_of(con%3A%20duckdb.DuckDBPyConnection%2C%20table%3A%20str)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20names%20of%20a%20table's%20columns%2C%20from%20DuckDB's%20DESCRIBE.%22%22%22%0A%20%20%20%20return%20%5Brow%5B0%5D%20for%20row%20in%20con.execute(f'DESCRIBE%20%22%7Btable%7D%22').fetchall()%5D%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%203.9.%20The%20dry%20run%3A%20what%20would%20change%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20dry%20run%20is%20what%20a%20reviewer%20reads%20before%20saying%20yes%2C%20and%20what%20the%20write%20itself%20checks%20before%20it%20touches%20a%20row.%20It%20reads%20the%20database%20and%20changes%20nothing.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20an%20open%20DuckDB%20connection%20to%20one%20region's%20database%20and%20an%20edits%20table%20%E2%86%92%20the%20edits%20table%20with%20two%20columns%20added%3A%0A%0A%20%20%20%20-%20%60now%60%3A%20the%20value%20the%20row%20holds%20now%2C%20or%20missing%20if%20the%20row%20is%20not%20there%3B%0A%20%20%20%20-%20%60status%60%3A%20%60%22already%20edited%22%60%20if%20the%20row's%20audit%20flag%20is%201%3B%20else%20%60%22will%20change%22%60%20if%20%60now%60%20equals%20%60old%60%3B%20else%20%60%22conflict%22%60%20(the%20row%20is%20missing%2C%20or%20holds%20another%20value).%0A%0A%20%20%20%20%7C%20row_id%20%7C%20column%20%7C%20old%20%7C%20new%20%7C%20now%20%7C%20status%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%202%20%7C%20catch_operator_line%20%7C%202%20%7C%201%20%7C%201%20%7C%20already%20edited%20%7C%0A%20%20%20%20%7C%203%20%7C%20catch_operator_line%20%7C%202%20%7C%201%20%7C%202%20%7C%20will%20change%20%7C%0A%20%20%20%20%7C%203%20%7C%20catch_unit_no%20%7C%203%20%7C%201%20%7C%204%20%7C%20conflict%20%7C%0A%0A%20%20%20%20**First%2C%20the%20region%3A**%20read%20%60region_code%60%20from%20the%20database's%20%60household%60%20table.%20If%20any%20edit's%20%60region_code%60%20is%20another%20region%2C%20%60raise%20ValueError(...)%60%20with%20a%20message%20that%20names%20both.%20Compare%20the%20regions%20as%20numbers%3A%20region%2004's%20database%20stores%20%60%2204%22%60%20as%20text%2C%20the%20other%20two%20store%20%601%60%20and%20%607%60%20as%20numbers%2C%20and%20%60int()%60%20reads%20both.%0A%0A%20%20%20%20A%20table%20that%20has%20never%20been%20written%20has%20no%20audit%20column%20yet%3A%20read%20%60NULL%60%20in%20its%20place%20(%60columns_of%60%20tells%20you%20which).%0A%0A%20%20%20%20**Tools%3A**%20%60con.execute(sql%2C%20%5Bvalues%5D).fetchall()%60%20and%20%60.fetchone()%60%20(module%202)%2C%20%60edits.to_dict(%22records%22)%60%20for%20a%20loop%20over%20the%20edits%2C%20and%20%60columns_of%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20plan_write(con%3A%20duckdb.DuckDBPyConnection%2C%20edits%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20dry%20run%3A%20the%20edits%20with%20now%20(the%20row's%20current%20value)%20and%20status%20(will%20change%2C%20already%20edited%2C%20conflict).%0A%20%20%20%20Refuses%20edits%20for%20another%20region%20with%20a%20ValueError.%22%22%22%0A%20%20%20%20%23%20Exercise%203.9%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20database's%20region(s)%3A%20SELECT%20DISTINCT%20region_code%20FROM%20household%2C%20each%20as%20int()%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20the%20edits'%20regions%2C%20each%20as%20int()%3B%20raise%20ValueError%20naming%20both%20if%20an%20edit's%20region%20is%20not%20the%20database's%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20edit%20(edits.to_dict(%22records%22))%3A%20the%20flag%20column%20is%20AUDIT%20if%20columns_of(con%2C%20its%20table)%20has%20it%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20else%20NULL%3B%20read%20the%20row's%20value%20and%20flag%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20SELECT%20%22%3Ccolumn%3E%22%2C%20%3Cflag%3E%20FROM%20%22%3Ctable%3E%22%20WHERE%20row_id%20%3D%20%3F%20%20%20(with%20%5Brow_id%5D)%0A%20%20%20%20%23%20Step%203%3A%20status%3A%20no%20row%20-%3E%20%22conflict%22%3B%20flag%201%20-%3E%20%22already%20edited%22%3B%20value%20%3D%3D%20old%20-%3E%20%22will%20change%22%3B%20else%20%22conflict%22%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20edits%20with%20now%20and%20status%20added%20(assign)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.9%22)%0A%0A%0A%40app.function%0Adef%20tiny_rows(state%3A%20str%20%3D%20%22fresh%22)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20catch%20rows%20of%20one%20region's%20database%2C%20for%20the%20checks%20of%20Exercises%203.9%20and%203.10.%20%22fresh%22%3A%20never%20edited%3B%0A%20%20%20%20%22partly%20edited%22%3A%20row%202%20already%20corrected%20and%20flagged%3B%20%22changed%20since%22%3A%20row%203's%20unit%20number%20changed%20by%20someone%20else.%22%22%22%0A%20%20%20%20rows%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22row_id%22%3A%20%5B1%2C%202%2C%203%2C%204%2C%205%5D%2C%20%22hh_id%22%3A%20%5B5101%2C%205101%2C%205102%2C%205102%2C%205103%5D%2C%0A%20%20%20%20%20%20%20%20%22catch_operator_line%22%3A%20%5B1%2C%202%2C%202%2C%202%2C%201%5D%2C%20%22catch_unit_no%22%3A%20%5B1%2C%201%2C%203%2C%203%2C%203%5D%2C%20%22catch_line%22%3A%20%5B1%2C%202%2C%201%2C%202%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22catch_species%22%3A%20%5B101%2C%20104%2C%20201%2C%20101%2C%20104%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20if%20state%20%3D%3D%20%22partly%20edited%22%3A%0A%20%20%20%20%20%20%20%20rows.loc%5Brows%5B%22row_id%22%5D%20%3D%3D%202%2C%20%22catch_operator_line%22%5D%20%3D%201%0A%20%20%20%20%20%20%20%20rows%5BAUDIT%5D%20%3D%20pd.array(%5BNone%2C%201%2C%20None%2C%20None%2C%20None%5D%2C%20dtype%3D%22Int64%22)%0A%20%20%20%20if%20state%20%3D%3D%20%22changed%20since%22%3A%0A%20%20%20%20%20%20%20%20rows.loc%5Brows%5B%22row_id%22%5D%20%3D%3D%203%2C%20%22catch_unit_no%22%5D%20%3D%204%0A%20%20%20%20return%20rows%0A%0A%0A%40app.function%0Adef%20tiny_edits(region%3A%20str%20%3D%20%2201%22%2C%20missing_row%3A%20bool%20%3D%20False)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20edits%20for%20tiny_rows%3A%20row%202's%20line%2C%20and%20rows%203%20and%204's%20line%20and%20unit%20number.%20With%20missing_row%2C%20one%20more%20edit%0A%20%20%20%20for%20a%20row%20the%20table%20does%20not%20have.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(region%2C%205101%2C%20CATCH%2C%202%2C%20%22catch_operator_line%22%2C%202%2C%201%2C%20ONE_NEAR)%2C%0A%20%20%20%20%20%20%20%20(region%2C%205102%2C%20CATCH%2C%203%2C%20%22catch_operator_line%22%2C%202%2C%201%2C%20PAIRED)%2C%0A%20%20%20%20%20%20%20%20(region%2C%205102%2C%20CATCH%2C%203%2C%20%22catch_unit_no%22%2C%203%2C%201%2C%20PAIRED)%2C%0A%20%20%20%20%20%20%20%20(region%2C%205102%2C%20CATCH%2C%204%2C%20%22catch_operator_line%22%2C%202%2C%201%2C%20PAIRED)%2C%0A%20%20%20%20%20%20%20%20(region%2C%205102%2C%20CATCH%2C%204%2C%20%22catch_unit_no%22%2C%203%2C%201%2C%20PAIRED)%2C%0A%20%20%20%20%5D%0A%20%20%20%20if%20missing_row%3A%0A%20%20%20%20%20%20%20%20rows.append((region%2C%205104%2C%20CATCH%2C%2099%2C%20%22catch_unit_no%22%2C%202%2C%201%2C%20ONE_NEAR))%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3Dedit_columns())%0A%0A%0A%40app.function%0Adef%20fill_database(con%3A%20duckdb.DuckDBPyConnection%2C%20rows%3A%20pd.DataFrame%2C%20region)%20-%3E%20None%3A%0A%20%20%20%20%22%22%22A%20region's%20database%20in%20miniature%3A%20a%20household%20table%20that%20holds%20the%20region%2C%20and%20the%20catch%20table.%20A%20region%20given%0A%20%20%20%20as%20text%20(%2204%22)%20is%20stored%20as%20text%2C%20as%20region%2004's%20database%20stores%20it%3B%20a%20number%2C%20as%20a%20number.%22%22%22%0A%20%20%20%20stored%20%3D%20f%22'%7Bregion%7D'%22%20if%20isinstance(region%2C%20str)%20else%20str(int(region))%0A%20%20%20%20con.register(%22incoming%22%2C%20rows)%0A%20%20%20%20con.execute(f%22CREATE%20TABLE%20household%20AS%20SELECT%20DISTINCT%20hh_id%2C%20%7Bstored%7D%20AS%20region_code%20FROM%20incoming%22)%0A%20%20%20%20con.execute(f'CREATE%20TABLE%20%22%7BCATCH%7D%22%20AS%20SELECT%20*%20FROM%20incoming')%0A%20%20%20%20con.unregister(%22incoming%22)%0A%0A%0A%40app.function%0Adef%20plan_story(rows%3A%20pd.DataFrame%2C%20edits%3A%20pd.DataFrame%2C%20region)%3A%0A%20%20%20%20%22%22%22Exercise%203.9%20on%20a%20small%20database%20in%20memory%3A%20the%20dry%20run's%20table%2C%20or%20the%20error%20it%20refused%20with.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20fill_database(con%2C%20rows%2C%20region)%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20planned%20%3D%20plan_write(con%2C%20edits)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20a%20refusal%20is%20the%20answer%20for%20an%20edit%20of%20another%20region%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20f%22refused%3A%20%7Btype(error).__name__%7D%22%0A%20%20%20%20%20%20%20%20return%20as_rows(planned%2C%20%5B%22row_id%22%2C%20%22column%22%5D%2C%20edit_columns()%20%2B%20%5B%22now%22%2C%20%22status%22%5D)%0A%20%20%20%20finally%3A%0A%20%20%20%20%20%20%20%20con.close()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.9%22%2C%20plan_story%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20database%20never%20edited%22%2C%20(tiny_rows(%22fresh%22)%2C%20tiny_edits()%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20database%20with%20row%202%20already%20edited%20and%20flagged%22%2C%20(tiny_rows(%22partly%20edited%22)%2C%20tiny_edits()%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20database%20where%20someone%20changed%20row%203%20since%22%2C%20(tiny_rows(%22changed%20since%22)%2C%20tiny_edits()%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22an%20edit%20for%20a%20row%20the%20table%20does%20not%20have%22%2C%20(tiny_rows(%22fresh%22)%2C%20tiny_edits(missing_row%3DTrue)%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20edits%20against%20region%2001's%20database%22%2C%20(tiny_rows(%22fresh%22)%2C%20tiny_edits(%2204%22)%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20database%2C%20which%20stores%20its%20region%20as%20text%22%2C%20(tiny_rows(%22fresh%22)%2C%20tiny_edits(%2204%22)%2C%20%2204%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22databases%22%2C%20hint%3D%22Check%20the%20region%20first%20(as%20numbers)%2C%20then%20read%20each%20row's%20value%20and%20flag%3A%20flag%201%20is%20already%20edited%2C%20value%20%3D%3D%20old%20will%20change%2C%20anything%20else%20a%20conflict.%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%20dry%20run%20on%20a%20small%20database%20where%20row%202%20is%20already%20edited%0A%20%20%20%20try_it(plan_story%2C%20tiny_rows(%22partly%20edited%22)%2C%20tiny_edits()%2C%201)%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.9%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%20Region%2004's%20edits%20against%20region%2001's%20database%20get%20a%20table%2C%20not%20%60refused%60%20%7C%20The%20region%20was%20not%20checked%2C%20so%20row%20ids%20of%20another%20region's%20file%20were%20read.%20%7C%20step%201%20%7C%0A%20%20%20%20%7C%20Region%2004's%20own%20database%20is%20%60refused%60%20%7C%20%60%2204%22%60%20compared%20with%20%604%60%3A%20text%20against%20a%20number.%20%7C%20%60int()%60%20on%20both%20sides%20%7C%0A%20%20%20%20%7C%20Row%202%20of%20the%20partly%20edited%20database%20is%20%60conflict%60%20%7C%20The%20value%20was%20tested%20before%20the%20flag%3B%20an%20edited%20row%20holds%20the%20new%20value.%20%7C%20test%20the%20flag%20first%20%7C%0A%20%20%20%20%7C%20*BinderException%3A%20Binder%20Error%3A%20Referenced%20column%20%22key_corrected%22%20not%20found%20in%20FROM%20clause!*%20%7C%20The%20audit%20column%20read%20from%20a%20table%20that%20does%20not%20have%20it%20yet.%20%7C%20%60NULL%60%20when%20%60columns_of%60%20lacks%20it%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20cannot%20unpack%20non-iterable%20NoneType%20object*%20%7C%20%60fetchone()%60%20gave%20%60None%60%20for%20a%20missing%20row.%20%7C%20test%20%60found%20is%20None%60%20first%20%7C%0A%20%20%20%20%7C%20*InvalidInputException%3A%20Invalid%20Input%20Error%3A%20Values%20were%20not%20provided%20for%20the%20following%20parameters%3A%201*%20%7C%20The%20%60%3F%60%20in%20the%20query%2C%20but%20no%20list%20of%20values%20after%20it.%20%7C%20%60%5Bedit%5B%22row_id%22%5D%5D%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%20Exercise%203.10.%20The%20write%2C%20all%20or%20nothing%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20one%20step%20in%20the%20module%20that%20changes%20a%20database.%20Everything%20before%20it%20can%20be%20run%20any%20number%20of%20times%3B%20this%20one%20is%20safe%20to%20run%20more%20than%20once%20only%20because%20of%20how%20it%20is%20written.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20a%20region's%20database%20file%2C%20an%20edits%20table%2C%20and%20%60apply%60%20(default%20%60False%60)%20%E2%86%92%20the%20dry%20run's%20table%2C%20with%20%60%22will%20change%22%60%20read%20as%20%60%22changed%22%60%20when%20the%20edits%20were%20written.%0A%0A%20%20%20%20-%20**%60apply%3DFalse%60**%20(the%20default)%3A%20open%20the%20file%20read-only%20(%60duckdb.connect(str(database)%2C%20read_only%3DTrue)%60)%20and%20return%20%60plan_write%60's%20table.%20Nothing%20is%20written.%0A%20%20%20%20-%20**%60apply%3DTrue%60**%3A%20open%20it%20for%20writing%2C%20and%20in%20one%20transaction%3A%20plan%20again%3B%20if%20any%20edit%20is%20a%20%60conflict%60%2C%20%60raise%20ValueError(...)%60%3B%20add%20the%20audit%20column%20to%20each%20table%20you%20change%20(%60ALTER%20TABLE%20...%20ADD%20COLUMN%20IF%20NOT%20EXISTS%20key_corrected%20INTEGER%60)%3B%20run%20one%20%60UPDATE%60%20per%20%60will%20change%60%20edit%2C%20setting%20the%20column%20and%20the%20flag%3B%20%60COMMIT%60.%20On%20any%20error%2C%20%60ROLLBACK%60%2C%20then%20raise%20the%20error%20again.%0A%0A%20%20%20%20Open%20the%20file%20with%20%60with%20duckdb.connect(...)%20as%20con%3A%60%20so%20it%20is%20closed%20when%20the%20function%20returns%2C%20even%20after%20an%20error%3A%20a%20file%20still%20open%20in%20one%20connection%20cannot%20be%20opened%20again%20with%20other%20settings.%0A%0A%20%20%20%20%7C%20run%20%7C%20statuses%20it%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60write_back(path%2C%20edits)%60%20%7C%20%60will%20change%60%20%C3%975%2C%20nothing%20written%20%7C%0A%20%20%20%20%7C%20%60write_back(path%2C%20edits%2C%20apply%3DTrue)%60%20%7C%20%60changed%60%20%C3%975%20%7C%0A%20%20%20%20%7C%20%60write_back(path%2C%20edits%2C%20apply%3DTrue)%60%20again%20%7C%20%60already%20edited%60%20%C3%975%2C%20nothing%20written%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60plan_write%60%20(Exercise%203.9)%2C%20and%20this%20transaction%20shape%20(Python%20for%20Data%20Management%20teaches%20it%20too)%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20con.execute(%22BEGIN%20TRANSACTION%22)%0A%20%20%20%20try%3A%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%20%20%23%20plan%2C%20refuse%20on%20conflict%2C%20add%20the%20column%2C%20update%0A%20%20%20%20%20%20%20%20con.execute(%22COMMIT%22)%0A%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20con.execute(%22ROLLBACK%22)%20%20%20%20%20%23%20undo%20everything%20since%20BEGIN%0A%20%20%20%20%20%20%20%20raise%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20and%20let%20the%20caller%20know%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20write_back(database%2C%20edits%3A%20pd.DataFrame%2C%20apply%3A%20bool%20%3D%20False)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Write%20the%20edits%20into%20a%20region's%20database%20file%2C%20all%20or%20nothing%2C%20flagging%20each%20edited%20row.%20apply%3DFalse%20(the%0A%20%20%20%20default)%20is%20a%20dry%20run%3A%20it%20reads%20the%20file%20and%20writes%20nothing.%22%22%22%0A%20%20%20%20%23%20Exercise%203.10%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20open%20the%20file%20with%20%60with%20duckdb.connect(str(database)%2C%20read_only%3Dnot%20apply)%20as%20con%3A%60%0A%20%20%20%20%23%20Step%202%3A%20if%20not%20apply%2C%20return%20plan_write(con%2C%20edits)%3A%20the%20dry%20run%0A%20%20%20%20%23%20Step%203%3A%20BEGIN%20TRANSACTION%3B%20then%20try%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20plan%20%3D%20plan_write(con%2C%20edits)%3B%20raise%20ValueError%20if%20any%20status%20is%20%22conflict%22%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20ALTER%20TABLE%20%22%3Ctable%3E%22%20ADD%20COLUMN%20IF%20NOT%20EXISTS%20key_corrected%20INTEGER%2C%20for%20each%20table%20with%20a%20%22will%20change%22%20edit%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20for%20each%20%22will%20change%22%20edit%3A%20UPDATE%20%22%3Ctable%3E%22%20SET%20%22%3Ccolumn%3E%22%20%3D%20%3F%2C%20key_corrected%20%3D%201%20WHERE%20row_id%20%3D%20%3F%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20COMMIT%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20except%20Exception%3A%20ROLLBACK%2C%20then%20raise%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20plan%20with%20%22will%20change%22%20replaced%20by%20%22changed%22%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.10%22)%0A%0A%0A%40app.function%0Adef%20table_state(path)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20small%20database's%20catch%20table%20as%20it%20stands%3A%20its%20columns%20and%20rows%2C%20in%20row-id%20order.%22%22%22%0A%20%20%20%20with%20duckdb.connect(str(path)%2C%20read_only%3DTrue)%20as%20con%3A%0A%20%20%20%20%20%20%20%20return%20%7B%22columns%22%3A%20columns_of(con%2C%20CATCH)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22rows%22%3A%20%5Blist(row)%20for%20row%20in%20con.execute(f'SELECT%20*%20FROM%20%22%7BCATCH%7D%22%20ORDER%20BY%20row_id').fetchall()%5D%7D%0A%0A%0A%40app.function%0Adef%20write_back_story(rows%3A%20pd.DataFrame%2C%20edits%3A%20pd.DataFrame%2C%20region)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22Exercise%203.10%20on%20a%20small%20database%20file%20in%20scratch%2F%3A%20a%20dry%20run%2C%20a%20write%2C%20the%20same%20write%20again%2C%20and%20what%20each%20left.%22%22%22%0A%20%20%20%20path%20%3D%20SCRATCH%20%2F%20%22check_3_10.duckdb%22%0A%20%20%20%20path.unlink(missing_ok%3DTrue)%0A%20%20%20%20path.with_name(path.name%20%2B%20%22.wal%22).unlink(missing_ok%3DTrue)%0A%20%20%20%20with%20duckdb.connect(str(path))%20as%20con%3A%0A%20%20%20%20%20%20%20%20fill_database(con%2C%20rows%2C%20region)%0A%20%20%20%20before%20%3D%20table_state(path)%0A%0A%20%20%20%20def%20run(apply%3A%20bool)%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20write_back(path%2C%20edits%2C%20apply%3Dapply)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20a%20refusal%20is%20the%20answer%20for%20a%20plan%20that%20no%20longer%20fits%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20f%22refused%3A%20%7Btype(error).__name__%7D%22%0A%20%20%20%20%20%20%20%20return%20sorted(%5Bint(r)%2C%20c%2C%20s%5D%20for%20r%2C%20c%2C%20s%20in%20zip(result%5B%22row_id%22%5D%2C%20result%5B%22column%22%5D%2C%20result%5B%22status%22%5D))%0A%0A%20%20%20%20story%20%3D%20%7B%22dry%20run%22%3A%20run(False)%2C%20%22unchanged%20by%20the%20dry%20run%22%3A%20table_state(path)%20%3D%3D%20before%7D%0A%20%20%20%20story%5B%22write%22%5D%20%3D%20run(True)%0A%20%20%20%20story%5B%22write%20again%22%5D%20%3D%20run(True)%0A%20%20%20%20story%5B%22table%20after%22%5D%20%3D%20table_state(path)%0A%20%20%20%20story%5B%22unchanged%20in%20the%20end%22%5D%20%3D%20story%5B%22table%20after%22%5D%20%3D%3D%20before%0A%20%20%20%20return%20story%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.10%22%2C%20write_back_story%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20database%20never%20edited%3A%20dry%20run%2C%20write%2C%20write%20again%22%2C%20(tiny_rows(%22fresh%22)%2C%20tiny_edits()%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20database%20with%20row%202%20already%20edited%20and%20flagged%22%2C%20(tiny_rows(%22partly%20edited%22)%2C%20tiny_edits()%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20database%20where%20someone%20changed%20row%203%20since%20the%20edits%20were%20made%22%2C%20(tiny_rows(%22changed%20since%22)%2C%20tiny_edits()%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20edits%20against%20region%2001's%20database%22%2C%20(tiny_rows(%22fresh%22)%2C%20tiny_edits(%2204%22)%2C%201))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22stories%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Dry%20run%20by%20default%20(read-only)%3B%20with%20apply%3DTrue%20one%20transaction%20that%20refuses%20any%20conflict%2C%20adds%20key_corrected%2C%20updates%20each%20'will%20change'%20edit%2C%20and%20rolls%20back%20on%20error.%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%20three%20runs%20on%20a%20small%20database%2C%20and%20the%20table%20they%20leave%0A%20%20%20%20def%20_try_write()%3A%0A%20%20%20%20%20%20%20%20story%20%3D%20attempt(write_back_story%2C%20tiny_rows(%22fresh%22)%2C%20tiny_edits()%2C%201)%0A%20%20%20%20%20%20%20%20if%20story%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Exercises%203.9%20and%203.10%20both%20need%20their%20%60raise%60%20line%20deleted.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20after%20%3D%20pd.DataFrame(story%5B%22table%20after%22%5D%5B%22rows%22%5D%2C%20columns%3Dstory%5B%22table%20after%22%5D%5B%22columns%22%5D)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**Dry%20run%3A**%20%7Bstory%5B'dry%20run'%5D%7D%20%20%5Cnunchanged%20by%20it%3A%20**%7Bstory%5B'unchanged%20by%20the%20dry%20run'%5D%7D**%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**Write%3A**%20%7Bstory%5B'write'%5D%7D%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**Write%20again%3A**%20%7Bstory%5B'write%20again'%5D%7D%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20after%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_try_write()%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.10%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%20dry%20run%20changed%20the%20table%20%7C%20The%20dry%20run%20wrote%2C%20or%20the%20file%20was%20opened%20for%20writing%20and%20changed.%20%7C%20%60read_only%3Dnot%20apply%60%2C%20and%20%60return%60%20before%20%60BEGIN%60%20%7C%0A%20%20%20%20%7C%20%60write%20again%60%20says%20%60changed%60%20%7C%20The%20second%20run%20did%20not%20skip%20flagged%20rows.%20%7C%20%60plan_write%60's%20%60already%20edited%60%20%7C%0A%20%20%20%20%7C%20After%20a%20refusal%2C%20the%20table%20has%20a%20%60key_corrected%60%20column%20%7C%20The%20%60ALTER%20TABLE%60%20ran%20outside%20the%20transaction%2C%20or%20no%20%60ROLLBACK%60.%20%7C%20%60BEGIN%60%20before%20the%20%60ALTER%60%20%7C%0A%20%20%20%20%7C%20%60refused%3A%20RuntimeError%60%20%7C%20The%20conflict%20raised%20another%20kind%20of%20error.%20%7C%20%60raise%20ValueError(...)%60%20%7C%0A%20%20%20%20%7C%20*ConnectionException%3A%20Connection%20Error%3A%20Can't%20open%20a%20connection%20to%20same%20database%20file%20with%20a%20different%20configuration%20than%20existing%20connections*%20%7C%20A%20connection%20to%20the%20file%20was%20left%20open.%20%7C%20%60with%20duckdb.connect(...)%20as%20con%3A%60%20%7C%0A%20%20%20%20%7C%20*TransactionException%3A%20TransactionContext%20Error%3A%20cannot%20start%20a%20transaction%20within%20a%20transaction*%20%7C%20An%20earlier%20failed%20run%20left%20a%20transaction%20open%2C%20because%20%60ROLLBACK%60%20was%20missing.%20%7C%20%60except%20Exception%3A%20ROLLBACK%3B%20raise%60%20%7C%0A%20%20%20%20%7C%20Only%20one%20of%20a%20row's%20two%20parts%20changed%20%7C%20The%20flag%20was%20required%20to%20be%20empty%20in%20the%20%60UPDATE%60%2C%20and%20the%20first%20part's%20update%20set%20it.%20%7C%20update%20every%20%60will%20change%60%20edit%3B%20plan%20once%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%20On%20the%20survey%3A%20a%20copy%2C%20the%20dry%20run%2C%20then%20the%20write%0A%0A%20%20%20%20Now%20the%20survey%20itself%2C%20one%20region%20at%20a%20time%2C%20as%20each%20region%20is%20its%20own%20database.%20The%20cell%20below%20copies%20the%20region's%20file%20from%20%60data%2F%60%20into%20%60scratch%2F%60%2C%20and%20every%20step%20after%20it%20works%20on%20the%20copy.%20Running%20the%20cell%20again%20gives%20a%20fresh%20copy.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20REGION%20%3D%20%2201%22%20%20%23%20the%20region%20to%20write%3B%20change%20it%20to%20%2204%22%20or%20%2207%22%20and%20run%20this%20cell%20again%0A%20%20%20%20working_copy%20%3D%20SCRATCH%20%2F%20PATHS.region(REGION).name%0A%20%20%20%20working_copy.with_name(working_copy.name%20%2B%20%22.wal%22).unlink(missing_ok%3DTrue)%20%20%23%20a%20log%20left%20by%20an%20interrupted%20run%20belongs%20to%20an%20older%20copy%0A%20%20%20%20shutil.copyfile(PATHS.region(REGION)%2C%20working_copy)%20%20%23%20data%2F%20is%20read%20here%20and%20never%20opened%20for%20writing%0A%20%20%20%20mo.md(f%22A%20fresh%20copy%20of%20%60data%2F%7Bworking_copy.name%7D%60%20is%20at%20%60scratch%2F%7Bworking_copy.name%7D%60.%22)%0A%20%20%20%20return%20REGION%2C%20working_copy%0A%0A%0A%40app.cell%0Adef%20_(REGION%2C%20all_edits%2C%20working_copy)%3A%0A%20%20%20%20%23%20The%20dry%20run%20on%20the%20copy%3A%20what%20the%20edits%20would%20change%2C%20and%20nothing%20written%0A%20%20%20%20mo.stop(all_edits%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.8%22))%0A%20%20%20%20region_edits%20%3D%20all_edits%5Ball_edits%5B%22region_code%22%5D%20%3D%3D%20REGION%5D%0A%20%20%20%20dry_run%20%3D%20attempt(write_back%2C%20working_copy%2C%20region_edits)%0A%20%20%20%20mo.stop(dry_run%20is%20NOT_YET%2C%20waiting_for(%22Exercises%203.9%20and%203.10%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(f%22**DRY%20RUN%3A%20nothing%20was%20written.**%20%7Blen(dry_run)%7D%20edits%20to%20%7Bdry_run%5B'row_id'%5D.nunique()%7D%20catch%20rows%20of%20region%20%7BREGION%7D%3B%20statuses%3A%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%2B%20%22%2C%20%22.join(f%22%7Bcount%7D%20%7Bstatus%7D%22%20for%20status%2C%20count%20in%20dry_run%5B%22status%22%5D.value_counts().items())%20%2B%20%22.%22)%2C%20kind%3D%22info%22)%2C%0A%20%20%20%20%20%20%20%20dry_run%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20dry_run%2C%20region_edits%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20apply_switch%20%3D%20mo.ui.switch(label%3D%22**apply**%3A%20write%20these%20edits%20into%20the%20copy%22%2C%20value%3DFalse)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22Read%20the%20dry%20run%20above.%20When%20it%20is%20what%20you%20expect%2C%20switch%20**apply**%20on%3A%20that%20is%20the%20explicit%20%60apply%3DTrue%60.%22)%2C%20apply_switch%5D)%0A%20%20%20%20return%20(apply_switch%2C)%0A%0A%0A%40app.cell%0Adef%20_(REGION%2C%20apply_switch%2C%20dry_run%2C%20region_edits%2C%20working_copy)%3A%0A%20%20%20%20%23%20The%20write%2C%20only%20when%20apply%20is%20on%0A%20%20%20%20mo.stop(not%20apply_switch.value%2C%20mo.callout(mo.md(f%22**Not%20written.**%20The%20copy%20is%20as%20it%20came%20from%20%60data%2F%60%3B%20%7B(dry_run%5B'status'%5D%20%3D%3D%20'will%20change').sum()%7D%20edits%20are%20waiting%20for%20**apply**.%22)%2C%20kind%3D%22neutral%22))%0A%20%20%20%20first_write%20%3D%20write_back(working_copy%2C%20region_edits%2C%20apply%3DTrue)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(f%22**Written%20to%20the%20copy%20of%20region%20%7BREGION%7D%3A**%20%22%20%2B%20%22%2C%20%22.join(f%22%7Bcount%7D%20%7Bstatus%7D%22%20for%20status%2C%20count%20in%20first_write%5B%22status%22%5D.value_counts().items())%20%2B%20%22.%22)%2C%20kind%3D%22success%22)%2C%0A%20%20%20%20%20%20%20%20first_write%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(first_write%2C)%0A%0A%0A%40app.cell%0Adef%20_(first_write%2C%20region_edits%2C%20working_copy)%3A%0A%20%20%20%20%23%20The%20same%20write%20again%3A%20every%20edited%20row%20is%20flagged%2C%20so%20it%20changes%20nothing%0A%20%20%20%20second_write%20%3D%20write_back(working_copy%2C%20region_edits%2C%20apply%3DTrue)%0A%20%20%20%20_changed%20%3D%20(second_write%5B%22status%22%5D%20%3D%3D%20%22changed%22).sum()%0A%20%20%20%20mo.md(f%22**The%20same%20write%2C%20run%20again%3A**%20%7B_changed%7D%20changed%2C%20%7B(second_write%5B'status'%5D%20%3D%3D%20'already%20edited').sum()%7D%20already%20edited%20(the%20first%20run%20changed%20%7B(first_write%5B'status'%5D%20%3D%3D%20'changed').sum()%7D).%20Running%20it%20twice%20left%20the%20copy%20as%20running%20it%20once%20did.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(REGION%2C%20decisions%2C%20first_write%2C%20working_copy)%3A%0A%20%20%20%20%23%20The%20proof%3A%20match%20the%20keys%20again%20on%20the%20copy%3B%20compare%20it%20with%20data%2F%2C%20row%20by%20row%0A%20%20%20%20_after%20%3D%20course.table(CATCH%2C%20folder%3DSCRATCH)%0A%20%20%20%20_after%20%3D%20_after%5B_after%5B%22region_code%22%5D%20%3D%3D%20REGION%5D%0A%20%20%20%20_left%2C%20_%20%3D%20leftovers_in_sql(in_region(units%2C%20REGION)%2C%20_after)%0A%20%20%20%20_manual%20%3D%20in_region(decisions%2C%20REGION)%0A%20%20%20%20_manual%20%3D%20_manual%5B_manual%5B%22case%22%5D%20%3D%3D%20%22manual%22%5D%0A%20%20%20%20_same%20%3D%20_left%5BKEY%5D.merge(_manual%5BKEY%5D%2C%20on%3DKEY%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%5B%22_merge%22%5D.eq(%22both%22).all()%0A%20%20%20%20_parts%20%3D%20%5Bf%22catch_%7Bpart%7D%22%20for%20part%20in%20PARTS%5D%0A%20%20%20%20with%20duckdb.connect(str(PATHS.region(REGION))%2C%20read_only%3DTrue)%20as%20_con%3A%0A%20%20%20%20%20%20%20%20_original%20%3D%20_con.execute(f'SELECT%20row_id%2C%20%7B%22%2C%20%22.join(_parts)%7D%20FROM%20%22%7BCATCH%7D%22%20ORDER%20BY%201').df()%0A%20%20%20%20%20%20%20%20_audit_in_data%20%3D%20AUDIT%20in%20columns_of(_con%2C%20CATCH)%0A%20%20%20%20with%20duckdb.connect(str(working_copy)%2C%20read_only%3DTrue)%20as%20_con%3A%0A%20%20%20%20%20%20%20%20_copy%20%3D%20_con.execute(f'SELECT%20row_id%2C%20%7B%22%2C%20%22.join(_parts)%7D%20FROM%20%22%7BCATCH%7D%22%20ORDER%20BY%201').df()%0A%20%20%20%20%20%20%20%20_flagged%20%3D%20_con.execute(f'SELECT%20count(*)%20FROM%20%22%7BCATCH%7D%22%20WHERE%20%7BAUDIT%7D%20%3D%201').fetchone()%5B0%5D%0A%20%20%20%20_rows_changed%20%3D%20int((_original%5B_parts%5D%20!%3D%20_copy%5B_parts%5D).any(axis%3D1).sum())%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20check%20on%20the%20copy%20of%20region%20%7BREGION%7D%20%7C%20result%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20catch%20keys%20with%20no%20unit%20after%20the%20write%20%7C%20**%7Blen(_left)%7D**%2C%20the%20same%20keys%20as%20the%20manual%20cases%3A%20**%7B_same%7D**%20%7C%0A%20%20%20%20%7C%20catch%20rows%20whose%20key%20differs%20from%20%60data%2F%60%20%7C%20**%7B_rows_changed%7D**%20%7C%0A%20%20%20%20%7C%20rows%20with%20%60%7BAUDIT%7D%20%3D%201%60%20%7C%20**%7B_flagged%7D**%20%7C%0A%20%20%20%20%7C%20rows%20the%20edits%20name%20%7C%20**%7Bfirst_write%5B'row_id'%5D.nunique()%7D**%20%7C%0A%20%20%20%20%7C%20%60data%2F%7BPATHS.region(REGION).name%7D%60%20has%20a%20%60%7BAUDIT%7D%60%20column%20%7C%20**%7B_audit_in_data%7D**%3A%20the%20original%20was%20never%20written%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%20After%20the%20write%2C%20matching%20the%20keys%20again%20finds%20only%20the%20keys%20referred%20to%20a%20person%3B%20every%20changed%20row%20is%20flagged%20and%20named%20in%20the%20edits%3B%20the%20survey's%20own%20file%20is%20as%20it%20came.%20Run%20from%20a%20terminal%20as%20a%20script%20(module%207%20builds%20one)%2C%20%60apply%60%20becomes%20an%20%60--apply%60%20flag%2C%20and%20the%20dry%20run%20is%20what%20the%20bare%20command%20does.%0A%0A%20%20%20%20%23%23%23%20The%20table%20a%20reviewer%20reads%0A%0A%20%20%20%20The%20module%20ends%20where%20an%20editor's%20day%20starts%3A%20one%20table%20with%20every%20proposed%20edit%20and%20every%20referral%2C%20each%20with%20its%20reason%2C%20written%20to%20%60scratch%2Fkey_matching_review.csv%60.%20A%20reviewer%20reads%20it%2C%20an%20editor%20answers%20its%20questions%2C%20and%20only%20then%20does%20anyone%20switch%20**apply**%20on.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(all_edits%2C%20referrals)%3A%0A%20%20%20%20%23%20Every%20edit%20and%20every%20referral%2C%20in%20one%20table%20for%20the%20reviewer%2C%20saved%20beside%20the%20copies%0A%20%20%20%20mo.stop(all_edits%20is%20NOT_YET%2C%20waiting_for(%22Exercise%203.8%22))%0A%20%20%20%20_edits%20%3D%20all_edits.assign(action%3D%22edit%22%2C%20question%3D%22%22)%0A%20%20%20%20_referred%20%3D%20referrals.rename(columns%3D%7B%22what%22%3A%20%22item%22%7D).assign(action%3D%22refer%22%2C%20table%3DCATCH)%0A%20%20%20%20_referred.loc%5B_referred%5B%22item%22%5D%20%3D%3D%20%22unit%22%2C%20%22table%22%5D%20%3D%20%22fishing_unit%22%0A%20%20%20%20review%20%3D%20pd.concat(%5B_edits%2C%20_referred%5D%2C%20ignore_index%3DTrue)%5B%0A%20%20%20%20%20%20%20%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22action%22%2C%20%22table%22%2C%20%22row_id%22%2C%20%22column%22%2C%20%22old%22%2C%20%22new%22%2C%20%22operator_line%22%2C%20%22unit_no%22%2C%20%22reason%22%2C%20%22question%22%5D%0A%20%20%20%20%5D.sort_values(%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22action%22%2C%20%22row_id%22%2C%20%22column%22%5D%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%0A%20%20%20%20review.to_csv(SCRATCH%20%2F%20%22key_matching_review.csv%22%2C%20index%3DFalse%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7B(review%5B'action'%5D%20%3D%3D%20'edit').sum()%7D%20edits**%20and%20**%7B(review%5B'action'%5D%20%3D%3D%20'refer').sum()%7D%20referrals**%2C%20saved%20to%20%60scratch%2Fkey_matching_review.csv%60.%20A%20referral%20names%20its%20key%20(%60operator_line%60%2C%20%60unit_no%60)%3B%20an%20edit%20names%20its%20row%20and%20column.%22)%2C%0A%20%20%20%20%20%20%20%20review%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%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20use%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20find%20the%20orphans%20of%20every%20child%20table%20%7C%20one%20function%20that%20renames%20a%20table's%20parts%20by%20its%20prefix%2C%20then%20an%20anti-join%3B%20a%20loop%20over%20the%20tables%20%7C%20the%20prefix%20read%20from%20the%20columns%2C%20so%20no%20table%20is%20skipped%20%7C%0A%20%20%20%20%7C%20check%20operators%20against%20the%20roster%20%7C%20units%20counted%20per%20operator%2C%20%60merge(left_on%3D...%2C%20right_on%3D...%2C%20how%3D%22left%22)%60%2C%20%60np.select%60%2C%20then%20an%20anti-join%20the%20other%20way%20%7C%20one%20row%20per%20operator%3B%20a%20blank%20flag%20or%20count%20filled%20before%20it%20is%20compared%20%7C%0A%20%20%20%20%7C%20find%20the%20rows%20with%20no%20match%20%7C%20%60merge(how%3D%22left_anti%22)%60%2C%20polars%20%60how%3D%22anti%22%60%2C%20SQL%20%60ANTI%20JOIN%60%20%7C%20the%20whole%20key%2C%20the%20region%20included%3B%20distinct%20keys%2C%20with%20their%20row%20counts%20%7C%0A%20%20%20%20%7C%20see%20both%20directions%20in%20one%20join%20%7C%20%60merge(how%3D%22outer%22%2C%20indicator%3DTrue)%60%20%7C%20distinct%20keys%20on%20both%20sides%20%7C%0A%20%20%20%20%7C%20keep%20the%20rows%20that%20have%20a%20match%20%7C%20a%20join%20to%20the%20other%20side's%20distinct%20keys%20(a%20semi-join)%20%7C%20%60validate%3D%60%3A%20an%20inner%20join%20to%20every%20row%20multiplies%20%7C%0A%20%20%20%20%7C%20pair%20leftovers%20within%20a%20household%20%7C%20%60groupby(...).cumcount()%20%2B%201%60%20and%20an%20outer%20join%20%7C%20position%20says%20that%20a%20key%20is%20wrong%2C%20not%20which%20part%20%7C%0A%20%20%20%20%7C%20find%20which%20part%20differs%20%7C%20a%20join%20on%20the%20household%2C%20then%20part%20by%20part%20%7C%20the%20units'%20distinct%20keys%3B%20%60validate%3D%22many_to_many%22%60%20when%20intended%20%7C%0A%20%20%20%20%7C%20decide%20by%20rule%20%7C%20%60np.select%60%2C%20%60case_when()%60%2C%20%60CASE%20WHEN%60%20%7C%20the%20rules'%20order%3A%20the%20first%20that%20holds%20decides%20%7C%0A%20%20%20%20%7C%20turn%20decisions%20into%20edits%20%7C%20a%20join%20back%20to%20the%20rows%2C%20one%20edit%20per%20value%20%7C%20the%20database's%20own%20names%20and%20row%20ids%20%7C%0A%20%20%20%20%7C%20write%20edits%20back%20%7C%20a%20copy%2C%20a%20dry%20run%2C%20an%20explicit%20%60apply%60%2C%20a%20transaction%2C%20an%20audit%20flag%20%7C%20a%20second%20run%20changes%20nothing%3B%20a%20stale%20plan%20is%20refused%20%7C%0A%0A%20%20%20%20Next%2C%20module%204%20(%6030_edit_rules.py%60)%20checks%20the%20values%20inside%20the%20rows%3A%20codes%20outside%20the%20codebook%2C%20ages%20and%20tonnages%20out%20of%20range%2C%20items%20that%20contradict%20each%20other%2C%20and%20duplicates%2C%20written%20as%20edit%20rules%20a%20program%20runs.%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.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
30abe94b3652e836eaeb91d7cf13d4e9