import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Get%20the%20tables%3A%20from%20regional%20databases%20to%20one%20flat%20table%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20io%0A%20%20%20%20import%20sqlite3%0A%20%20%20%20import%20tempfile%0A%20%20%20%20import%20time%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%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%20where%20the%20survey%20is%2C%20its%20code%20widths%20and%20keys%2C%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20AREA_CODE_WIDTH%2C%20NOT_YET%2C%20PATHS%2C%20REGIONS%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20%23%20A%20folder%20of%20its%20own%20for%20the%20files%20this%20notebook%20writes%2C%20so%20the%20course%20folder%20stays%20as%20it%20came%0A%20%20%20%20SCRATCH%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22svy-flat-table-%22))%0A%20%20%20%20%23%20The%20name%20each%20region's%20database%20is%20attached%20under%20in%20one%20connection%2C%20its%20file's%20name%3A%20sfh2025_r01%2C%20sfh2025_r04%2C%20sfh2025_r07%0A%20%20%20%20DATABASES%20%3D%20%5Bf%22%7Bcourse.SURVEY%7D_r%7Breg%7D%22%20for%20reg%20in%20REGIONS%5D%0A%20%20%20%20%23%20The%20flat%20table's%20columns%3A%20the%20household's%20area%20codes%2C%20the%20unit's%20key%20and%20answers%2C%20and%20the%20household's%20stratum%2C%20area%20and%20final%20weight%0A%20%20%20%20UNIT_COLUMNS%20%3D%20%5B%22hh_id%22%2C%20%22operator_line%22%2C%20%22unit_no%22%2C%20%22unit_sold_share%22%2C%20%22unit_ownership%22%2C%20%22unit_uses_boat%22%2C%20%22unit_boats%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22unit_scale%22%2C%20%22unit_waters%22%2C%20%22unit_months_active%22%2C%20%22unit_hired_crew%22%2C%20%22unit_assisted%22%5D%0A%20%20%20%20DESIGN_COLUMNS%20%3D%20%5B%22sel_stratum%22%2C%20%22sel_ea%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20FLAT_COLUMNS%20%3D%20%5B*AREA_CODE_WIDTH%2C%20*UNIT_COLUMNS%2C%20*DESIGN_COLUMNS%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%20Get%20the%20tables%3A%20from%20regional%20databases%20to%20one%20flat%20table%0A%0A%20%20%20%20**Where%20this%20is%20in%20processing%20a%20survey.**%20The%20first%20processing%20step%3A%20bringing%20the%20data%20together.%20Every%20region's%20tables%20in%20one%20place%2C%20the%20removed%20records%20left%20out%2C%20the%20types%20set%2C%20and%20the%20result%20saved%20once%2C%20as%20one%20flat%20table.%20In%20R%20you%20would%20write%20this%20step%20as%20a%20loop%3A%20a%20%60dbConnect%60%20to%20each%20regional%20database%2C%20the%20removed%20and%20voided%20rows%20filtered%20(%60hh_deleted_on%60%2C%20%60row_void%60%2C%20%60sel_excluded%60)%2C%20the%20rows%20bound%20together%20and%20saved%20as%20an%20RDS%20file.%0A%0A%20%20%20%20Every%20estimate%20in%20this%20course%20starts%20from%20one%20table%3A%20each%20fishing%20unit%20of%20the%20survey%20on%20one%20row%2C%20with%20its%20household's%20area%20codes%2C%20its%20stratum%2C%20its%20area%20and%20its%20final%20weight.%20The%20survey%20does%20not%20arrive%20that%20way.%20It%20is%20kept%20as%20many%20surveys%20are%2C%20**one%20database%20per%20region**%2C%20ten%20tables%20in%20each%2C%20with%20the%20households%20removed%20in%20the%20capture%20system%2C%20in%20editing%20or%20by%20the%20sampling%20side%20still%20in%20them.%0A%0A%20%20%20%20This%20module%20builds%20that%20table%20in%20Python%2C%20with%20DuckDB%2C%20in%20one%20query.%20On%20the%20way%20it%20shows%20four%20things%20a%20loop%20gets%20wrong%20without%20an%20error%3A%20household%20ids%20that%20repeat%20across%20regions%2C%20area%20codes%20stored%20as%20numbers%20in%20one%20region%20and%20as%20text%20in%20another%2C%20a%20filter%20on%20a%20blank%20mark%20that%20drops%20every%20row%2C%20and%20a%20join%20that%20multiplies%20rows.%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%20One%20regional%20database%20%7C%20open%20a%20region's%20database%2C%20query%20it%2C%20get%20pandas%20and%20polars%20tables%2C%20and%20keep%20only%20the%20households%20still%20in%20the%20survey%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Every%20region%20in%20one%20query%20%7C%20attach%20every%20region%20to%20one%20connection%2C%20stack%20their%20tables%20with%20%60UNION%20ALL%20BY%20NAME%60%2C%20and%20filter%20removed%20rows%20region%20by%20region%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Codes%20kept%20whole%20%7C%20pad%20area%20codes%20as%20text%2C%20read%20the%20database's%20CSV%20export%20without%20losing%20a%20zero%20or%20a%20letter%2C%20and%20keep%20whole%20numbers%20whole%20when%20values%20are%20missing%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20The%20flat%20table%20%7C%20one%20row%20per%20fishing%20unit%20with%20its%20household's%20area%20and%20weight%2C%20saved%20as%20Parquet%2C%20its%20rows%20counted%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20The%20real%20thing%20%7C%20the%20same%20queries%20against%20a%20MySQL%20server%2C%20through%20DuckDB's%20mysql%20extension%20%7C%0A%0A%20%20%20%20**What%20%60course.table()%60%20does.**%20From%20module%203%20on%2C%20notebooks%20start%20from%20%60course.units()%60%2C%20%60course.households()%60%20and%20%60course.table()%60.%20Those%20helpers%20do%20what%20this%20module%20builds%20by%20hand.%20After%20this%20module%20you%20know%20what%20they%20hand%20you%2C%20and%20you%20can%20build%20the%20same%20from%20your%20own%20regional%20databases.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20module%201%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%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(%222.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%20One%20regional%20database%20into%20pandas%20and%20polars%0A%0A%20%20%20%20**The%20problem.**%20An%20R%20script%20opens%20one%20region%20at%20a%20time%20and%20pulls%20a%20table%20into%20R%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20con%20%3C-%20dbConnect(RMySQL%3A%3AMySQL()%2C%20dbname%20%3D%20%22sfh2025_r01%22%2C%20host%20%3D%20%22...%22%2C%20user%20%3D%20%22...%22%2C%20password%20%3D%20%22...%22)%0A%20%20%20%20households%20%3C-%20tbl(con%2C%20%22household%22)%20%7C%3E%20filter(is.na(hh_deleted_on))%20%7C%3E%20collect()%0A%20%20%20%20dbDisconnect(con)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20In%20this%20course%20each%20region's%20database%20is%20a%20**DuckDB%20file**%3A%20%60data%2Fsfh2025_r01.duckdb%60%2C%20%60sfh2025_r04.duckdb%60%20and%20%60sfh2025_r07.duckdb%60.%20A%20DuckDB%20database%20is%20one%20file%20holding%20many%20tables.%20The%20duckdb%20package%20reads%20it%20from%20inside%20Python%3A%20no%20server%2C%20no%20account%2C%20nothing%20else%20to%20install.%20It%20speaks%20**SQL**%20(Structured%20Query%20Language)%2C%20the%20language%20every%20database%20answers%20in%2C%20so%20what%20you%20write%20here%20works%20on%20a%20MySQL%20server%20too%20(section%205).%0A%0A%20%20%20%20**The%20tool.**%20A%20**connection**%20is%20your%20open%20line%20to%20a%20database%3B%20a%20**query**%20is%20one%20SQL%20statement%20sent%20down%20it.%20The%20steps%20map%20one%20to%20one%3A%0A%0A%20%20%20%20%7C%20step%20%7C%20R%20(DBI%20and%20dplyr)%20%7C%20Python%20(duckdb)%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20open%20%7C%20%60dbConnect(duckdb%3A%3Aduckdb()%2C%20%22data%2Fsfh2025_r01.duckdb%22%2C%20read_only%20%3D%20TRUE)%60%20%7C%20%60duckdb.connect(%22data%2Fsfh2025_r01.duckdb%22%2C%20read_only%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20list%20the%20tables%20%7C%20%60dbListTables(con)%60%20%7C%20%60con.sql(%22SHOW%20TABLES%22).fetchall()%60%20%7C%0A%20%20%20%20%7C%20a%20query%2C%20as%20a%20table%20%7C%20%60dbGetQuery(con%2C%20sql)%60%20%7C%20%60con.sql(sql).df()%60%20for%20pandas%2C%20%60.pl()%60%20for%20polars%20%7C%0A%20%20%20%20%7C%20rows%20as%20plain%20Python%20values%20%7C%20%60dbGetQuery(con%2C%20sql)%60%20%7C%20%60con.sql(sql).fetchall()%60%3A%20a%20list%20of%20tuples%2C%20one%20per%20row%20%7C%0A%20%20%20%20%7C%20close%20%7C%20%60dbDisconnect(con)%60%20%7C%20%60con.close()%60%20%7C%0A%0A%20%20%20%20%60read_only%3DTrue%60%20opens%20the%20file%20without%20the%20right%20to%20change%20it.%20It%20also%20lets%20several%20programs%20read%20the%20same%20file%20at%20once%3A%20DuckDB%20locks%20a%20file%20that%20is%20open%20for%20writing.%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%20region's%20database%2C%20opened%20read-only%2C%20and%20the%20tables%20in%20it%0A%20%20%20%20region01%20%3D%20duckdb.connect(str(PATHS.region(%2201%22))%2C%20read_only%3DTrue)%0A%20%20%20%20region01.sql(%22SHOW%20TABLES%22).df()%0A%20%20%20%20return%20(region01%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%20The%20table%20names%20say%20what%20each%20holds%3A%20%60household%60%20one%20row%20per%20household%2C%20%60selection%60%20its%20stratum%2C%20area%20and%20weights%2C%20%60member%60%20its%20members%2C%20%60fishing_unit%60%20its%20fishing%20units%2C%20and%20%60crew%60%2C%20%60boat%60%2C%20%60gear%60%2C%20%60catch%60%2C%20%60landing%60%20and%20%60assistance%60%20the%20units'%20lists.%20Module%201%20walks%20through%20the%20codebook.%0A%0A%20%20%20%20%23%23%23%20SQL%20for%20a%20dplyr%20user%0A%0A%20%20%20%20A%20query%20is%20a%20sentence%20of%20clauses%2C%20each%20the%20SQL%20word%20for%20a%20dplyr%20verb%3A%0A%0A%20%20%20%20%7C%20dplyr%20%7C%20SQL%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60select(a%2C%20b)%60%20%7C%20%60SELECT%20a%2C%20b%60%20%7C%20the%20columns%20you%20want%3B%20%60*%60%20is%20all%20of%20them%20%7C%0A%20%20%20%20%7C%20(the%20table)%20%7C%20%60FROM%20household%60%20%7C%20the%20table%20they%20come%20from%20%7C%0A%20%20%20%20%7C%20%60filter(...)%60%20%7C%20%60WHERE%20...%60%20%7C%20the%20rows%20you%20keep%20%7C%0A%20%20%20%20%7C%20%60arrange(...)%60%20%7C%20%60ORDER%20BY%20...%60%20%7C%20their%20order%20%7C%0A%20%20%20%20%7C%20%60count()%60%2C%20%60n()%60%20%7C%20%60count(*)%60%20%7C%20how%20many%20rows%20%7C%0A%20%20%20%20%7C%20%60group_by(a)%20%5C%7C%3E%20summarise(...)%60%20%7C%20%60...%20GROUP%20BY%20a%60%20%7C%20one%20row%20per%20value%20of%20%60a%60%20%7C%0A%0A%20%20%20%20**Quotes.**%20Single%20quotes%20hold%20a%20text%20value%3A%20%60WHERE%20hh_interviewer%20%3D%20'INT004'%60.%20Double%20quotes%20hold%20a%20**name**%20(a%20table%20or%20a%20column).%20The%20survey's%20names%20need%20none%2C%20but%20a%20name%20with%20a%20hyphen%2C%20a%20space%20or%20capitals%20does.%20Say%20someone%20saved%20a%20copy%20of%20the%20households%20under%20the%20name%20%60household-backup%60%3A%20written%20without%20its%20quotes%2C%20SQL%20reads%20the%20hyphen%20as%20a%20minus%20sign.%20The%20error%20says%20so%20on%20its%20last%20lines%2C%20with%20a%20caret%20under%20the%20place%20it%20stopped%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%20A%20copy%20of%20the%20households%20under%20a%20name%20with%20a%20hyphen%2C%20in%20a%20database%20held%20in%20memory%2C%20counted%20without%20and%20with%20its%20quotes%0A%20%20%20%20_con%20%3D%20duckdb.connect()%0A%20%20%20%20_con.execute(f%22ATTACH%20'%7BPATHS.region('01').as_posix()%7D'%20AS%20sfh2025_r01%20(READ_ONLY)%22)%0A%20%20%20%20_con.execute('CREATE%20TABLE%20%22household-backup%22%20AS%20SELECT%20*%20FROM%20sfh2025_r01.household')%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_con.sql(%22SELECT%20count(*)%20FROM%20household-backup%22).fetchall()%0A%20%20%20%20%20%20%20%20_message%20%3D%20%22no%20error%22%0A%20%20%20%20except%20duckdb.Error%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_message%20%3D%20f%22%7Btype(_error).__name__%7D%3A%20%7B_error%7D%22%0A%20%20%20%20_households%20%3D%20_con.sql('SELECT%20count(*)%20FROM%20%22household-backup%22').fetchall()%5B0%5D%5B0%5D%0A%20%20%20%20_con.close()%0A%20%20%20%20mo.callout(mo.md(f%22%60%60%60%5Cn%7B_message%7D%5Cn%60%60%60%5Cn%5CnWith%20its%20quotes%2C%20%60SELECT%20count(*)%20FROM%20%5C%22household-backup%5C%22%60%20counts%20**%7B_households%3A%2C%7D**%20households.%22)%2C%20kind%3D%22warn%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%20queries%20this%20module%20builds%20from%20text%20put%20every%20table's%20name%20in%20double%20quotes%3A%20a%20habit%20that%20costs%20nothing%20on%20the%20survey's%20names%20and%20saves%20the%20day%20a%20database%20has%20a%20name%20like%20that%20one.%0A%0A%20%20%20%20%23%23%23%20pandas%20or%20polars%3A%20%60.df()%60%20and%20%60.pl()%60%0A%0A%20%20%20%20%60con.sql(query)%60%20does%20not%20run%20the%20query%20yet.%20It%20returns%20a%20**relation**%3A%20the%20query%2C%20waiting%2C%20much%20as%20%60tbl(con%2C%20%22household%22)%60%20in%20dplyr%20waits%20for%20%60collect()%60.%20Asking%20for%20the%20result%20runs%20it%3A%20%60.df()%60%20gives%20a%20pandas%20DataFrame%2C%20%60.pl()%60%20a%20polars%20one%2C%20%60.fetchall()%60%20a%20list%20of%20rows.%20The%20two%20tables%20hold%20the%20same%20values%3B%20their%20column%20types%20differ%20in%20one%20place%20that%20matters%20to%20a%20survey.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(region01)%3A%0A%20%20%20%20%23%20The%20same%20query%20into%20pandas%20and%20into%20polars%2C%20and%20the%20type%20each%20gives%20every%20column%0A%20%20%20%20_query%20%3D%20%22SELECT%20*%20FROM%20household%22%0A%20%20%20%20_as_pandas%20%3D%20region01.sql(_query).df()%0A%20%20%20%20_as_polars%20%3D%20region01.sql(_query).pl()%0A%20%20%20%20_voided_only%20%3D%20region01.sql(%22SELECT%20row_void%20FROM%20household%20WHERE%20row_void%20%3D%201%22).df()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22pandas%2C%20.df()%22%3A%20_as_pandas.dtypes.astype(str)%2C%20%22polars%2C%20.pl()%22%3A%20%5Bstr(t)%20for%20t%20in%20_as_polars.dtypes%5D%7D).T%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%60row_void%60%20is%20blank%20on%20%7B_as_pandas%5B'row_void'%5D.isna().sum()%3A%2C%7D%20of%20%7Blen(_as_pandas)%3A%2C%7D%20rows%2C%20and%20pandas%20types%20it%20%60%7B_as_pandas%5B'row_void'%5D.dtype%7D%60.%20Query%20only%20the%20rows%20where%20it%20is%201%2C%20and%20the%20same%20column%20comes%20back%20%60%7B_voided_only%5B'row_void'%5D.dtype%7D%60.%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%20Read%20the%20%60row_void%60%20column.%20NumPy's%20integer%20types%2C%20%60int32%60%20and%20%60int64%60%2C%20have%20**no%20missing%20value**%3A%20every%20slot%20holds%20a%20number.%20pandas%20therefore%20has%20a%20second%20family%2C%20the%20**nullable%20integers**%20%60Int32%60%20and%20%60Int64%60%20(capital%20I)%2C%20which%20can%20hold%20a%20missing%20value%2C%20shown%20%60%3CNA%3E%60.%20DuckDB's%20%60.df()%60%20picks%20%60Int32%60%20for%20a%20column%20only%20when%20the%20rows%20it%20returns%20have%20a%20missing%20value%20in%20it%2C%20and%20%60int32%60%20otherwise.%20So%20**the%20type%20depends%20on%20the%20rows**%3A%20the%20same%20column%2C%20from%20the%20same%20table%2C%20comes%20back%20with%20two%20types%20from%20two%20queries.%20polars%20has%20no%20such%20split%3A%20every%20polars%20column%20can%20hold%20a%20missing%20value%20(%60null%60)%2C%20so%20%60row_void%60%20is%20%60i32%60%20whatever%20the%20rows.%20Section%203%20comes%20back%20to%20it%2C%20because%20a%20third%20reader%20turns%20such%20a%20column%20into%20decimals.%0A%0A%20%20%20%20%23%23%23%20Values%20as%20parameters%2C%20and%20fetching%20the%20whole%20result%0A%0A%20%20%20%20A%20query%20that%20depends%20on%20a%20value%2C%20such%20as%20one%20province%2C%20takes%20it%20as%20a%20**parameter**%3A%20a%20%60%3F%60%20in%20the%20SQL%20and%20the%20value%20in%20%60params%3D%60.%20DuckDB%20passes%20the%20value%20apart%20from%20the%20SQL%20text%2C%20so%20a%20value%20can%20never%20be%20misread%20as%20SQL.%20Names%20(tables%2C%20databases)%20cannot%20be%20parameters%3B%20they%20are%20written%20into%20the%20text%2C%20from%20a%20list%20you%20control.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(region01)%3A%0A%20%20%20%20%23%20Households%20per%20city%20or%20municipality%20in%20one%20province%20of%20region%2001%20(La%20Union%2C%20province%20033)%3A%20the%20province%20code%20is%20a%20parameter%0A%20%20%20%20region01.sql(%0A%20%20%20%20%20%20%20%20%22SELECT%20citymun_code%2C%20count(*)%20AS%20households%20FROM%20household%20WHERE%20province_code%20%3D%20%3F%20GROUP%20BY%20citymun_code%20ORDER%20BY%20citymun_code%22%2C%0A%20%20%20%20%20%20%20%20params%3D%5B33%5D%2C%0A%20%20%20%20).df()%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**Fetch%20the%20whole%20result.**%20%60con.execute(query).fetchone()%60%20takes%20the%20first%20row%20and%20leaves%20the%20rest%20waiting.%20DuckDB%202%20then%20refuses%20a%20%60con.sql(...)%60%20query%20on%20that%20connection%20until%20the%20result%20is%20finished%20(a%20new%20%60con.execute(...)%60%20still%20runs).%20The%20message%20names%20the%20cause%20and%20the%20ways%20out%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%20fetchone()%20leaves%20the%20rest%20of%20the%20result%20waiting%3B%20the%20next%20query%20on%20that%20connection%20is%20refused%0A%20%20%20%20_con%20%3D%20duckdb.connect(str(PATHS.region(%2201%22))%2C%20read_only%3DTrue)%0A%20%20%20%20_first%20%3D%20_con.execute(%22SELECT%20hh_id%20FROM%20household%22).fetchone()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_con.sql(%22SELECT%201%22).fetchall()%0A%20%20%20%20%20%20%20%20_message%20%3D%20%22no%20error%22%0A%20%20%20%20except%20duckdb.Error%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_message%20%3D%20f%22%7Btype(_error).__name__%7D%3A%20%7B_error%7D%22%0A%20%20%20%20_con.close()%0A%20%20%20%20mo.callout(mo.md(f%22%60fetchone()%60%20gave%20%60%7B_first%7D%60.%20The%20next%20query%20on%20the%20same%20connection%3A%5Cn%5Cn%60%60%60%5Cn%7B_message%7D%5Cn%60%60%60%5Cn%5CnUse%20%60.fetchall()%60%2C%20%60.df()%60%20or%20%60.pl()%60%2C%20which%20take%20every%20row%2C%20and%20the%20question%20never%20comes%20up.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.1.%20What%20a%20regional%20database%20holds%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20before%20reading%20a%20region%2C%20count%20what%20is%20in%20it.%20A%20table%20with%20no%20rows%2C%20or%20with%20far%20fewer%20than%20the%20region%20next%20door%2C%20is%20a%20failed%20export%2C%20and%20a%20count%20taken%20now%20is%20what%20section%204%20checks%20the%20flat%20table%20against.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20connection%20to%20one%20regional%20database%20%E2%86%92%20a%20pandas%20table%20with%20one%20row%20per%20table%20in%20the%20database%2C%20sorted%20by%20table%20name%2C%20and%20two%20columns%3A%0A%0A%20%20%20%20%7C%20table%20%7C%20rows%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20assistance%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20boat%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20selection%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20%60rows%60%20is%20the%20number%20of%20rows%20each%20table%20holds%2C%20counted%20with%20%60count(*)%60.%20A%20table%20with%20no%20rows%20is%20listed%20with%200.%0A%0A%20%20%20%20**Tools%3A**%20%60con.sql(%22SHOW%20TABLES%22).fetchall()%60%2C%20a%20loop%20or%20list%20comprehension%20over%20the%20names%2C%20%60SELECT%20count(*)%20FROM%20%22...%22%60%20with%20the%20name%20in%20double%20quotes%2C%20and%20%60pd.DataFrame%60%20(above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20table_counts(con%3A%20duckdb.DuckDBPyConnection)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20table%20of%20the%20connection's%20database%3A%20table%20and%20rows%2C%20sorted%20by%20table%20name.%22%22%22%0A%20%20%20%20%23%20Exercise%202.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20table%20names%3A%20SHOW%20TABLES%2C%20fetchall()%2C%20the%20first%20value%20of%20each%20row%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20name%2C%20SELECT%20count(*)%20FROM%20%22%3Cname%3E%22%20(double%20quotes%20around%20the%20name)%2C%20fetchall()%5B0%5D%5B0%5D%0A%20%20%20%20%23%20Step%203%3A%20a%20pandas%20DataFrame%20with%20columns%20table%20and%20rows%2C%20sorted%20by%20table%2C%20index%200%2C%201%2C%202%2C%20...%3B%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.1%22)%0A%0A%0A%40app.function%0Adef%20small_region()%20-%3E%20duckdb.DuckDBPyConnection%3A%0A%20%20%20%20%22%22%22A%20regional%20database%20of%20five%20households%2C%20in%20memory%2C%20used%20by%20the%20check%20of%20Exercise%202.1%3A%20one%20household%20deleted%20in%20the%0A%20%20%20%20capture%20system%2C%20one%20voided%20in%20editing%2C%20households%20not%20in%20id%20order%2C%20a%20copy%20of%20three%20households%20saved%20under%20a%20name%0A%20%20%20%20with%20a%20hyphen%2C%20and%20a%20catch%20table%20with%20no%20rows.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.execute(%22%22%22CREATE%20TABLE%20household%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20hh_code%20VARCHAR%2C%20region_code%20INTEGER%2C%20province_code%20INTEGER%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20citymun_code%20INTEGER%2C%20barangay_code%20INTEGER%2C%20hh_interviewer%20VARCHAR%2C%20hh_deleted_on%20DATE%2C%20row_void%20INTEGER)%22%22%22)%0A%20%20%20%20con.execute(%22%22%22INSERT%20INTO%20household%20VALUES%0A%20%20%20%20%20%20%20%20(1%2C%20201%2C%20'0301401001001'%2C%203%2C%2014%2C%201%2C%201%2C%20'INT101'%2C%20NULL%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(2%2C%20202%2C%20'0301401001002'%2C%203%2C%2014%2C%201%2C%201%2C%20'INT101'%2C%20NULL%2C%201)%2C%0A%20%20%20%20%20%20%20%20(3%2C%20204%2C%20'0301401002001'%2C%203%2C%2014%2C%201%2C%202%2C%20'INT102'%2C%20DATE%20'2026-04-06'%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(4%2C%20205%2C%20'0301402003002'%2C%203%2C%2014%2C%202%2C%203%2C%20'INT102'%2C%20NULL%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(5%2C%20203%2C%20'0301402003001'%2C%203%2C%2014%2C%202%2C%203%2C%20'INT101'%2C%20NULL%2C%20NULL)%22%22%22)%0A%20%20%20%20con.execute(%22CREATE%20TABLE%20selection%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20sel_stratum%20VARCHAR%2C%20sel_final_weight%20DOUBLE%2C%20sel_excluded%20INTEGER)%22)%0A%20%20%20%20con.execute(%22INSERT%20INTO%20selection%20VALUES%20(1%2C%20201%2C%20'C'%2C%2015.2%2C%20NULL)%2C%20(2%2C%20202%2C%20'C'%2C%2015.2%2C%20NULL)%2C%20(3%2C%20203%2C%20'F'%2C%201.0%2C%20NULL)%2C%20(4%2C%20204%2C%20'B'%2C%205.1%2C%20NULL)%2C%20(5%2C%20205%2C%20'C'%2C%2015.2%2C%20NULL)%22)%0A%20%20%20%20con.execute(%22CREATE%20TABLE%20catch%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20catch_species%20INTEGER)%22)%0A%20%20%20%20con.execute('CREATE%20TABLE%20%22household-backup%22%20AS%20SELECT%20*%20FROM%20household%20WHERE%20hh_id%20%3C%3D%20203')%0A%20%20%20%20return%20con%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%202.1%22%2C%20table_counts%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20database%22%2C%20(duckdb.connect(str(PATHS.region(%2201%22))%2C%20read_only%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20database%22%2C%20(duckdb.connect(str(PATHS.region(%2207%22))%2C%20read_only%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20database%20of%20four%20tables%3A%20one%20empty%2C%20one%20with%20a%20hyphen%20in%20its%20name%22%2C%20(small_region()%2C))%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%20summarize%3Dlambda%20table%3A%20table.reset_index(drop%3DTrue)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22List%20the%20tables%20with%20SHOW%20TABLES%2C%20count%20each%20with%20count(*)%20and%20its%20name%20in%20double%20quotes%2C%20and%20sort%20by%20table.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(region01)%3A%0A%20%20%20%20%23%20Try%20it%3A%20what%20region%2001's%20database%20holds%0A%20%20%20%20try_it(table_counts%2C%20region01)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*ParserException%3A%20syntax%20error%20at%20or%20near%20%22-%22*%20%7C%20A%20name%20with%20a%20hyphen%2C%20written%20without%20double%20quotes.%20%7C%20%60f'SELECT%20count(*)%20FROM%20%22%7Bname%7D%22'%60%20%7C%0A%20%20%20%20%7C%20*ResourceInUseException%3A%20connection%20has%20an%20unfinished%20result*%20%7C%20%60fetchone()%60%20left%20the%20rest%20of%20a%20result%20waiting.%20%7C%20%60.fetchall()%5B0%5D%5B0%5D%60%20%7C%0A%20%20%20%20%7C%20Counts%20that%20are%20estimates%2C%20not%20counts%20%7C%20%60estimated_size%60%20from%20%60duckdb_tables()%60.%20%7C%20%60count(*)%60%20%7C%0A%20%20%20%20%7C%20The%20right%20counts%20in%20another%20order%20%7C%20Not%20sorted%20by%20table%20name.%20%7C%20%60.sort_values(%22table%22%2C%20ignore_index%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20Columns%20named%20%600%60%20and%20%601%60%2C%20or%20%60name%60%20%7C%20The%20columns%20were%20not%20named%20%60table%60%20and%20%60rows%60.%20%7C%20%60pd.DataFrame(%7B%22table%22%3A%20...%2C%20%22rows%22%3A%20...%7D)%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%20Households%20still%20in%20the%20survey%3A%20a%20filter%20on%20a%20blank%20mark%0A%0A%20%20%20%20**The%20problem.**%20Not%20every%20household%20in%20%60household%60%20belongs%20in%20an%20estimate.%20The%20codebook%20names%20two%20marks%20on%20it%3A%20%60hh_deleted_on%60%20is%20the%20day%20the%20household%20was%20deleted%20in%20the%20capture%20system%2C%20blank%20if%20it%20never%20was%3B%20%60row_void%60%20is%201%20for%20a%20household%20voided%20in%20editing%20and%20**blank**%20otherwise.%20In%20SQL%2C%20as%20in%20R%2C%20a%20blank%20value%20is%20**%60NULL%60**%20(R's%20%60NA%60)%3A%20not%20zero%2C%20not%20empty%2C%20but%20unknown.%20The%20obvious%20filter%20is%20%60WHERE%20hh_deleted_on%20IS%20NULL%20AND%20row_void%20%3C%3E%201%60%20(%60%3C%3E%60%20is%20SQL's%20%22not%20equal%22).%20Count%20what%20it%20keeps%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(region01)%3A%0A%20%20%20%20%23%20The%20obvious%20filter%2C%20and%20what%20it%20keeps%0A%20%20%20%20def%20_kept(where%3A%20str)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20return%20region01.sql(f%22SELECT%20count(*)%20FROM%20household%20WHERE%20%7Bwhere%7D%22).fetchall()%5B0%5D%5B0%5D%0A%0A%20%20%20%20_all%20%3D%20region01.sql(%22SELECT%20count(*)%20FROM%20household%22).fetchall()%5B0%5D%5B0%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20filter%20%7C%20households%20kept%2C%20of%20%7B_all%3A%2C%7D%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20%60hh_deleted_on%20IS%20NULL%20AND%20row_void%20%3C%3E%201%60%20%7C%20**%7B_kept(%22hh_deleted_on%20IS%20NULL%20AND%20row_void%20%3C%3E%201%22)%3A%2C%7D**%20%7C%0A%20%20%20%20%7C%20%60hh_deleted_on%20%3D%20NULL%20AND%20row_void%20IS%20DISTINCT%20FROM%201%60%20%7C%20**%7B_kept(%22hh_deleted_on%20%3D%20NULL%20AND%20row_void%20IS%20DISTINCT%20FROM%201%22)%3A%2C%7D**%20%7C%0A%20%20%20%20%7C%20%60hh_deleted_on%20IS%20NULL%20AND%20row_void%20IS%20DISTINCT%20FROM%201%60%20%7C%20**%7B_kept(%22hh_deleted_on%20IS%20NULL%20AND%20row_void%20IS%20DISTINCT%20FROM%201%22)%3A%2C%7D**%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**Why%20the%20first%20two%20keep%20nothing.**%20A%20comparison%20with%20an%20unknown%20value%20is%20unknown%3A%20%60NULL%20%3C%3E%201%60%20is%20not%20true%20but%20%60NULL%60%2C%20and%20%60WHERE%60%20keeps%20only%20the%20rows%20where%20its%20condition%20is%20**true**.%20Every%20household%20with%20a%20blank%20%60row_void%60%2C%20which%20is%20nearly%20every%20household%2C%20fails%20the%20first%20filter%2C%20and%20so%20the%20query%20keeps%20only%20households%20marked%20with%20some%20other%20value%2C%20of%20which%20there%20are%20none.%20The%20second%20filter%20fails%20on%20the%20date%20the%20same%20way%3A%20%60hh_deleted_on%20%3D%20NULL%60%20is%20%60NULL%60%20for%20every%20row%2C%20blank%20or%20not%2C%20which%20is%20why%20a%20blank%20is%20tested%20with%20%60IS%20NULL%60.%20No%20error%2C%20no%20warning%3A%20an%20empty%20table%2C%20or%20in%20a%20join%20further%20on%2C%20a%20short%20one.%0A%0A%20%20%20%20R's%20%60filter(row_void%20!%3D%201)%60%20does%20the%20same%3A%20it%20drops%20the%20rows%20where%20the%20condition%20is%20%60NA%60.%20The%20readers%20you%20will%20use%20do%20not%20all%20agree%2C%20which%20is%20a%20reason%20to%20write%20the%20condition%20so%20it%20cannot%20be%20misread%3A%0A%0A%20%20%20%20%7C%20where%20%7C%20%60row_void%20!%3D%201%60%20on%20the%20values%201%2C%20blank%2C%200%20%7C%20keeps%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20SQL%2C%20DuckDB%20or%20MySQL%20%7C%20%60WHERE%20row_void%20%3C%3E%201%60%20%7C%200%20only%20%7C%0A%20%20%20%20%7C%20R%2C%20dplyr%20%7C%20%60filter(row_void%20!%3D%201)%60%20%7C%200%20only%20%7C%0A%20%20%20%20%7C%20polars%20%7C%20%60.filter(pl.col(%22row_void%22)%20!%3D%201)%60%20%7C%200%20only%20%7C%0A%20%20%20%20%7C%20pandas%2C%20a%20nullable%20%60Int32%60%20column%20%7C%20%60table%5Btable%5B%22row_void%22%5D%20!%3D%201%5D%60%20%7C%200%20only%20%7C%0A%20%20%20%20%7C%20pandas%2C%20a%20%60float64%60%20column%20(%60NaN%60%20for%20blank)%20%7C%20%60table%5Btable%5B%22row_void%22%5D%20!%3D%201%5D%60%20%7C%20blank%20and%200%20%7C%0A%0A%20%20%20%20**The%20tool.**%20Say%20what%20a%20blank%20means.%20%60row_void%20IS%20DISTINCT%20FROM%201%60%20treats%20%60NULL%60%20as%20a%20value%20like%20any%20other%3A%20it%20is%20true%20for%20blank%20and%20for%200%2C%20false%20only%20for%201.%20%60row_void%20IS%20NULL%20OR%20row_void%20%3C%3E%201%60%20and%20%60coalesce(row_void%2C%200)%20%3C%3E%201%60%20(%60coalesce%60%20takes%20the%20first%20value%20that%20is%20not%20%60NULL%60)%20say%20the%20same.%20In%20R%2C%20%60filter(is.na(row_void)%20%7C%20row_void%20!%3D%201)%60%3B%20in%20polars%2C%20%60pl.col(%22row_void%22).ne_missing(1)%60.%0A%0A%20%20%20%20Section%202%20writes%20this%20condition%20into%20every%20region's%20query%2C%20in%20Exercise%202.3.%20The%20stretch%20S1%2C%20%5Bprocessing%5D(%2F%3Ffile%3D36_stretch_processing.py)%2C%20has%20it%20as%20an%20exercise%20of%20its%20own%2C%20on%20one%20region%2C%20with%20the%20households%20returned%20as%20a%20polars%20table.%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%202%20%C2%B7%20Every%20region%20in%20one%20query%3A%20ATTACH%2C%20UNION%20ALL%20BY%20NAME%2C%20deleted%20rows%20filtered%0A%0A%20%20%20%20**The%20problem.**%20In%20R%2C%20section%201%20is%20repeated%20once%20per%20region%20and%20the%20results%20stacked%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20out%20%3C-%20NULL%0A%20%20%20%20for%20(reg%20in%20c(%2201%22%2C%20%2204%22%2C%20%2207%22))%20%7B%0A%20%20%20%20%20%20con%20%3C-%20dbConnect(duckdb%3A%3Aduckdb()%2C%20paste0(%22data%2Fsfh2025_r%22%2C%20reg%2C%20%22.duckdb%22)%2C%20read_only%20%3D%20TRUE)%0A%20%20%20%20%20%20units%20%3C-%20tbl(con%2C%20%22fishing_unit%22)%20%7C%3E%20collect()%0A%20%20%20%20%20%20out%20%3C-%20bind_rows(out%2C%20units)%0A%20%20%20%20%20%20dbDisconnect(con)%0A%20%20%20%20%7D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20It%20works%2C%20and%20it%20hides%20three%20questions.%20Which%20region%20is%20each%20row%20from%3F%20The%20%60fishing_unit%60%20table%20has%20no%20region%20column%2C%20and%20the%20loop%20drops%20%60reg%60%20on%20the%20floor.%20Is%20a%20household%20id%20enough%20to%20find%20a%20household%3F%20Only%20within%20one%20region%2C%20as%20the%20cell%20below%20counts.%20And%20what%20if%20the%20regions%20do%20not%20store%20a%20column%20the%20same%20way%3F%20Section%203%20answers%20that%20one.%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.**%20%60ATTACH%60%20opens%20a%20database%20**inside**%20a%20connection%20you%20already%20have%2C%20under%20a%20name%20you%20give%20it.%20Attach%20each%20region's%20file%20to%20one%20connection%2C%20and%20a%20table%20is%20then%20named%20by%20its%20database%20and%20its%20own%20name%3A%20%60sfh2025_r01.household%60%2C%20%60sfh2025_r04.household%60.%20One%20connection%2C%20three%20regions%2C%20one%20query%20across%20them.%20In%20R%2C%20the%20same%20is%20%60dbExecute(con%2C%20%22ATTACH%20'data%2Fsfh2025_r01.duckdb'%20AS%20sfh2025_r01%20(READ_ONLY)%22)%60.%0A%0A%20%20%20%20The%20cell%20below%20attaches%20the%20three%20files%20to%20a%20connection%20held%20in%20memory%20(%60duckdb.connect()%60%20with%20no%20file)%2C%20each%20%60READ_ONLY%60%2C%20under%20the%20names%20in%20%60DATABASES%60%3A%20each%20file's%20name%20without%20its%20%60.duckdb%60.%20The%20function%20that%20does%20it%20is%20used%20by%20the%20checks%20for%20the%20rest%20of%20this%20notebook.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20attach_regions(regions%3A%20list%5Bstr%5D%20%3D%20REGIONS)%20-%3E%20duckdb.DuckDBPyConnection%3A%0A%20%20%20%20%22%22%22A%20connection%20with%20each%20region's%20database%20attached%2C%20read-only%2C%20under%20its%20file's%20name%3A%20sfh2025_r01%2C%20sfh2025_r04%2C%20sfh2025_r07.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%20%20%23%20a%20database%20in%20memory%3A%20nothing%20is%20written%20anywhere%0A%20%20%20%20for%20reg%20in%20regions%3A%0A%20%20%20%20%20%20%20%20con.execute(f%22ATTACH%20'%7BPATHS.region(reg).as_posix()%7D'%20AS%20%7Bcourse.SURVEY%7D_r%7Breg%7D%20(READ_ONLY)%22)%0A%20%20%20%20return%20con%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20One%20connection%2C%20every%20region%20attached%3B%20duckdb_databases()%20lists%20what%20the%20connection%20can%20see%0A%20%20%20%20survey_con%20%3D%20attach_regions()%0A%20%20%20%20survey_con.sql(f%22SELECT%20database_name%2C%20path%2C%20readonly%20FROM%20duckdb_databases()%20WHERE%20database_name%20LIKE%20'%7Bcourse.SURVEY%7D_r%25'%20ORDER%20BY%20database_name%22).df()%0A%20%20%20%20return%20(survey_con%2C)%0A%0A%0A%40app.cell%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20The%20same%20household%20id%20in%20several%20regions%3A%20every%20region%20numbers%20its%20households%20from%20its%20own%20start%0A%20%20%20%20_ids%20%3D%20survey_con.sql(%0A%20%20%20%20%20%20%20%20%22SELECT%20hh_id%2C%20count(*)%20AS%20regions%20FROM%20(%22%20%2B%20%22%20UNION%20ALL%20%22.join(f%22SELECT%20hh_id%20FROM%20%7B_db%7D.household%22%20for%20_db%20in%20DATABASES)%20%2B%20%22)%20GROUP%20BY%20hh_id%22%0A%20%20%20%20).df()%0A%20%20%20%20_shared%20%3D%20_ids%5B_ids%5B%22regions%22%5D%20%3E%201%5D%0A%20%20%20%20_example%20%3D%20int(_shared.sort_values(%5B%22regions%22%2C%20%22hh_id%22%5D%2C%20ascending%3D%5BFalse%2C%20True%5D)%5B%22hh_id%22%5D.iloc%5B0%5D)%0A%20%20%20%20mo.md(f%22Of%20the%20**%7Blen(_ids)%3A%2C%7D**%20household%20ids%20in%20the%20three%20databases%2C%20**%7Blen(_shared)%3A%2C%7D**%20are%20used%20in%20more%20than%20one%20region%3B%20household%20**%7B_example%7D**%2C%20for%20one%2C%20is%20a%20household%20in%20each%20of%20%7Bint(_shared.loc%5B_shared%5B'hh_id'%5D%20%3D%3D%20_example%2C%20'regions'%5D.iloc%5B0%5D)%7D%20regions.%20A%20household%20is%20its%20region%20**and**%20its%20id%3A%20%60region_code%60%20plus%20%60hh_id%60%2C%20which%20is%20why%20%60course.UNIT_KEY%60%20starts%20with%20%60region_code%60.%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%20Stacking%3A%20%60UNION%20ALL%60%20and%20%60UNION%20ALL%20BY%20NAME%60%0A%0A%20%20%20%20%60query1%20UNION%20ALL%20query2%60%20puts%20the%20rows%20of%20the%20second%20query%20under%20those%20of%20the%20first%2C%20as%20%60bind_rows%60%20does.%20The%20two%20differ%20in%20how%20they%20line%20up%20columns.%20%60UNION%20ALL%60%20lines%20them%20up%20**by%20position**%3A%20the%20first%20column%20under%20the%20first%2C%20whatever%20their%20names.%20%60UNION%20ALL%20BY%20NAME%60%20lines%20them%20up%20**by%20name**%2C%20as%20%60bind_rows%60%20does%2C%20and%20fills%20a%20column%20one%20query%20lacks%20with%20%60NULL%60.%20A%20region%20whose%20table%20was%20rebuilt%20with%20two%20columns%20in%20another%20order%20shows%20the%20difference%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%20regions'%20units%2C%20the%20second%20with%20operator_line%20and%20unit_no%20in%20the%20other%20order%0A%20%20%20%20_query%20%3D%20%22%22%22%0A%20%20%20%20%20%20%20%20SELECT%20*%20FROM%20(VALUES%20(5012%2C%201%2C%202))%20AS%20a(hh_id%2C%20operator_line%2C%20unit_no)%0A%20%20%20%20%20%20%20%20%7Bhow%7D%0A%20%20%20%20%20%20%20%20SELECT%20*%20FROM%20(VALUES%20(5013%2C%201%2C%203))%20AS%20b(hh_id%2C%20unit_no%2C%20operator_line)%22%22%22%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22%60UNION%20ALL%60%3A%20by%20position%22)%2C%20duckdb.sql(_query.format(how%3D%22UNION%20ALL%22)).df()%5D)%2C%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22%60UNION%20ALL%20BY%20NAME%60%3A%20by%20name%22)%2C%20duckdb.sql(_query.format(how%3D%22UNION%20ALL%20BY%20NAME%22)).df()%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%20Household%205013's%20unit%20is%20run%20by%20the%20member%20on%20line%203%20of%20its%20roster%2C%20and%20it%20is%20that%20member's%20first%20unit.%20By%20position%2C%20the%20stacked%20table%20says%20line%201%2C%20unit%203%3A%20no%20error%2C%20and%20every%20key%20built%20on%20it%20is%20wrong.%20**Stack%20survey%20tables%20by%20name.**%20(%60UNION%60%20without%20%60ALL%60%20also%20removes%20rows%20that%20are%20the%20same%20in%20every%20column.%20A%20survey's%20duplicate%20rows%20are%20module%204's%20to%20find%2C%20not%20a%20union's%20to%20hide%2C%20so%20write%20%60ALL%60.)%0A%0A%20%20%20%20%23%23%23%20One%20query%20from%20a%20list%20of%20regions%0A%0A%20%20%20%20The%20query%20is%20text%2C%20and%20Python%20builds%20it%3A%20one%20%60SELECT%60%20per%20region%2C%20joined%20with%20%60UNION%20ALL%20BY%20NAME%60.%20Each%20%60SELECT%60%20adds%20the%20region%20as%20a%20column%2C%20written%20in%20from%20the%20database's%20name%2C%20which%20ends%20in%20the%20region's%20two%20digits%2C%20since%20the%20%60fishing_unit%60%20table%20has%20no%20region%20column%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20parts%20%3D%20%5Bf%22SELECT%20'%7Bdb%5B-2%3A%5D%7D'%20AS%20region_code%2C%20*%20FROM%20%7Bdb%7D.fishing_unit%22%20for%20db%20in%20DATABASES%5D%0A%20%20%20%20query%20%3D%20%22%20UNION%20ALL%20BY%20NAME%20%22.join(parts)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60'01'%20AS%20region_code%60%20is%20a%20**constant%20column**%3A%20the%20same%20text%20on%20every%20row%20of%20that%20region.%20%60db%5B-2%3A%5D%60%20is%20the%20name's%20last%20two%20characters.%20The%20database%20names%20come%20from%20your%20own%20list%2C%20so%20writing%20them%20into%20the%20text%20is%20safe%3B%20a%20value%20from%20outside%20(a%20code%20someone%20typed)%20goes%20in%20as%20a%20parameter%2C%20as%20in%20section%201.%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%20query%20the%20list%20builds%2C%20printed%0A%20%20%20%20_parts%20%3D%20%5Bf%22SELECT%20'%7B_db%5B-2%3A%5D%7D'%20AS%20region_code%2C%20*%20FROM%20%7B_db%7D.fishing_unit%22%20for%20_db%20in%20DATABASES%5D%0A%20%20%20%20mo.md(%22%60%60%60sql%5Cn%22%20%2B%20%22%5CnUNION%20ALL%20BY%20NAME%5Cn%22.join(_parts)%20%2B%20%22%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.2.%20Every%20region's%20rows%20of%20one%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20R%20loop%20in%20one%20query.%20Every%20table%20under%20the%20household%20goes%20through%20it%3A%20units%2C%20members%2C%20boats%2C%20catch.%20If%20the%20region%20is%20not%20on%20every%20row%2C%20the%20next%20join%20cannot%20tell%20region%2001's%20household%20from%20region%2004's%20with%20the%20same%20id.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20connection%20with%20regional%20databases%20attached%2C%20a%20table's%20name%2C%20and%20a%20list%20of%20the%20attached%20databases'%20names%20(such%20as%20%60%5B%22sfh2025_r01%22%2C%20%22sfh2025_r04%22%5D%60)%20%E2%86%92%20a%20pandas%20table%20of%20that%20table's%20rows%20from%20every%20listed%20database%2C%20stacked%20**by%20name**%2C%20with%20a%20first%20column%20%60region_code%60%3A%20the%20region's%20two%20digits%20as%20text%20(%60%2201%22%60)%2C%20the%20last%20two%20characters%20of%20the%20database's%20name.%20Every%20other%20column%20as%20the%20databases%20store%20it.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20row_id%20%7C%20hh_id%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%201%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%2004%20%7C%201%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20It%20is%20for%20every%20table%20but%20%60household%60%2C%20which%20has%20its%20own%20%60region_code%60%20(section%203%20deals%20with%20it).%20The%20check%20compares%20rows%20in%20any%20order.%0A%0A%20%20%20%20**Tools%3A**%20a%20list%20comprehension%20over%20the%20databases%2C%20%60db%5B-2%3A%5D%60%2C%20%60'01'%20AS%20region_code%60%2C%20%60%22%20UNION%20ALL%20BY%20NAME%20%22.join(...)%60%2C%20double%20quotes%20around%20the%20table's%20name%2C%20and%20%60.df()%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20stack_regions(con%3A%20duckdb.DuckDBPyConnection%2C%20table%3A%20str%2C%20databases%3A%20list%5Bstr%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20listed%20database's%20rows%20of%20one%20table%2C%20stacked%20by%20name%2C%20with%20region_code%20(the%20region's%20two%20digits%2C%20as%20text)%20first.%22%22%22%0A%20%20%20%20%23%20Exercise%202.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20one%20SELECT%20per%20database%3A%20the%20region's%20digits%20as%20a%20constant%20column%20region_code%2C%20then%20every%20column%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20of%20%3Cdatabase%3E.%22%3Ctable%3E%22%0A%20%20%20%20%23%20Step%202%3A%20join%20the%20SELECTs%20with%20%22%20UNION%20ALL%20BY%20NAME%20%22%2C%20run%20the%20query%2C%20and%20return%20it%20as%20a%20pandas%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.2%22)%0A%0A%0A%40app.function%0Adef%20two_regions()%20-%3E%20duckdb.DuckDBPyConnection%3A%0A%20%20%20%20%22%22%22Two%20small%20regional%20databases%2C%20in%20memory%2C%20attached%20as%20sfh2025_r03%20(area%20codes%20as%20numbers)%20and%20sfh2025_r13%20(area%0A%20%20%20%20codes%20as%20zero-padded%20text%2C%20and%20a%20fishing_unit%20table%20with%20its%20columns%20in%20another%20order).%20Household%20101%20is%20deleted%20in%0A%20%20%20%20sfh2025_r03%20and%20still%20in%20the%20survey%20in%20sfh2025_r13%3B%20household%20102%20is%20voided%20in%20sfh2025_r13%20and%20still%20in%20the%20survey%0A%20%20%20%20in%20sfh2025_r03%3B%20household%20103%20is%20excluded%20by%20the%20sampling%20side%2C%20and%20household%20104's%20unit%20is%20voided.%20Used%20by%20the%0A%20%20%20%20checks%20of%20Exercises%202.2%2C%202.3%20and%202.7.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20for%20db%2C%20code%20in%20((%22sfh2025_r03%22%2C%20%22INTEGER%22)%2C%20(%22sfh2025_r13%22%2C%20%22VARCHAR%22))%3A%0A%20%20%20%20%20%20%20%20con.execute(f%22ATTACH%20'%3Amemory%3A'%20AS%20%7Bdb%7D%22)%0A%20%20%20%20%20%20%20%20con.execute(f%22%22%22CREATE%20TABLE%20%7Bdb%7D.household%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20hh_code%20VARCHAR%2C%20region_code%20%7Bcode%7D%2C%20province_code%20%7Bcode%7D%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%20citymun_code%20%7Bcode%7D%2C%20barangay_code%20%7Bcode%7D%2C%20hh_interviewer%20VARCHAR%2C%20hh_deleted_on%20DATE%2C%20row_void%20INTEGER)%22%22%22)%0A%20%20%20%20%20%20%20%20con.execute(f%22%22%22CREATE%20TABLE%20%7Bdb%7D.selection%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20sel_stratum%20VARCHAR%2C%20sel_ea%20INTEGER%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%20sel_final_weight%20DOUBLE%2C%20sel_excluded%20INTEGER%2C%20row_void%20INTEGER)%22%22%22)%0A%20%20%20%20con.execute(%22%22%22INSERT%20INTO%20sfh2025_r03.household%20VALUES%0A%20%20%20%20%20%20%20%20(1%2C%20101%2C%20'0301401001001'%2C%203%2C%2014%2C%201%2C%201%2C%20'INT101'%2C%20DATE%20'2026-04-02'%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(2%2C%20102%2C%20'0301401001002'%2C%203%2C%2014%2C%201%2C%201%2C%20'INT101'%2C%20NULL%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(3%2C%20103%2C%20'0301402005001'%2C%203%2C%2014%2C%202%2C%205%2C%20'INT102'%2C%20NULL%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(4%2C%20104%2C%20'0301402005002'%2C%203%2C%2014%2C%202%2C%205%2C%20'INT102'%2C%20NULL%2C%20NULL)%22%22%22)%0A%20%20%20%20con.execute(%22%22%22INSERT%20INTO%20sfh2025_r13.household%20VALUES%0A%20%20%20%20%20%20%20%20(1%2C%20101%2C%20'1307404012001'%2C%20'13'%2C%20'074'%2C%20'04'%2C%20'012'%2C%20'INT201'%2C%20NULL%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(2%2C%20102%2C%20'1307404012002'%2C%20'13'%2C%20'074'%2C%20'04'%2C%20'012'%2C%20'INT201'%2C%20NULL%2C%201)%2C%0A%20%20%20%20%20%20%20%20(3%2C%20105%2C%20'1307410003001'%2C%20'13'%2C%20'074'%2C%20'10'%2C%20'003'%2C%20'INT202'%2C%20NULL%2C%20NULL)%22%22%22)%0A%20%20%20%20con.execute(%22INSERT%20INTO%20sfh2025_r03.selection%20VALUES%20(1%2C%20101%2C%20'F'%2C%20NULL%2C%201.0%2C%20NULL%2C%20NULL)%2C%20(2%2C%20102%2C%20'C'%2C%203004%2C%2015.6%2C%20NULL%2C%20NULL)%2C%20(3%2C%20103%2C%20'C'%2C%203011%2C%2016.2%2C%201%2C%20NULL)%2C%20(4%2C%20104%2C%20'B'%2C%202003%2C%205.3%2C%20NULL%2C%20NULL)%22)%0A%20%20%20%20con.execute(%22INSERT%20INTO%20sfh2025_r13.selection%20VALUES%20(1%2C%20101%2C%20'B'%2C%202007%2C%204.9%2C%20NULL%2C%20NULL)%2C%20(2%2C%20102%2C%20'C'%2C%203015%2C%2014.8%2C%20NULL%2C%20NULL)%2C%20(3%2C%20105%2C%20'F'%2C%20NULL%2C%201.0%2C%20NULL%2C%20NULL)%22)%0A%20%20%20%20con.execute(%22%22%22CREATE%20TABLE%20sfh2025_r03.fishing_unit%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20operator_line%20INTEGER%2C%20unit_no%20INTEGER%2C%0A%20%20%20%20%20%20%20%20unit_sold_share%20INTEGER%2C%20unit_ownership%20INTEGER%2C%20unit_uses_boat%20INTEGER%2C%20unit_boats%20INTEGER%2C%20unit_scale%20INTEGER%2C%0A%20%20%20%20%20%20%20%20unit_waters%20INTEGER%2C%20unit_months_active%20INTEGER%2C%20unit_hired_crew%20INTEGER%2C%20unit_assisted%20INTEGER%2C%20row_void%20INTEGER)%22%22%22)%0A%20%20%20%20con.execute(%22%22%22INSERT%20INTO%20sfh2025_r03.fishing_unit%20VALUES%0A%20%20%20%20%20%20%20%20(1%2C%20101%2C%201%2C%201%2C%203%2C%2010%2C%201%2C%202%2C%202%2C%203%2C%2010%2C%201%2C%200%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(2%2C%20102%2C%201%2C%201%2C%202%2C%2010%2C%201%2C%201%2C%201%2C%202%2C%2012%2C%200%2C%200%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(3%2C%20102%2C%201%2C%202%2C%201%2C%2020%2C%200%2C%20NULL%2C%201%2C%201%2C%206%2C%200%2C%201%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(4%2C%20103%2C%201%2C%201%2C%202%2C%2010%2C%201%2C%201%2C%201%2C%202%2C%2011%2C%200%2C%200%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(5%2C%20104%2C%202%2C%201%2C%202%2C%2030%2C%201%2C%201%2C%201%2C%202%2C%209%2C%200%2C%200%2C%201)%22%22%22)%0A%20%20%20%20con.execute(%22%22%22CREATE%20TABLE%20sfh2025_r13.fishing_unit%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20row_void%20INTEGER%2C%20unit_no%20INTEGER%2C%0A%20%20%20%20%20%20%20%20operator_line%20INTEGER%2C%20unit_sold_share%20INTEGER%2C%20unit_ownership%20INTEGER%2C%20unit_uses_boat%20INTEGER%2C%20unit_boats%20INTEGER%2C%0A%20%20%20%20%20%20%20%20unit_scale%20INTEGER%2C%20unit_waters%20INTEGER%2C%20unit_months_active%20INTEGER%2C%20unit_hired_crew%20INTEGER%2C%20unit_assisted%20INTEGER)%22%22%22)%0A%20%20%20%20con.execute(%22%22%22INSERT%20INTO%20sfh2025_r13.fishing_unit%20VALUES%0A%20%20%20%20%20%20%20%20(1%2C%20101%2C%20NULL%2C%201%2C%203%2C%203%2C%2020%2C%201%2C%201%2C%201%2C%202%2C%2012%2C%200%2C%200)%2C%0A%20%20%20%20%20%20%20%20(2%2C%20102%2C%20NULL%2C%201%2C%201%2C%202%2C%2010%2C%201%2C%202%2C%201%2C%202%2C%2010%2C%201%2C%200)%2C%0A%20%20%20%20%20%20%20%20(3%2C%20105%2C%20NULL%2C%201%2C%201%2C%203%2C%2040%2C%201%2C%203%2C%202%2C%203%2C%2011%2C%201%2C%201)%2C%0A%20%20%20%20%20%20%20%20(4%2C%20105%2C%20NULL%2C%201%2C%202%2C%202%2C%2010%2C%200%2C%20NULL%2C%201%2C%201%2C%208%2C%200%2C%200)%22%22%22)%0A%20%20%20%20return%20con%0A%0A%0A%40app.function%0Adef%20as_rows(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20table%20in%20one%20shape%20for%20the%20checks%3A%20columns%20in%20name%20order%2C%20rows%20sorted%20by%20every%20column%2C%20numbered%20from%200.%20So%20a%0A%20%20%20%20right%20answer%20passes%20whatever%20order%20its%20rows%20and%20columns%20came%20in.%22%22%22%0A%20%20%20%20columns%20%3D%20sorted(table.columns%2C%20key%3Dstr)%0A%20%20%20%20return%20table%5Bcolumns%5D.sort_values(columns%2C%20na_position%3D%22last%22%2C%20kind%3D%22stable%22).reset_index(drop%3DTrue)%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%202.2%22%2C%20stack_regions%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20fishing%20units%20of%20the%20three%20regions%22%2C%20(attach_regions()%2C%20%22fishing_unit%22%2C%20DATABASES))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20small%20regions%2C%20the%20second's%20columns%20in%20another%20order%20and%20a%20household%20id%20in%20both%22%2C%20(two_regions()%2C%20%22fishing_unit%22%2C%20%5B%22sfh2025_r03%22%2C%20%22sfh2025_r13%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20catch%20of%20regions%2004%20and%2007%22%2C%20(attach_regions()%2C%20%22catch%22%2C%20%5B%22sfh2025_r04%22%2C%20%22sfh2025_r07%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20selection%20rows%20alone%22%2C%20(attach_regions()%2C%20%22selection%22%2C%20%5B%22sfh2025_r07%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20databases%22%2C%20summarize%3Das_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22One%20SELECT%20per%20database%20with%20its%20last%20two%20characters%20as%20region_code%2C%20joined%20with%20UNION%20ALL%20BY%20NAME%3B%20the%20table's%20name%20in%20double%20quotes.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20catch%20of%20every%20region%20in%20one%20table%0A%20%20%20%20try_it(stack_regions%2C%20survey_con%2C%20%22catch%22%2C%20DATABASES)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20No%20%60region_code%60%20column%2C%20or%20%60region_code%60%20reads%20%601%60%20%7C%20The%20region%20was%20not%20added%2C%20or%20added%20as%20a%20number.%20%7C%20%60'01'%20AS%20region_code%60%2C%20in%20single%20quotes%20%7C%0A%20%20%20%20%7C%20%60region_code%60%20reads%20%60sfh2025_r01%60%20%7C%20The%20database's%20name%2C%20not%20its%20digits.%20%7C%20%60db%5B-2%3A%5D%60%20%7C%0A%20%20%20%20%7C%20Only%20the%20small%20regions'%20check%20is%20red%20%7C%20%60UNION%20ALL%60%20by%20position%3A%20sfh2025_r13's%20columns%20are%20in%20another%20order.%20%7C%20%60UNION%20ALL%20BY%20NAME%60%20%7C%0A%20%20%20%20%7C%20Fewer%20rows%20than%20the%20tables%20hold%20%7C%20%60UNION%60%20without%20%60ALL%60%20dropped%20rows%20that%20repeat.%20%7C%20%60UNION%20ALL%20BY%20NAME%60%20%7C%0A%20%20%20%20%7C%20*CatalogException%3A%20Table%20with%20name%20...%20does%20not%20exist*%20%7C%20The%20table%20named%20without%20its%20database%2C%20or%20with%20its%20name%20misspelt.%20%7C%20%60%7Bdb%7D.%22%7Btable%7D%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%20Deleted%20rows%2C%20region%20by%20region%0A%0A%20%20%20%20**The%20problem.**%20A%20fishing%20unit%20belongs%20in%20the%20flat%20table%20when%20four%20things%20hold%3A%20its%20household%20was%20not%20deleted%20in%20the%20capture%20system%20(%60hh_deleted_on%60%20is%20blank)%2C%20its%20household%20was%20not%20voided%20in%20editing%20(%60household%60's%20%60row_void%60%20is%20not%201)%2C%20its%20household%20was%20not%20left%20out%20by%20the%20sampling%20side%20(%60sel_excluded%60%20in%20%60selection%60%20is%20not%201)%2C%20and%20its%20own%20row%20is%20not%20voided%20(its%20%60row_void%60%20is%20not%201).%20Three%20of%20the%20four%20marks%20live%20in%20other%20tables%20than%20the%20unit.%0A%0A%20%20%20%20The%20tempting%20way%20is%20two%20steps%3A%20list%20the%20households%20still%20in%20the%20survey%20across%20every%20region%2C%20then%20keep%20the%20units%20whose%20%60hh_id%60%20is%20in%20that%20list.%20Household%20ids%20repeat%20across%20regions%2C%20so%20the%20list%20mixes%20regions%3A%20an%20id%20removed%20in%20one%20region%20is%20in%20the%20list%20because%20another%20region%20uses%20it%20for%20a%20household%20still%20in%20the%20survey.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20The%20tempting%20way%3A%20one%20list%20of%20household%20ids%20still%20in%20the%20survey%2C%20for%20all%20regions%2C%20and%20every%20region's%20units%20filtered%20against%20it%0A%20%20%20%20def%20_stack(select%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20return%20survey_con.sql(%22%20UNION%20ALL%20%22.join(f%22SELECT%20'%7B_db%5B-2%3A%5D%7D'%20AS%20region_code%2C%20%7Bselect.format(db%3D_db)%7D%22%20for%20_db%20in%20DATABASES)).df()%0A%0A%20%20%20%20_households%20%3D%20_stack(%22h.hh_id%2C%20h.hh_deleted_on%20IS%20NULL%20AND%20h.row_void%20IS%20DISTINCT%20FROM%201%20AND%20s.sel_excluded%20IS%20DISTINCT%20FROM%201%20AS%20kept%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%22FROM%20%7Bdb%7D.household%20AS%20h%20JOIN%20%7Bdb%7D.selection%20AS%20s%20ON%20s.hh_id%20%3D%20h.hh_id%22)%0A%20%20%20%20_units%20%3D%20_stack(%22hh_id%20FROM%20%7Bdb%7D.fishing_unit%22)%0A%20%20%20%20_one_list%20%3D%20set(_households.loc%5B_households%5B%22kept%22%5D%2C%20%22hh_id%22%5D)%20%20%23%20ids%20still%20in%20the%20survey%2C%20regions%20mixed%0A%20%20%20%20_gone%20%3D%20_households.loc%5B~_households%5B%22kept%22%5D%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D%5D%0A%20%20%20%20_slipped%20%3D%20_units.merge(_gone%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D)%0A%20%20%20%20_slipped%20%3D%20_slipped%5B_slipped%5B%22hh_id%22%5D.isin(_one_list)%5D%0A%20%20%20%20_example%20%3D%20_slipped.iloc%5B0%5D%0A%20%20%20%20mo.md(f%22Filtered%20against%20one%20list%20for%20all%20regions%2C%20**%7Blen(_slipped)%7D%20units%20of%20%7B_slipped.drop_duplicates().shape%5B0%5D%7D%20removed%20households%20stay%20in**.%20Household%20%7B_example%5B'hh_id'%5D%7D%20of%20region%20%7B_example%5B'region_code'%5D%7D%20is%20one%3A%20removed%20in%20its%20own%20region%2C%20its%20id%20is%20in%20the%20list%20because%20another%20region%20has%20a%20household%20still%20in%20the%20survey%20under%20the%20same%20number.%20A%20loop%20that%20filters%20inside%20each%20region%20gets%20this%20right%20without%20trying%3B%20one%20query%20must%20say%20it%20on%20purpose.%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.**%20Filter%20**inside%20each%20region's%20%60SELECT%60**%2C%20against%20that%20region's%20own%20tables%2C%20then%20stack.%20To%20keep%20the%20rows%20of%20one%20table%20whose%20key%20is%20in%20another%2C%20SQL%20has%20%60IN%60%20with%20a%20query%20inside%20it%2C%20a%20**subquery**%3A%0A%0A%20%20%20%20%60%60%60sql%0A%20%20%20%20SELECT%20'01'%20AS%20region_code%2C%20u.*%0A%20%20%20%20FROM%20sfh2025_r01.fishing_unit%20AS%20u%0A%20%20%20%20WHERE%20u.hh_id%20IN%20(SELECT%20hh_id%20FROM%20sfh2025_r01.household%20WHERE%20hh_deleted_on%20IS%20NULL)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60AS%20u%60%20gives%20the%20table%20a%20short%20name%2C%20so%20%60u.*%60%20is%20all%20its%20columns%20and%20%60u.row_void%60%20its%20own%20%60row_void%60%2C%20not%20the%20household's.%20Keeping%20a%20table's%20rows%20that%20have%20a%20match%20in%20another%2C%20without%20taking%20any%20column%20from%20it%2C%20is%20what%20dplyr%20calls%20a%20**semi-join**%3A%20%60semi_join(units%2C%20kept_households%2C%20by%20%3D%20%22hh_id%22)%60.%20Each%20region's%20subquery%20reads%20only%20that%20region's%20tables%2C%20so%20an%20id%20from%20another%20region%20cannot%20get%20in.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.3.%20Fishing%20units%20still%20in%20the%20survey%2C%20every%20region%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20these%20are%20the%20units%20the%20survey%20counts.%20Filtered%20against%20one%20list%20for%20all%20regions%2C%20the%20removed%20households%20of%20one%20region%20come%20back%20in%20under%20another%20region's%20ids%2C%20and%20every%20total%20built%20on%20them%20counts%20households%20that%20are%20not%20in%20the%20survey.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20connection%20with%20regional%20databases%20attached%2C%20and%20a%20list%20of%20their%20names%20%E2%86%92%20a%20pandas%20table%20of%20every%20listed%20region's%20rows%20of%20%60fishing_unit%60%20that%20are%20still%20in%20the%20survey%2C%20with%20%60region_code%60%20(the%20region's%20digits%2C%20as%20text)%20first%20and%20then%20every%20column%20of%20the%20units%20table.%20A%20unit%20is%20still%20in%20the%20survey%20when%2C%20**in%20its%20own%20region**%3A%0A%0A%20%20%20%20%7C%20table%20%7C%20condition%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60household%60%20%7C%20its%20household's%20%60hh_deleted_on%60%20is%20blank%20and%20its%20%60row_void%60%20is%20not%201%20%7C%0A%20%20%20%20%7C%20%60selection%60%20%7C%20its%20household's%20%60sel_excluded%60%20is%20not%201%20%7C%0A%20%20%20%20%7C%20%60fishing_unit%60%20%7C%20its%20own%20%60row_void%60%20is%20not%201%20%7C%0A%0A%20%20%20%20In%20the%20check's%20small%20regions%2C%20household%20101%20is%20deleted%20in%20sfh2025_r03%20and%20still%20in%20the%20survey%20in%20sfh2025_r13.%20The%20check%20compares%20rows%20in%20any%20order.%0A%0A%20%20%20%20**Tools%3A**%20Exercise%202.2's%20query%2C%20with%20a%20%60WHERE%60%20in%20each%20region's%20%60SELECT%60%3A%20%60IN%60%20with%20a%20subquery%2C%20%60IS%20NULL%60%2C%20%60IS%20DISTINCT%20FROM%60%2C%20and%20%60AS%20u%60%20(above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20live_units(con%3A%20duckdb.DuckDBPyConnection%2C%20databases%3A%20list%5Bstr%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20listed%20region's%20fishing%20units%20still%20in%20the%20survey%2C%20filtered%20region%20by%20region%2C%20with%20region_code%20(text)%20first.%22%22%22%0A%20%20%20%20%23%20Exercise%202.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20for%20each%20database%2C%20a%20SELECT%20of%20the%20region's%20digits%20AS%20region_code%20and%20every%20column%20of%20its%20fishing_unit%20(AS%20u)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20WHERE%20u.row_void%20is%20distinct%20from%201%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20AND%20u.hh_id%20IN%20(the%20region's%20household%20ids%20with%20hh_deleted_on%20blank%20and%20row_void%20distinct%20from%201)%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20AND%20u.hh_id%20IN%20(the%20region's%20selection%20ids%20with%20sel_excluded%20distinct%20from%201)%0A%20%20%20%20%23%20Step%202%3A%20join%20the%20SELECTs%20with%20%22%20UNION%20ALL%20BY%20NAME%20%22%2C%20run%2C%20and%20return%20a%20pandas%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.3%22)%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%202.3%22%2C%20live_units%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20small%20regions%3A%20household%20101%20deleted%20in%20one%20and%20still%20in%20the%20survey%20in%20the%20other%22%2C%20(two_regions()%2C%20%5B%22sfh2025_r03%22%2C%20%22sfh2025_r13%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20three%20regions%22%2C%20(attach_regions()%2C%20DATABASES))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004%20alone%22%2C%20(attach_regions()%2C%20%5B%22sfh2025_r04%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20databases%22%2C%20summarize%3Das_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Filter%20each%20region's%20units%20against%20its%20own%20household%20and%20selection%20tables%2C%20inside%20its%20own%20SELECT%3B%20a%20blank%20mark%20is%20not%20a%20removal.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20fishing%20units%20still%20in%20the%20survey%2C%20every%20region%0A%20%20%20%20try_it(live_units%2C%20survey_con%2C%20DATABASES)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.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%20No%20rows%20at%20all%20%7C%20%60row_void%20%3C%3E%201%60%2C%20%60sel_excluded%20%3C%3E%201%60%20or%20%60hh_deleted_on%20%3D%20NULL%60%3A%20a%20blank%20makes%20the%20condition%20%60NULL%60.%20%7C%20%60IS%20DISTINCT%20FROM%201%60%2C%20and%20%60IS%20NULL%60%20for%20the%20date%20%7C%0A%20%20%20%20%7C%20sfh2025_r03's%20household%20101%20is%20still%20there%20%7C%20The%20kept%20ids%20were%20collected%20across%20every%20region%2C%20then%20used%20for%20all.%20%7C%20each%20subquery%20reads%20%60%7Bdb%7D%60's%20own%20tables%20%7C%0A%20%20%20%20%7C%20sfh2025_r13's%20household%20102%20is%20still%20there%20%7C%20The%20household's%20%60row_void%60%20was%20not%20checked.%20%7C%20the%20subquery%20on%20%60household%60%20%7C%0A%20%20%20%20%7C%20sfh2025_r03's%20household%20103%20is%20still%20there%20%7C%20The%20selection's%20%60sel_excluded%60%20was%20not%20checked.%20%7C%20the%20subquery%20on%20%60selection%60%20%7C%0A%20%20%20%20%7C%20sfh2025_r03's%20household%20104%20is%20still%20there%20%7C%20The%20unit's%20own%20%60row_void%60%20was%20not%20checked.%20%7C%20%60u.row_void%20IS%20DISTINCT%20FROM%201%60%20%7C%0A%20%20%20%20%7C%20*BinderException%3A%20Ambiguous%20reference%20to%20column%20name%20%22row_void%22*%20%7C%20Two%20joined%20tables%20with%20%60row_void%60%20in%20one%20%60SELECT%60%2C%20the%20column%20named%20without%20its%20table.%20%7C%20%60u.row_void%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%20Codes%20kept%20whole%3A%20text%20types%2C%20zero-padding%2C%20null%20tokens%2C%20nullable%20integers%0A%0A%20%20%20%20**The%20problem.**%20Regions%2001%20and%2007%20store%20the%20area%20codes%20of%20%60household%60%20as%20**integers**%3A%20La%20Union's%20province%20code%20is%20%6033%60.%20Region%2004%20stores%20them%20as%20**text**%2C%20zero-padded%20to%20their%20width%3A%20Batangas's%20is%20%60%22010%22%60.%20Stack%20%60household%60%20across%20the%20regions%20and%20a%20database%20has%20to%20give%20%60province_code%60%20one%20type.%20DuckDB%20makes%20it%20text%2C%20so%20the%20integers%20become%20text%20**without%20their%20zeros**%2C%20and%20province%20codes%20of%20different%20widths%20sit%20in%20one%20column%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20household%20of%20every%20region%2C%20stacked%20by%20name%3A%20the%20province%20codes%2C%20and%20how%20many%20of%20them%20find%20their%20province%20in%20the%20code%20list%0A%20%20%20%20_stacked%20%3D%20survey_con.sql(%22%20UNION%20ALL%20BY%20NAME%20%22.join(f%22SELECT%20region_code%2C%20province_code%20FROM%20%7B_db%7D.household%22%20for%20_db%20in%20DATABASES)).df()%0A%20%20%20%20_provinces%20%3D%20course.codes(%22geography%22)%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22province%22%5D%5D.drop_duplicates()%0A%20%20%20%20_joined%20%3D%20_stacked.merge(_provinces%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%5D%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20_found%20%3D%20_joined%5B_joined%5B%22_merge%22%5D%20%3D%3D%20%22both%22%5D%0A%20%20%20%20_counts%20%3D%20_stacked.value_counts(%5B%22region_code%22%2C%20%22province_code%22%5D%2C%20sort%3DFalse).rename(%22households%22).reset_index()%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20_counts%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20code%20list%20(%60course.codes(%5C%22geography%5C%22)%60)%20writes%20every%20code%20zero-padded%3A%20%6001%60%2C%20%60033%60.%20Joined%20to%20it%20on%20region%20and%20province%2C%20**%7Blen(_found)%3A%2C%7D%20of%20%7Blen(_joined)%3A%2C%7D%20households%20find%20their%20province**%3A%20region%20%7B'%2C%20'.join(_found%5B'region_code'%5D.unique())%7D's.%20Every%20household%20of%20the%20other%20regions%20is%20left%20with%20no%20province%2C%20and%20nothing%20raised%20an%20error.%22)%2C%0A%20%20%20%20%5D%2C%20justify%3D%22start%22%2C%20gap%3D2%2C%20widths%3D%5B1%2C%202%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%20R's%20%60bind_rows%60%20refuses%20the%20same%20stack%3A%20it%20stops%20with%20an%20error%20that%20it%20cannot%20combine%20an%20integer%20column%20with%20a%20character%20one.%20In%20R%20you%20would%20pad%20every%20code%20before%20binding%2C%20with%20%60stringr%3A%3Astr_pad(province_code%2C%203%2C%20pad%20%3D%20%220%22)%60.%20A%20**code**%20is%20a%20label%2C%20not%20a%20quantity%3A%20nobody%20adds%20province%20codes.%20Keep%20codes%20as%20**text%2C%20padded%20with%20zeros%20to%20their%20width**%2C%20the%20form%20the%20PSGC%20and%20the%20code%20lists%20use.%20The%20widths%20are%20in%20%60course.AREA_CODE_WIDTH%60%3A%20%60region_code%60%202%2C%20%60province_code%60%203%2C%20%60citymun_code%60%202%2C%20%60barangay_code%60%203.%0A%0A%20%20%20%20**The%20tool.**%20Each%20library%20pads%20text%20on%20the%20left%3A%0A%0A%20%20%20%20%7C%20where%20%7C%20pad%20%60province_code%60%20to%203%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20SQL%20(DuckDB%2C%20MySQL)%20%7C%20%60lpad(CAST(province_code%20AS%20VARCHAR)%2C%203%2C%20'0')%60%20%7C%0A%20%20%20%20%7C%20pandas%20%7C%20%60table%5B%22province_code%22%5D.astype(%22Int64%22).astype(%22string%22).str.zfill(3)%60%20%7C%0A%20%20%20%20%7C%20polars%20%7C%20%60pl.col(%22province_code%22).cast(pl.String).str.zfill(3)%60%20%7C%0A%20%20%20%20%7C%20R%20%7C%20%60stringr%3A%3Astr_pad(province_code%2C%203%2C%20pad%20%3D%20%220%22)%60%20%7C%0A%0A%20%20%20%20%60zfill%60%20(zero%20fill)%20pads%20a%20text%20with%20zeros%20on%20the%20left%20to%20a%20width%3B%20a%20text%20already%20that%20wide%20is%20left%20alone%2C%20so%20%60%22010%22%60%20stays%20%60%22010%22%60.%20The%20trap%20is%20in%20pandas'%20middle%20step.%20A%20whole-number%20column%20with%20a%20missing%20value%20often%20arrives%20as%20**decimals**%20(%60float64%60%2C%20missing%20as%20%60NaN%60)%2C%20and%20decimals%20turn%20into%20text%20with%20their%20decimal%20point%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%20A%20province%20code%20column%20as%20decimals%2C%20because%20one%20value%20is%20missing%2C%20padded%20two%20ways%0A%20%20%20%20_codes%20%3D%20pd.Series(%5B33.0%2C%20None%2C%2055.0%5D%2C%20name%3D%22province_code%22)%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22as%20it%20arrives%22%3A%20_codes%2C%0A%20%20%20%20%20%20%20%20'.astype(str).str.zfill(3)'%3A%20_codes.astype(str).str.zfill(3)%2C%0A%20%20%20%20%20%20%20%20'.astype(%22Int64%22).astype(%22string%22).str.zfill(3)'%3A%20_codes.astype(%22Int64%22).astype(%22string%22).str.zfill(3)%2C%0A%20%20%20%20%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%60%2233.0%22%60%20is%20already%20three%20characters%20or%20more%2C%20so%20%60zfill(3)%60%20leaves%20it%20as%20it%20is.%20(The%20missing%20value%20stays%20missing%20in%20pandas%203%3B%20pandas%202%20turned%20it%20into%20the%20text%20%60%22nan%22%60.)%20Going%20through%20%60Int64%60%20first%2C%20pandas'%20nullable%20integer%2C%20drops%20the%20decimal%20point%20and%20keeps%20the%20missing%20value%20missing.%20Text%20that%20arrives%20already%20padded%20goes%20through%20%60pd.to_numeric%60%20first%2C%20which%20reads%20%60%22010%22%60%20as%2010%3B%20the%20padding%20then%20puts%20the%20zero%20back.%0A%0A%20%20%20%20%23%23%23%20Exercise%202.4.%20Area%20codes%20as%20zero-padded%20text%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20join%20to%20the%20code%20list%2C%20every%20table%20by%20province%20and%20every%20key%20that%20includes%20the%20region%20depends%20on%20these%20four%20columns%20reading%20the%20same%20in%20every%20region.%20One%20unpadded%20region%20and%20its%20households%20find%20no%20province%2C%20with%20no%20error.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20pandas%20table%20that%20has%20some%20or%20all%20of%20the%20area%20code%20columns%20(%60region_code%60%2C%20%60province_code%60%2C%20%60citymun_code%60%2C%20%60barangay_code%60)%2C%20stored%20as%20integers%2C%20as%20decimals%20with%20missing%20values%2C%20or%20as%20text%20with%20or%20without%20zeros%20%E2%86%92%20the%20same%20table%2C%20every%20other%20column%20unchanged%2C%20each%20area%20code%20column%20as%20**text%20zero-padded%20to%20its%20width**%20in%20%60AREA_CODE_WIDTH%60%2C%20a%20missing%20code%20still%20missing%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20arrives%20as%20%7C%20becomes%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60region_code%60%20%7C%20%601.0%60%2C%20%60NaN%60%20%7C%20%60%2201%22%60%2C%20missing%20%7C%0A%20%20%20%20%7C%20%60province_code%60%20%7C%20%60%2233%22%60%2C%20%60%22010%22%60%20%7C%20%60%22033%22%60%2C%20%60%22010%22%60%20%7C%0A%20%20%20%20%7C%20%60citymun_code%60%20%7C%20%601%60%20%7C%20%60%2201%22%60%20%7C%0A%20%20%20%20%7C%20%60barangay_code%60%20%7C%20%6019%60%20%7C%20%60%22019%22%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20loop%20over%20%60AREA_CODE_WIDTH.items()%60%2C%20%60column%20in%20table.columns%60%2C%20%60pd.to_numeric%60%2C%20%60.astype(%22Int64%22).astype(%22string%22).str.zfill(width)%60%2C%20above.%20Work%20on%20%60table.copy()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20pad_codes(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20table%20with%20each%20area%20code%20column%20it%20has%20as%20text%2C%20zero-padded%20to%20its%20width%20in%20AREA_CODE_WIDTH%3B%20missing%20stays%20missing.%22%22%22%0A%20%20%20%20%23%20Exercise%202.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20copy%20of%20the%20table%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20column%20and%20width%20in%20AREA_CODE_WIDTH%20that%20the%20table%20has%3A%20to%20numbers%20(pd.to_numeric)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20then%20nullable%20integers%20(%22Int64%22)%2C%20then%20text%20(%22string%22)%2C%20then%20.str.zfill(width)%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20copy%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.4%22)%0A%0A%0A%40app.function%0Adef%20column_kinds(table%3A%20pd.DataFrame%2C%20nullable%3A%20bool%20%3D%20False)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20table%20with%20the%20kind%20of%20value%20each%20column%20holds%2C%20for%20the%20checks%20of%20Exercises%202.4%20to%202.6%3A%20every%20type%20of%20text%20counts%0A%20%20%20%20as%20text%2C%20every%20whole-number%20type%20as%20whole%20numbers%20and%20every%20decimal%20type%20as%20decimals%2C%20so%20any%20correct%20way%20of%20reading%0A%20%20%20%20passes.%20With%20nullable%3DTrue%2C%20a%20whole-number%20type%20that%20can%20hold%20a%20missing%20value%20(Int64%2C%20Int32)%20is%20told%20apart%20from%20one%0A%20%20%20%20that%20cannot%20(int64).%20Rows%20are%20numbered%20from%200%2C%20so%20the%20index%20does%20not%20count.%22%22%22%0A%0A%20%20%20%20def%20kind(column%3A%20pd.Series)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20if%20pd.api.types.is_datetime64_any_dtype(column)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22dates%22%0A%20%20%20%20%20%20%20%20if%20pd.api.types.is_bool_dtype(column)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22true%20or%20false%22%0A%20%20%20%20%20%20%20%20if%20pd.api.types.is_integer_dtype(column)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20extension%20%3D%20isinstance(column.dtype%2C%20pd.api.extensions.ExtensionDtype)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22nullable%20whole%20numbers%22%20if%20nullable%20and%20extension%20else%20%22whole%20numbers%22%0A%20%20%20%20%20%20%20%20if%20pd.api.types.is_float_dtype(column)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22decimal%20numbers%22%0A%20%20%20%20%20%20%20%20if%20pd.api.types.is_string_dtype(column)%20and%20pd.api.types.infer_dtype(column%2C%20skipna%3DTrue)%20in%20(%22string%22%2C%20%22empty%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22text%22%0A%20%20%20%20%20%20%20%20return%20f%22other%20(%7Bcolumn.dtype%7D)%22%0A%0A%20%20%20%20return%20%7B%22kinds%22%3A%20%7Bstr(name)%3A%20kind(table%5Bname%5D)%20for%20name%20in%20table.columns%7D%2C%20%22table%22%3A%20table.reset_index(drop%3DTrue)%7D%0A%0A%0A%40app.function%0Adef%20codes_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20households%20whose%20area%20codes%20arrive%20four%20ways%3A%20decimals%20with%20a%20missing%20value%2C%20text%20with%20and%20without%20zeros%2C%0A%20%20%20%20nullable%20integers%2C%20and%20plain%20integers.%20Used%20by%20the%20check%20of%20Exercise%202.4.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5301%2C%205302%2C%205303%5D%2C%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B1.0%2C%20None%2C%204.0%5D%2C%0A%20%20%20%20%20%20%20%20%22province_code%22%3A%20pd.Series(%5B%2233%22%2C%20%22010%22%2C%20None%5D%2C%20dtype%3D%22string%22)%2C%0A%20%20%20%20%20%20%20%20%22citymun_code%22%3A%20pd.array(%5B1%2C%20None%2C%2010%5D%2C%20dtype%3D%22Int64%22)%2C%0A%20%20%20%20%20%20%20%20%22barangay_code%22%3A%20%5B7%2C%2019%2C%20120%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_interviewer%22%3A%20%5B%22INT004%22%2C%20%22INT009%22%2C%20%22INT012%22%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_regions%20%3D%20attach_regions()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.4%22%2C%20pad_codes%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20households%20whose%20codes%20arrive%20as%20decimals%2C%20text%2C%20nullable%20and%20plain%20integers%22%2C%20(codes_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20household%20table%2C%20codes%20as%20integers%22%2C%20(_regions.sql(%22SELECT%20*%20FROM%20sfh2025_r01.household%22).df()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20household%20table%2C%20codes%20as%20padded%20text%22%2C%20(_regions.sql(%22SELECT%20*%20FROM%20sfh2025_r04.household%22).df()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20landing%20sites%2C%20province%20and%20city%20codes%20under%20other%20names%22%2C%20(_regions.sql(%22SELECT%20*%20FROM%20sfh2025_r07.landing%22).df()%2C))%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%3Dcolumn_kinds%2C%0A%20%20%20%20%20%20%20%20hint%3D%22For%20each%20code%20column%20the%20table%20has%3A%20pd.to_numeric%2C%20then%20.astype('Int64').astype('string').str.zfill(width).%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%20three%20households%20whose%20codes%20arrive%20four%20ways%0A%20%20%20%20try_it(pad_codes%2C%20codes_fixture())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.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%20%60%221.0%22%60%20in%20%60region_code%60%20%7C%20Decimals%20turned%20into%20text%20without%20%60Int64%60%20first.%20%7C%20%60.astype(%22Int64%22)%60%20before%20%60.astype(%22string%22)%60%20%7C%0A%20%20%20%20%7C%20%60%22nan%22%60%20or%20%60%22%3CNA%3E%22%60%20as%20a%20code%20%7C%20A%20missing%20value%20turned%20into%20text.%20%7C%20%60Int64%60%2C%20then%20%60%22string%22%60%2C%20keeps%20it%20missing%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'region_code'*%20%7C%20A%20code%20column%20the%20table%20does%20not%20have.%20%7C%20%60if%20column%20in%20out.columns%60%20%7C%0A%20%20%20%20%7C%20%60%2233%22%60%20in%20%60province_code%60%20%7C%20%60zfill%60%20with%20the%20wrong%20width%2C%20or%20none.%20%7C%20%60zfill(width)%60%20from%20%60AREA_CODE_WIDTH%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%20CSV%20export%3A%20Windows-1252%2C%20%60%5CN%60%20and%20padded%20text%0A%0A%20%20%20%20**The%20problem.**%20Surveys%20also%20travel%20as%20files%3A%20the%20database's%20export%20writes%20each%20table%20as%20a%20CSV%2C%20one%20folder%20per%20region%20(%60data%2Fexport%2Fr01%2Fhousehold.csv%60%20and%20so%20on).%20Arrays%20%26%20DataFrames'%20module%203%20taught%20%60dtype%3D%60%20for%20codes.%20This%20export%20adds%20three%20habits%20of%20its%20own.%20Here%20are%20the%20first%20lines%20of%20region%2004's%20landing%20sites%2C%20as%20the%20file%20holds%20them%2C%20and%20a%20line%20further%20down%20with%20a%20letter%20outside%20English%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%20first%20lines%20of%20an%20exported%20file%2C%20as%20text%3A%20each%20line%20is%20one%20row%2C%20the%20fields%20separated%20by%20commas%0A%20%20%20%20_lines%20%3D%20(PATHS.export%20%2F%20%22r04%22%20%2F%20%22landing.csv%22).read_bytes().decode(%22cp1252%22).splitlines()%0A%20%20%20%20_named%20%3D%20next(_line%20for%20_line%20in%20_lines%20if%20%22Ni%C3%B1o%22%20in%20_line)%0A%20%20%20%20mo.md(%22%60%60%60%5Cn%22%20%2B%20%22%5Cn%22.join(%5B_lines%5B0%5D%2C%20_lines%5B1%5D%2C%20_named%5D)%20%2B%20%22%5Cn%60%60%60%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%7C%20what%20you%20see%20%7C%20what%20it%20is%20%7C%20what%20it%20does%20to%20a%20plain%20%60pd.read_csv%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%5CN%60%20%7C%20the%20export's%20word%20for%20a%20missing%20value%20(MySQL%20writes%20%60NULL%60%20this%20way%20in%20its%20exports)%20%7C%20a%20whole-number%20column%20with%20any%20%60%5CN%60%20in%20it%20becomes%20text%20%7C%0A%20%20%20%20%7C%20%60land_name%60%20followed%20by%20spaces%20%7C%20text%20answers%20**padded**%20with%20spaces%20to%2030%20characters%20%7C%20%60%22Santo%20Ni%C3%B1o%20wharf%22%60%20is%20a%20different%20text%20from%20%60%22Santo%20Ni%C3%B1o%20wharf%20%20%20%22%60%20%7C%0A%20%20%20%20%7C%20%60%C3%B1%60%20in%20%60Ni%C3%B1o%60%20%7C%20the%20file%20is%20**Windows-1252**%3A%20each%20character%20one%20byte%20%7C%20Python%20reads%20files%20as%20UTF-8%20by%20default%2C%20and%20stops%20%7C%0A%0A%20%20%20%20An%20**encoding**%20is%20the%20rule%20that%20turns%20characters%20into%20bytes.%20UTF-8%2C%20today's%20default%2C%20writes%20%60%C3%B1%60%20as%20two%20bytes%3B%20Windows-1252%20(Python's%20name%20for%20it%20is%20%60cp1252%60)%20writes%20it%20as%20one%20byte%2C%20%600xf1%60.%20Read%20with%20the%20wrong%20rule%2C%20that%20byte%20is%20not%20valid%20UTF-8%2C%20and%20the%20error's%20last%20line%20says%20where%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%20landing%20file%20with%20pandas'%20defaults%2C%20then%20the%20household%20file%20with%20the%20encoding%20only%0A%20%20%20%20_path%20%3D%20PATHS.export%20%2F%20%22r04%22%20%2F%20%22landing.csv%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20pd.read_csv(_path)%0A%20%20%20%20%20%20%20%20_message%20%3D%20%22no%20error%22%0A%20%20%20%20except%20UnicodeDecodeError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_message%20%3D%20f%22UnicodeDecodeError%3A%20%7B_error%7D%22%0A%20%20%20%20_households%20%3D%20pd.read_csv(PATHS.export%20%2F%20%22r04%22%20%2F%20%22household.csv%22%2C%20encoding%3D%22cp1252%22)%0A%20%20%20%20_first%20%3D%20pd.read_csv(PATHS.export%20%2F%20%22r04%22%20%2F%20%22household.csv%22%2C%20encoding%3D%22cp1252%22%2C%20dtype%3Dstr).iloc%5B0%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(f%22%60%60%60%5Cn%7B_message%7D%5Cn%60%60%60%22)%2C%20kind%3D%22warn%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22With%20%60encoding%3D%5C%22cp1252%5C%22%60%20the%20file%20reads.%20Region%2004's%20%60household.csv%60%20then%20comes%20back%20like%20this%3A%20the%20household%20code%20%60%7B_first%5B'hh_code'%5D%7D%60%20as%20the%20number%20**%7B_households%5B'hh_code'%5D.iloc%5B0%5D%7D**%2C%20the%20province%20%60%7B_first%5B'province_code'%5D%7D%60%20as%20**%7B_households%5B'province_code'%5D.iloc%5B0%5D%7D**%2C%20and%20%60row_void%60%2C%20a%20whole-number%20mark%2C%20as%20**%7B_households%5B'row_void'%5D.dtype%7D**%20holding%20the%20two-character%20text%20%60%7B_households%5B'row_void'%5D.iloc%5B0%5D%7D%60.%22)%2C%0A%20%20%20%20%20%20%20%20_households.dtypes.astype(str).to_frame(%22type%2C%20plain%20read%22).T%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**The%20tool.**%20%60pd.read_csv%60%20takes%20each%20habit%20as%20an%20argument%3A%0A%0A%20%20%20%20%7C%20argument%20%7C%20here%20%7C%20why%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60encoding%3D%22cp1252%22%60%20%7C%20the%20file's%20encoding%20%7C%20%60%C3%B1%60%20reads%20as%20%60%C3%B1%60%20%7C%0A%20%20%20%20%7C%20%60na_values%3D%5Br%22%5CN%22%5D%60%20%7C%20%60%5CN%60%20is%20missing%20%7C%20whole-number%20columns%20stay%20numbers%20(%60r%22...%22%60%20keeps%20the%20backslash%20as%20written)%20%7C%0A%20%20%20%20%7C%20%60keep_default_na%3DFalse%60%20%7C%20%60%5CN%60%20is%20the%20**only**%20missing%20marker%20%7C%20see%20below%20%7C%0A%20%20%20%20%7C%20%60dtype%3D%7B%22hh_code%22%3A%20str%2C%20%22province_code%22%3A%20str%2C%20...%7D%60%20%7C%20codes%20as%20text%20%7C%20the%20zero%20stays%3B%20module%203%20of%20Arrays%20%26%20DataFrames%20%7C%0A%20%20%20%20%7C%20%60dtype_backend%3D%22numpy_nullable%22%60%20%7C%20nullable%20types%20for%20every%20other%20column%20%7C%20a%20whole-number%20column%20with%20missing%20values%20stays%20whole%20(%60Int64%60)%2C%20not%20decimals%20%7C%0A%0A%20%20%20%20and%20after%20the%20read%2C%20%60.str.rstrip()%60%20removes%20the%20padding%20at%20the%20end%20of%20each%20text.%20**Why%20%60keep_default_na%3DFalse%60.**%20By%20default%20pandas%20treats%20nineteen%20texts%20as%20missing%20on%20top%20of%20what%20you%20name%3A%20the%20empty%20field%2C%20%60NA%60%2C%20%60N%2FA%60%2C%20%60NULL%60%2C%20%60None%60%20and%20%60nan%60%20among%20them.%20The%20database%20tells%20a%20missing%20answer%20(%60%5CN%60%2C%20its%20%60NULL%60)%20from%20an%20empty%20one%20(a%20text%20answered%20with%20nothing).%20With%20the%20defaults%2C%20both%20become%20missing%3B%20with%20%60keep_default_na%3DFalse%60%2C%20the%20table%20keeps%20the%20difference%20the%20database%20made%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%20landing%20sites%3A%20a%20name%20answered%20empty%2C%20and%20one%20missing%20(%5CN)%0A%20%20%20%20_text%20%3D%20%22land_line%2Cland_type%2Cland_name%5Cn1%2C1%2C%5Cn2%2C3%2C%5C%5CN%5Cn%22%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22%60na_values%3D%5Br%5C%22%5C%5CN%5C%22%5D%60%22)%2C%20pd.read_csv(io.StringIO(_text)%2C%20na_values%3D%5Br%22%5CN%22%5D)%5D)%2C%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22%60na_values%3D%5Br%5C%22%5C%5CN%5C%22%5D%2C%20keep_default_na%3DFalse%60%22)%2C%20pd.read_csv(io.StringIO(_text)%2C%20na_values%3D%5Br%22%5CN%22%5D%2C%20keep_default_na%3DFalse)%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%20The%20bytes%20where%20Latin-1%20and%20Windows-1252%20read%20differently%2C%20and%20how%20many%20of%20the%20export's%20bytes%20are%20among%20them%0A%20%20%20%20_differ%20%3D%20%5Bb%20for%20b%20in%20range(256)%20if%20bytes(%5Bb%5D).decode(%22latin-1%22)%20!%3D%20bytes(%5Bb%5D).decode(%22cp1252%22%2C%20errors%3D%22replace%22)%5D%0A%20%20%20%20_export%20%3D%20b%22%22.join(p.read_bytes()%20for%20p%20in%20PATHS.export.rglob(%22*.csv%22))%0A%20%20%20%20_in_export%20%3D%20sum(_export.count(bytes(%5Bb%5D))%20for%20b%20in%20_differ)%0A%20%20%20%20mo.md(f%22Latin-1%20and%20Windows-1252%20read%20**%7Blen(_differ)%7D%20byte%20values**%20differently%2C%20%60%7B_differ%5B0%5D%3A%2304x%7D%60%20to%20%60%7B_differ%5B-1%5D%3A%2304x%7D%60%2C%20and%20agree%20on%20the%20other%20%7B256%20-%20len(_differ)%7D.%20The%20export%20holds%20**%7B_in_export%3A%2C%7D**%20bytes%20among%20those%20%7Blen(_differ)%7D%2C%20so%20read%20as%20Latin-1%20it%20gives%20the%20same%20text%20as%20read%20as%20Windows-1252.%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**DuckDB%20reads%20CSV%20too**%2C%20with%20the%20same%20settings%20under%20its%20own%20names.%20Without%20an%20extension%20it%20knows%20three%20encodings%2C%20UTF-8%2C%20UTF-16%20and%20Latin-1%3B%20Windows-1252%20is%20in%20its%20%60encodings%60%20extension%2C%20which%20it%20downloads.%20As%20the%20cell%20above%20counts%2C%20Latin-1%20reads%20this%20export%20right%2C%20so%20%60encoding%20%3D%20'latin-1'%60%20needs%20nothing%20more.%20Its%20reader%20guesses%20each%20column's%20type%20from%20the%20values%20it%20samples%20(the%20**sniffer**)%3A%20it%20keeps%20region%2004's%20%6004%60%20as%20text%2C%20because%20of%20the%20leading%20zero%2C%20and%20reads%20region%2001's%20%601%60%20as%20a%20number.%20Name%20the%20types%2C%20and%20both%20regions%20read%20as%20text%2C%20though%20still%20without%20the%20zeros%20the%20export%20left%20out%3A%0A%0A%20%20%20%20%60%60%60sql%0A%20%20%20%20SELECT%20*%20FROM%20read_csv('data%2Fexport%2Fr01%2Fhousehold.csv'%2C%20encoding%20%3D%20'latin-1'%2C%20nullstr%20%3D%20'%5CN'%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%20types%20%3D%20%7B'hh_code'%3A%20'VARCHAR'%2C%20'region_code'%3A%20'VARCHAR'%2C%20'province_code'%3A%20'VARCHAR'%7D)%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%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20sniffer's%20guess%20for%20region_code%20and%20province_code%2C%20in%20each%20region's%20export%2C%20then%20with%20the%20types%20named%2C%20and%20the%20first%20row%0A%20%20%20%20def%20_read(reg%3A%20str%2C%20named%3A%20bool)%20-%3E%20duckdb.DuckDBPyRelation%3A%0A%20%20%20%20%20%20%20%20_named%20%3D%20%22%2C%20types%20%3D%20%7B'region_code'%3A%20'VARCHAR'%2C%20'province_code'%3A%20'VARCHAR'%7D%22%20if%20named%20else%20%22%22%0A%20%20%20%20%20%20%20%20return%20duckdb.sql(f%22SELECT%20region_code%2C%20province_code%20FROM%20read_csv('%7B(PATHS.export%20%2F%20f'r%7Breg%7D'%20%2F%20'household.csv').as_posix()%7D'%2C%20encoding%20%3D%20'latin-1'%2C%20nullstr%20%3D%20'%5C%5CN'%7B_named%7D)%22)%0A%0A%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7Bf%22region%20%7B_reg%7D%2C%20%7B'types%20named'%20if%20_named%20else%20'guessed'%7D%22%3A%20%5Bf%22%7B_t%7D%3A%20%7B_v!r%7D%22%20for%20_t%2C%20_v%20in%20zip(_read(_reg%2C%20_named).types%2C%20_read(_reg%2C%20_named).limit(1).fetchall()%5B0%5D)%5D%0A%20%20%20%20%20%20%20%20%20for%20_named%20in%20(False%2C%20True)%20for%20_reg%20in%20REGIONS%7D%2C%0A%20%20%20%20%20%20%20%20index%3D%5B%22region_code%22%2C%20%22province_code%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.5.%20Read%20a%20table%20of%20the%20export%20as%20the%20database%20holds%20it%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20table%20read%20from%20the%20export%20should%20be%20the%20table%20in%20the%20database%3A%20the%20same%20codes%2C%20the%20same%20missing%20values%2C%20the%20same%20text.%20Then%20any%20check%20on%20one%20holds%20for%20the%20other%2C%20and%20a%20region%20that%20sends%20files%20instead%20of%20database%20access%20is%20processed%20the%20same%20way.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20one%20table's%20CSV%20in%20the%20export%20(any%20table%2C%20any%20region)%20%E2%86%92%20a%20pandas%20table%20where%0A%0A%20%20%20%20%7C%20column%20%7C%20in%20the%20file%20%7C%20in%20your%20table%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60hh_code%60%20and%20the%20area%20codes%20it%20has%20%7C%20%601%60%2C%20%6033%60%2C%20%60010%60%20%7C%20text%2C%20zero-padded%20to%20its%20width%20(%60%2201%22%60%2C%20%60%22033%22%60%2C%20%60%22010%22%60)%3B%20%60hh_code%60%20as%20written%20%7C%0A%20%20%20%20%7C%20a%20whole-number%20column%20with%20%60%5CN%60%20%7C%20%60%5CN%60%2C%20%601%60%20%7C%20whole%20numbers%2C%20missing%20for%20%60%5CN%60%20(not%20decimals%2C%20not%20text)%20%7C%0A%20%20%20%20%7C%20a%20text%20answer%20%7C%20%60Santo%20Ni%C3%B1o%20wharf%60%20and%20spaces%2C%20or%20nothing%20%7C%20%60%22Santo%20Ni%C3%B1o%20wharf%22%60%3B%20an%20empty%20answer%20stays%20an%20empty%20text%20(%60''%60)%2C%20not%20missing%20%7C%0A%20%20%20%20%7C%20anything%20else%20%7C%20%7C%20as%20%60dtype_backend%3D%22numpy_nullable%22%60%20reads%20it%20(%60hh_interview_date%60%20stays%20text)%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60pd.read_csv%60%20with%20%60encoding%3D%60%2C%20%60na_values%3D%60%2C%20%60keep_default_na%3D%60%2C%20%60dtype%3D%60%20and%20%60dtype_backend%3D%60%3B%20then%20%60.str.zfill()%60%20for%20the%20codes%20and%20%60.str.rstrip()%60%20for%20text%2C%20above.%20A%20%60dtype%3D%60%20entry%20for%20a%20column%20the%20file%20does%20not%20have%20is%20ignored%2C%20so%20one%20dictionary%20serves%20every%20table.%20You%20may%20call%20your%20%60pad_codes%60%20from%20Exercise%202.4.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_export(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20table%20of%20the%20CSV%20export%3A%20Windows-1252%2C%20only%20%5C%5CN%20missing%2C%20codes%20as%20zero-padded%20text%2C%20whole%20numbers%20whole%2C%20text%20unpadded.%22%22%22%0A%20%20%20%20%23%20Exercise%202.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20pd.read_csv%20with%20the%20file's%20encoding%2C%20%5CN%20as%20the%20only%20missing%20value%20(na_values%2C%20keep_default_na)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20hh_code%20and%20the%20area%20codes%20as%20text%20(dtype%3D)%2C%20and%20dtype_backend%3D%22numpy_nullable%22%0A%20%20%20%20%23%20Step%202%3A%20pad%20each%20area%20code%20column%20the%20table%20has%20to%20its%20width%20in%20AREA_CODE_WIDTH%0A%20%20%20%20%23%20Step%203%3A%20remove%20the%20spaces%20at%20the%20end%20of%20every%20text%20column%20(.str.rstrip())%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.5%22)%0A%0A%0A%40app.function%0Adef%20export_fixture()%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22A%20small%20landing%20table%20written%20as%20the%20export%20writes%20it%2C%20for%20the%20check%20of%20Exercise%202.5%3A%20Windows-1252%2C%20a%20name%20with%20%C3%B1%0A%20%20%20%20padded%20with%20spaces%2C%20a%20name%20answered%20empty%2C%20one%20missing%20(%5C%5CN)%2C%20and%20a%20voided%20row.%22%22%22%0A%20%20%20%20header%20%3D%20%22row_id%2Chh_id%2Cland_operator_line%2Cland_unit_no%2Cland_line%2Cland_type%2Cland_province_code%2Cland_citymun_code%2Cland_name%2Crow_void%5Cn%22%0A%20%20%20%20rows%20%3D%20%5B%221%2C5012%2C1%2C1%2C1%2C2%2C33%2C10%2C%22%20%2B%20%22Santo%20Ni%C3%B1o%20wharf%22.ljust(30)%20%2B%20%22%2C%5C%5CN%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222%2C5012%2C1%2C1%2C2%2C1%2C33%2C10%2C%2C%5C%5CN%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223%2C5013%2C1%2C1%2C1%2C1%2C33%2C17%2C%5C%5CN%2C1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%224%2C5014%2C1%2C1%2C1%2C3%2C55%2C2%2C%22%20%2B%20%22%22.ljust(30)%20%2B%20%22%2C%5C%5CN%22%5D%0A%20%20%20%20path%20%3D%20SCRATCH%20%2F%20%22export_landing.csv%22%0A%20%20%20%20path.write_bytes((header%20%2B%20%22%5Cn%22.join(rows)%20%2B%20%22%5Cn%22).encode(%22cp1252%22))%0A%20%20%20%20return%20path%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%202.5%22%2C%20read_export%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20household.csv%3A%20codes%20written%20without%20their%20zeros%22%2C%20(PATHS.export%20%2F%20%22r01%22%20%2F%20%22household.csv%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20household.csv%3A%20codes%20written%20with%20their%20zeros%22%2C%20(PATHS.export%20%2F%20%22r04%22%20%2F%20%22household.csv%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20landing.csv%3A%20Windows-1252%2C%20padded%20names%2C%20%5C%5CN%20in%20every%20row_void%22%2C%20(PATHS.export%20%2F%20%22r07%22%20%2F%20%22landing.csv%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20landing%20file%3A%20a%20name%20answered%20empty%2C%20one%20missing%2C%20one%20with%20%C3%B1%22%2C%20(export_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%22%2C%20summarize%3Dcolumn_kinds%2C%0A%20%20%20%20%20%20%20%20hint%3D%22encoding%3D'cp1252'%2C%20na_values%3D%5Br'%5C%5CN'%5D%2C%20keep_default_na%3DFalse%2C%20the%20codes%20as%20str%2C%20dtype_backend%3D'numpy_nullable'%3B%20then%20zfill%20the%20codes%20and%20rstrip%20the%20text.%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%20region%2001's%20households%20from%20the%20export%3B%20the%20codes%20should%20read%2001%2C%20033%2C%20...%0A%20%20%20%20try_it(read_export%2C%20PATHS.export%20%2F%20%22r01%22%20%2F%20%22household.csv%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%202.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%20*UnicodeDecodeError%3A%20'utf-8'%20codec%20can't%20decode%20byte%200xf1*%20%7C%20The%20file%20read%20as%20UTF-8.%20%7C%20%60encoding%3D%22cp1252%22%60%20%7C%0A%20%20%20%20%7C%20%60row_void%60%20holds%20the%20text%20%60%5CN%60%20%7C%20%60%5CN%60%20is%20not%20named%20as%20missing.%20%7C%20%60na_values%3D%5Br%22%5CN%22%5D%60%20%7C%0A%20%20%20%20%7C%20%60row_void%60%20is%20decimals%20(%601.0%60)%20%7C%20%60%5CN%60%20read%20as%20missing%2C%20but%20into%20a%20decimal%20column.%20%7C%20%60dtype_backend%3D%22numpy_nullable%22%60%20%7C%0A%20%20%20%20%7C%20%60hh_code%60%20is%20a%20number%2C%20or%20%60province_code%60%20reads%20%6010%60%20%7C%20Codes%20read%20as%20numbers%3A%20the%20zeros%20are%20gone%20before%20you%20can%20pad%20them.%20%7C%20%60dtype%3D%7B%22hh_code%22%3A%20str%2C%20...%7D%60%20%7C%0A%20%20%20%20%7C%20%60province_code%60%20reads%20%6033%60%20in%20region%2001%20%7C%20The%20export%20never%20wrote%20the%20zeros.%20%7C%20%60.str.zfill(width)%60%20%7C%0A%20%20%20%20%7C%20Only%20the%20small%20file's%20check%20is%20red%20%7C%20%60keep_default_na%60%20left%20on%3A%20the%20empty%20name%20became%20missing.%20%7C%20%60keep_default_na%3DFalse%60%20%7C%0A%20%20%20%20%7C%20Landing%20names%20end%20in%20spaces%20%7C%20The%20padding%20is%20still%20there.%20%7C%20%60.str.rstrip()%60%20on%20text%20columns%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%20Nullable%20integers%2C%20and%20SQLite%20as%20the%20fallback%0A%0A%20%20%20%20**The%20problem.**%20A%20learner%20without%20DuckDB%20still%20has%20a%20database%20in%20Python%3A%20the%20standard%20library's%20%60sqlite3%60%20reads%20**SQLite**%20files%2C%20another%20one-file%20database%2C%20with%20no%20package%20to%20install.%20One%20SQLite%20file%20per%20region%20works%20the%20way%20the%20DuckDB%20files%20do%2C%20and%20SQLite%20has%20its%20own%20%60ATTACH%20DATABASE%20'sfh2025_r01.sqlite'%20AS%20sfh2025_r01%60.%20The%20cell%20below%20makes%20a%20SQLite%20copy%20of%20regions%2001%20and%2004%20in%20a%20scratch%20folder%2C%20then%20reads%20%60household%60%20back%20with%20%60pd.read_sql%60%2C%20the%20usual%20way%20to%20put%20a%20SQL%20query%20into%20pandas%20from%20a%20%60sqlite3%60%20connection.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20sqlite_copy(reg%3A%20str)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22A%20SQLite%20copy%20of%20one%20region's%20database%2C%20in%20the%20notebook's%20scratch%20folder%2C%20made%20with%20pandas'%20to_sql.%22%22%22%0A%20%20%20%20path%20%3D%20SCRATCH%20%2F%20f%22%7Bcourse.SURVEY%7D_r%7Breg%7D.sqlite%22%0A%20%20%20%20if%20not%20path.exists()%3A%0A%20%20%20%20%20%20%20%20source%2C%20target%20%3D%20duckdb.connect(str(PATHS.region(reg))%2C%20read_only%3DTrue)%2C%20sqlite3.connect(path)%0A%20%20%20%20%20%20%20%20for%20(name%2C)%20in%20source.sql(%22SHOW%20TABLES%22).fetchall()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20source.sql(f'SELECT%20*%20FROM%20%22%7Bname%7D%22').df().to_sql(name%2C%20target%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20target.close()%0A%20%20%20%20%20%20%20%20source.close()%0A%20%20%20%20return%20path%0A%0A%0A%40app.function%0Adef%20read_sqlite(reg%3A%20str%2C%20table%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20table%20of%20a%20region's%20SQLite%20copy%2C%20read%20with%20pandas.read_sql%20from%20a%20standard-library%20sqlite3%20connection.%22%22%22%0A%20%20%20%20con%20%3D%20sqlite3.connect(sqlite_copy(reg))%0A%20%20%20%20rows%20%3D%20pd.read_sql(f'SELECT%20*%20FROM%20%22%7Btable%7D%22'%2C%20con)%0A%20%20%20%20con.close()%0A%20%20%20%20return%20rows%0A%0A%0A%40app.cell%0Adef%20_(region01)%3A%0A%20%20%20%20%23%20row_void%20of%20region%2001's%20household%20table%2C%20read%20four%20ways%0A%20%20%20%20_duck%20%3D%20region01.sql(%22SELECT%20row_void%20FROM%20household%22)%0A%20%20%20%20_con%20%3D%20sqlite3.connect(sqlite_copy(%2201%22))%0A%20%20%20%20_reads%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22DuckDB%2C%20.df()%22%3A%20_duck.df()%5B%22row_void%22%5D%2C%0A%20%20%20%20%20%20%20%20%22DuckDB%2C%20.pl()%22%3A%20_duck.pl()%5B%22row_void%22%5D%2C%0A%20%20%20%20%20%20%20%20%22SQLite%2C%20pd.read_sql%22%3A%20pd.read_sql(%22SELECT%20row_void%20FROM%20household%22%2C%20_con)%5B%22row_void%22%5D%2C%0A%20%20%20%20%20%20%20%20'SQLite%2C%20pd.read_sql(...%2C%20dtype_backend%3D%22numpy_nullable%22)'%3A%20pd.read_sql(%22SELECT%20row_void%20FROM%20household%22%2C%20_con%2C%20dtype_backend%3D%22numpy_nullable%22)%5B%22row_void%22%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20_con.close()%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22read%20with%22%3A%20list(_reads)%2C%0A%20%20%20%20%20%20%20%20%22row_void's%20type%22%3A%20%5Bstr(_column.dtype)%20for%20_column%20in%20_reads.values()%5D%2C%0A%20%20%20%20%20%20%20%20%22a%20mark%20of%201%20reads%22%3A%20%5Bstr(_column.drop_nulls()%5B0%5D%20if%20isinstance(_column%2C%20pl.Series)%20else%20_column.dropna().iloc%5B0%5D)%20for%20_column%20in%20_reads.values()%5D%2C%0A%20%20%20%20%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%60pd.read_sql%60%20turns%20a%20whole-number%20column%20with%20a%20missing%20value%20into%20**decimals**%3A%20%60float64%60%20is%20NumPy's%20only%20type%20with%20a%20missing%20value%20(%60NaN%60)%2C%20so%20the%20mark%201%20becomes%20%601.0%60.%20Nothing%20is%20lost%20on%20a%20mark%2C%20but%20a%20code%20read%20as%20%6033.0%60%20and%20then%20padded%20reads%20%60%2233.0%22%60%2C%20and%20an%20id%20read%20as%20%605012.0%60%20no%20longer%20joins%20to%20%605012%60%20written%20as%20text.%20%60dtype_backend%3D%22numpy_nullable%22%60%20asks%20for%20nullable%20types%20instead%2C%20and%20gets%20them%20from%20the%20rows%3A%20a%20column%20whose%20rows%20in%20this%20read%20are%20all%20missing%20gets%20no%20number%20type%20at%20all%2C%20as%20the%20next%20cell%20shows.%0A%0A%20%20%20%20SQLite%20has%20a%20second%20habit%2C%20its%20**type%20affinity**%3A%20a%20value%20put%20into%20a%20column%20declared%20%60INTEGER%60%20is%20stored%20as%20an%20integer%20when%20it%20looks%20like%20one.%20Region%2004's%20%60%22010%22%60%2C%20written%20into%20an%20%60INTEGER%60%20column%2C%20comes%20back%20as%2010.%20pandas'%20%60to_sql%60%20declared%20region%2004's%20codes%20%60TEXT%60%2C%20so%20the%20copy%20kept%20them%3B%20a%20table%20someone%20else%20created%20may%20not.%20That%20is%20why%20this%20course%20stands%20on%20DuckDB%2C%20which%20stores%20what%20it%20is%20told%2C%20and%20names%20SQLite%20only%20as%20the%20fallback.%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%20SQLite's%20type%20affinity%2C%20and%20a%20column%20whose%20rows%20are%20all%20missing%0A%20%20%20%20_con%20%3D%20sqlite3.connect(%22%3Amemory%3A%22)%0A%20%20%20%20_con.execute(%22CREATE%20TABLE%20t%20(province_code%20INTEGER%2C%20row_void%20INTEGER%2C%20unit_sold_share%20INTEGER)%22)%0A%20%20%20%20_con.execute(%22INSERT%20INTO%20t%20VALUES%20('010'%2C%20NULL%2C%201)%2C%20('056'%2C%20NULL%2C%203)%22)%0A%20%20%20%20_plain%20%3D%20pd.read_sql(%22SELECT%20*%20FROM%20t%22%2C%20_con)%0A%20%20%20%20_nullable%20%3D%20pd.read_sql(%22SELECT%20*%20FROM%20t%22%2C%20_con%2C%20dtype_backend%3D%22numpy_nullable%22)%0A%20%20%20%20_con.close()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%60'010'%60%20and%20%60'056'%60%20written%20into%20%60INTEGER%60%20columns%2C%20with%20a%20void%20mark%20that%20is%20missing%20on%20every%20row%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.hstack(%5B_plain%2C%20pd.DataFrame(%7B%22plain%20read%22%3A%20_plain.dtypes.astype(str)%2C%20%22numpy_nullable%22%3A%20_nullable.dtypes.astype(str)%7D)%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%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**The%20tool.**%20Take%20the%20types%20from%20the%20**codebook**%2C%20not%20from%20the%20rows.%20%60data%2Fcodebook.csv%60%20gives%20every%20variable%20of%20every%20table%20its%20database%20type%20(%60INTEGER%60%2C%20%60DOUBLE%60%2C%20%60VARCHAR%60%2C%20%60DATE%60).%20Typed%20from%20the%20codebook%2C%20a%20column%20has%20the%20same%20type%20whatever%20rows%20a%20query%20returned%3A%20the%20same%20in%20a%20region%20where%20it%20is%20always%20answered%20and%20in%20one%20where%20it%20never%20is.%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%20codebook%3A%20one%20row%20per%20variable%20of%20each%20table%2C%20with%20its%20database%20type%0A%20%20%20%20codebook%20%3D%20pd.read_csv(PATHS.codebook)%0A%20%20%20%20codebook%5Bcodebook%5B%22table%22%5D%20%3D%3D%20%22household%22%5D%5B%5B%22table%22%2C%20%22variable%22%2C%20%22type%22%2C%20%22label%22%5D%5D%0A%20%20%20%20return%20(codebook%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.6.%20Types%20from%20the%20codebook%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20table%20whose%20types%20depend%20on%20its%20rows%20breaks%20the%20day%20a%20batch%20has%20no%20missing%20value%20in%20a%20column%2C%20or%20no%20value%20at%20all.%20Typed%20from%20the%20codebook%2C%20every%20region%20and%20every%20batch%20reads%20the%20same%2C%20whichever%20reader%20brought%20it%20in.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20pandas%20table%2C%20the%20codebook%20(the%20table%20above%2C%20every%20table's%20rows)%2C%20and%20the%20survey%20table's%20name%20%E2%86%92%20the%20same%20table%2C%20each%20column%20the%20codebook%20lists%20for%20that%20table%20typed%20by%20its%20codebook%20type%3A%0A%0A%20%20%20%20%7C%20codebook%20type%20%7C%20pandas%20type%20%7C%20why%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60INTEGER%60%20%7C%20a%20nullable%20integer%3A%20%60%22Int64%22%60%20%7C%20whole%2C%20and%20able%20to%20hold%20a%20missing%20value%2C%20missing%20or%20not%20%7C%0A%20%20%20%20%7C%20%60DOUBLE%60%20%7C%20decimals%3A%20%60%22Float64%22%60%20(or%20%60float64%60)%20%7C%20even%20when%20every%20value%20happens%20to%20be%20whole%20%7C%0A%20%20%20%20%7C%20%60VARCHAR%60%20%7C%20text%3A%20%60%22string%22%60%20%7C%20%7C%0A%20%20%20%20%7C%20%60DATE%60%20%7C%20as%20it%20comes%20%7C%20%7C%0A%0A%20%20%20%20Leave%20alone%20the%20area%20codes%20(%60AREA_CODE_WIDTH%60%3A%20Exercise%202.4%20types%20them)%20and%20any%20column%20the%20codebook%20does%20not%20list%20for%20that%20table.%20A%20column%20typed%20%60INTEGER%60%20whose%20values%20are%20all%20missing%20still%20becomes%20%60Int64%60.%0A%0A%20%20%20%20**Tools%3A**%20a%20filter%20on%20the%20codebook%20(%60codebook%5B%22table%22%5D%20%3D%3D%20name%60)%2C%20a%20dictionary%20from%20%60variable%60%20to%20%60type%60%2C%20a%20loop%20over%20the%20table's%20columns%2C%20and%20%60.astype(...)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20restore_types(table%3A%20pd.DataFrame%2C%20codebook%3A%20pd.DataFrame%2C%20name%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20table%20with%20each%20column%20the%20codebook%20lists%20for%20the%20survey%20table%20%60name%60%20typed%20by%20its%20codebook%20type%20(INTEGER%20as%0A%20%20%20%20Int64%2C%20DOUBLE%20as%20Float64%2C%20VARCHAR%20as%20string)%3B%20area%20codes%2C%20DATE%20columns%20and%20unlisted%20columns%20left%20as%20they%20come.%22%22%22%0A%20%20%20%20%23%20Exercise%202.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20codebook's%20rows%20for%20this%20table%2C%20as%20a%20dictionary%20from%20variable%20to%20type%0A%20%20%20%20%23%20Step%202%3A%20the%20pandas%20type%20for%20each%20codebook%20type%3A%20INTEGER%20-%3E%20%22Int64%22%2C%20DOUBLE%20-%3E%20%22Float64%22%2C%20VARCHAR%20-%3E%20%22string%22%0A%20%20%20%20%23%20Step%203%3A%20on%20a%20copy%20of%20the%20table%2C%20for%20each%20column%20that%20the%20dictionary%20has%2C%20that%20is%20not%20an%20area%20code%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20and%20whose%20type%20has%20a%20pandas%20type%3A%20.astype()%20it%3B%20return%20the%20copy%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.6%22)%0A%0A%0A%40app.function%0Adef%20selection_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20selection%20rows%20as%20a%20careless%20read%20gives%20them%2C%20for%20the%20check%20of%20Exercise%202.6%3A%20a%20design%20weight%20whose%20values%20are%0A%20%20%20%20all%20whole%2C%20an%20area%20number%20and%20an%20exclusion%20mark%20as%20decimals%2C%20a%20void%20mark%20missing%20on%20every%20row%20as%20Python%20objects%2C%20and%0A%20%20%20%20a%20column%20the%20codebook%20does%20not%20list.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22row_id%22%3A%20%5B1%2C%202%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5013%2C%205014%2C%205015%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_stratum%22%3A%20%5B%22F%22%2C%20%22A%22%2C%20%22C%22%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_ea%22%3A%20%5BNone%2C%201003.0%2C%203011.0%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_design_weight%22%3A%20%5B1%2C%203%2C%2015%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_final_weight%22%3A%20%5B1.03%2C%202.61%2C%2015.2%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_excluded%22%3A%20%5BNone%2C%201.0%2C%20None%5D%2C%0A%20%20%20%20%20%20%20%20%22row_void%22%3A%20pd.Series(%5BNone%2C%20None%2C%20None%5D%2C%20dtype%3Dobject)%2C%0A%20%20%20%20%20%20%20%20%22checked_by%22%3A%20%5B%22AB%22%2C%20%22AB%22%2C%20%22CD%22%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(codebook)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.6%22%2C%20restore_types%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20household%20table%20from%20its%20SQLite%20copy%22%2C%20(read_sqlite(%2201%22%2C%20%22household%22)%2C%20codebook%2C%20%22household%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20selection%20rows%20from%20its%20SQLite%20copy%22%2C%20(read_sqlite(%2204%22%2C%20%22selection%22)%2C%20codebook%2C%20%22selection%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20selection%20rows%3A%20whole%20weights%2C%20a%20mark%20as%20decimals%2C%20a%20mark%20missing%20on%20every%20row%2C%20an%20unlisted%20column%22%2C%20(selection_fixture()%2C%20codebook%2C%20%22selection%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20boats%20from%20its%20SQLite%20copy%22%2C%20(read_sqlite(%2201%22%2C%20%22boat%22)%2C%20codebook%2C%20%22boat%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%20column_kinds(table%2C%20nullable%3DTrue)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22INTEGER%20-%3E%20'Int64'%2C%20DOUBLE%20-%3E%20'Float64'%2C%20VARCHAR%20-%3E%20'string'%2C%20from%20the%20codebook's%20rows%20for%20this%20table%3B%20skip%20the%20area%20codes.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(codebook)%3A%0A%20%20%20%20%23%20Try%20it%3A%20region%2001's%20household%20table%20from%20SQLite%2C%20each%20column's%20type%20before%20and%20after%0A%20%20%20%20def%20_try_types()%3A%0A%20%20%20%20%20%20%20%20_read%20%3D%20read_sqlite(%2201%22%2C%20%22household%22)%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_typed%20%3D%20restore_types(_read%2C%20codebook%2C%20%22household%22)%0A%20%20%20%20%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20try_it(restore_types%2C%20_read%2C%20codebook%2C%20%22household%22)%20%20%23%20explains%20a%20%22not%20written%20yet%22%20or%20an%20error%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(%7B%22from%20pd.read_sql%22%3A%20_read.dtypes.astype(str)%2C%20%22restore_types%22%3A%20_typed.dtypes.astype(str)%7D).T%0A%0A%20%20%20%20_try_types()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.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*IntCastingNaNError%3A%20Cannot%20convert%20non-finite%20values%20(NA%20or%20inf)%20to%20integer*%20%7C%20%60%22int64%22%60%2C%20which%20has%20no%20missing%20value.%20%7C%20%60%22Int64%22%60%2C%20capital%20I%20%7C%0A%20%20%20%20%7C%20%60row_void%60%20is%20not%20whole%20numbers%20%7C%20Only%20the%20columns%20with%20values%20were%20typed%2C%20or%20the%20type%20was%20guessed%20from%20the%20values.%20%7C%20type%20every%20listed%20column%20from%20the%20codebook%20%7C%0A%20%20%20%20%7C%20%60sel_design_weight%60%20is%20whole%20numbers%20%7C%20The%20type%20was%20inferred%20from%20the%20values%20(1%2C%203%2C%2015).%20%7C%20%60DOUBLE%60%20is%20decimals%2C%20whatever%20the%20values%20%7C%0A%20%20%20%20%7C%20%60province_code%60%20is%20whole%20numbers%20%7C%20The%20area%20codes%20were%20typed%20%60INTEGER%60%20too.%20%7C%20%60column%20not%20in%20AREA_CODE_WIDTH%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'checked_by'*%20%7C%20A%20column%20the%20codebook%20does%20not%20list.%20%7C%20%60if%20column%20in%20types%60%20%7C%0A%20%20%20%20%7C%20A%20column%20typed%20by%20another%20table's%20row%20%7C%20The%20codebook%20was%20not%20filtered%20to%20this%20table.%20%7C%20%60codebook%5B%22table%22%5D%20%3D%3D%20name%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%204%20%C2%B7%20The%20flat%20table%20saved%20as%20Parquet%2C%20with%20a%20row-count%20check%0A%0A%20%20%20%20**The%20problem.**%20In%20R%2C%20the%20loop%20ends%20by%20joining%20the%20tables%20and%20saving%20the%20result%20for%20the%20tabulation%3A%20%60saveRDS(flat%2C%20%22flat.rds%22)%60.%20The%20**flat%20table**%20is%20one%20row%20per%20fishing%20unit%20still%20in%20the%20survey%2C%20each%20with%20its%20household's%20area%20codes%2C%20its%20stratum%20and%20area%2C%20and%20its%20final%20weight.%20Its%20**grain**%2C%20what%20one%20row%20is%2C%20is%20the%20fishing%20unit.%20Two%20things%20can%20go%20wrong%20without%20an%20error.%20A%20join%20can%20**multiply**%20rows%3A%20a%20unit%20matched%20to%20two%20households%20is%20counted%20twice%20in%20every%20total.%20A%20join%20can%20**drop**%20rows%3A%20a%20unit%20with%20no%20match%20falls%20out%20of%20an%20inner%20join.%20And%20nothing%20in%20%60saveRDS%60%20checks%20either.%0A%0A%20%20%20%20Household%20ids%20repeat%20across%20regions%2C%20so%20joining%20every%20region's%20units%20to%20every%20region's%20selection%20rows%20on%20%60hh_id%60%20alone%20matches%20a%20unit%20to%20the%20household%20of%20the%20same%20number%20in%20each%20other%20region%20too%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20Units%20and%20selection%20rows%20stacked%20across%20regions%20first%2C%20then%20joined%20on%20hh_id%20alone%2C%20and%20on%20region%20and%20id%0A%20%20%20%20_units%20%3D%20%22%20UNION%20ALL%20%22.join(f%22SELECT%20'%7B_db%5B-2%3A%5D%7D'%20AS%20region_code%2C%20hh_id%20FROM%20%7B_db%7D.fishing_unit%22%20for%20_db%20in%20DATABASES)%0A%20%20%20%20_selection%20%3D%20%22%20UNION%20ALL%20%22.join(f%22SELECT%20'%7B_db%5B-2%3A%5D%7D'%20AS%20region_code%2C%20hh_id%2C%20sel_final_weight%20FROM%20%7B_db%7D.selection%22%20for%20_db%20in%20DATABASES)%0A%0A%20%20%20%20def%20_rows(on%3A%20str)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20return%20survey_con.sql(f%22SELECT%20count(*)%20FROM%20(%7B_units%7D)%20AS%20u%20JOIN%20(%7B_selection%7D)%20AS%20s%20ON%20%7Bon%7D%22).fetchall()%5B0%5D%5B0%5D%0A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20units%20joined%20to%20their%20households'%20selection%20rows%20%7C%20rows%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20units%20in%20the%20three%20databases%20%7C%20%7Bsurvey_con.sql(f%22SELECT%20count(*)%20FROM%20(%7B_units%7D)%22).fetchall()%5B0%5D%5B0%5D%3A%2C%7D%20%7C%0A%20%20%20%20%7C%20joined%20on%20%60hh_id%60%20alone%20%7C%20**%7B_rows(%22u.hh_id%20%3D%20s.hh_id%22)%3A%2C%7D**%20%7C%0A%20%20%20%20%7C%20joined%20on%20%60region_code%60%20and%20%60hh_id%60%20%7C%20%7B_rows(%22u.region_code%20%3D%20s.region_code%20AND%20u.hh_id%20%3D%20s.hh_id%22)%3A%2C%7D%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.**%20Join%20inside%20each%20region's%20%60SELECT%60%2C%20where%20a%20household%20id%20is%20unique%2C%20as%20Exercise%202.3%20filtered%20there%3B%20then%20stack.%20A%20%60JOIN%20...%20ON%60%20puts%20side%20by%20side%20the%20rows%20of%20two%20tables%20whose%20condition%20is%20true%2C%20as%20dplyr's%20%60inner_join%60%3A%0A%0A%20%20%20%20%60%60%60sql%0A%20%20%20%20SELECT%20...%2C%20s.sel_final_weight%0A%20%20%20%20FROM%20sfh2025_r01.fishing_unit%20AS%20u%0A%20%20%20%20JOIN%20sfh2025_r01.household%20AS%20h%20ON%20h.hh_id%20%3D%20u.hh_id%0A%20%20%20%20JOIN%20sfh2025_r01.selection%20AS%20s%20ON%20s.hh_id%20%3D%20u.hh_id%0A%20%20%20%20WHERE%20...%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20removal%20marks%20can%20then%20be%20tested%20on%20the%20joined%20columns%20directly%20(%60h.hh_deleted_on%60%2C%20%60h.row_void%60%2C%20%60s.sel_excluded%60%2C%20%60u.row_void%60)%2C%20with%20no%20subquery.%20The%20area%20codes%20are%20padded%20in%20the%20same%20%60SELECT%60%20with%20%60lpad%60%2C%20so%20every%20region%20gives%20text%20of%20one%20width.%0A%0A%20%20%20%20**Then%20count.**%20The%20flat%20table%20must%20have%20exactly%20one%20row%20per%20unit%20still%20in%20the%20survey%3A%20as%20many%20rows%20as%20Exercise%202.3%20returns.%20That%20count%2C%20taken%20a%20second%20way%2C%20is%20the%20**row-count%20check**%3A%20if%20the%20join%20multiplied%20or%20dropped%20a%20row%2C%20the%20two%20counts%20differ.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.7.%20The%20flat%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20modules%205%20and%206%20tabulate%20and%20estimate%20from%20this%20table.%20A%20row%20too%20many%20is%20a%20unit%20counted%20twice%20in%20every%20weighted%20total%3B%20a%20code%20left%20unpadded%20is%20a%20province%20missing%20from%20every%20table%20by%20province.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20connection%20with%20regional%20databases%20attached%2C%20and%20a%20list%20of%20their%20names%20%E2%86%92%20a%20pandas%20table%20with%20**one%20row%20per%20fishing%20unit%20still%20in%20the%20survey**%20(Exercise%202.3's%20rows)%2C%20and%20these%20columns%2C%20the%20names%20in%20%60FLAT_COLUMNS%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20from%20%7C%20columns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20household%2C%20%60household%60%20%7C%20%7B%22%2C%20%22.join(f%22%60%7Bc%7D%60%22%20for%20c%20in%20AREA_CODE_WIDTH)%7D%3A%20text%2C%20zero-padded%20to%20%7B%22%2C%20%22.join(str(w)%20for%20w%20in%20AREA_CODE_WIDTH.values())%7D%20%7C%0A%20%20%20%20%7C%20the%20unit%2C%20%60fishing_unit%60%20%7C%20%7B%22%2C%20%22.join(f%22%60%7Bc%7D%60%22%20for%20c%20in%20UNIT_COLUMNS%5B%3A3%5D)%7D%20(its%20key%2C%20with%20the%20region)%2C%20then%20its%20answers%3A%20%7B%22%2C%20%22.join(f%22%60%7Bc%7D%60%22%20for%20c%20in%20UNIT_COLUMNS%5B3%3A%5D)%7D%20(%60UNIT_COLUMNS%60)%20%7C%0A%20%20%20%20%7C%20the%20household's%20row%20in%20%60selection%60%20%7C%20%7B%22%2C%20%22.join(f%22%60%7Bc%7D%60%22%20for%20c%20in%20DESIGN_COLUMNS)%7D%20(%60DESIGN_COLUMNS%60)%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%7C%20region_code%20%7C%20province_code%20%7C%20citymun_code%20%7C%20barangay_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20%E2%80%A6%20%7C%20sel_final_weight%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%20033%20%7C%2010%20%7C%20008%20%7C%20%E2%80%A6%20%7C%201%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20The%20check%20compares%20rows%20in%20any%20order.%0A%0A%20%20%20%20**Tools%3A**%20one%20%60SELECT%60%20per%20region%20with%20%60JOIN%20...%20ON%60%2C%20the%20marks%20of%20Exercise%202.3%20on%20the%20joined%20tables%2C%20%60lpad(CAST(...%20AS%20VARCHAR)%2C%20width%2C%20'0')%60%2C%20and%20%60UNION%20ALL%20BY%20NAME%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20flat_table(con%3A%20duckdb.DuckDBPyConnection%2C%20databases%3A%20list%5Bstr%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20fishing%20unit%20still%20in%20the%20survey%2C%20every%20listed%20region%3A%20the%20household's%20area%20codes%20as%20padded%20text%2C%20the%0A%20%20%20%20unit's%20key%20and%20answers%2C%20and%20its%20household's%20stratum%2C%20area%20and%20final%20weight%20(FLAT_COLUMNS).%22%22%22%0A%20%20%20%20%23%20Exercise%202.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20for%20each%20database%2C%20a%20SELECT%20FROM%20its%20fishing_unit%20AS%20u%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20JOIN%20its%20household%20AS%20h%20ON%20the%20household%20id%2C%20JOIN%20its%20selection%20AS%20s%20ON%20the%20household%20id%0A%20%20%20%20%23%20Step%202%3A%20in%20it%2C%20the%20four%20area%20codes%20from%20h%2C%20each%20lpad(CAST(...%20AS%20VARCHAR)%2C%20width%2C%20'0')%20AS%20its%20own%20name%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20then%20UNIT_COLUMNS%20from%20u%20and%20DESIGN_COLUMNS%20from%20s%0A%20%20%20%20%23%20Step%203%3A%20WHERE%20the%20household%20is%20not%20deleted%20or%20voided%20(h.hh_deleted_on%2C%20h.row_void)%2C%20not%20excluded%20(s.sel_excluded)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20and%20the%20unit%20not%20voided%20(u.row_void)%0A%20%20%20%20%23%20Step%204%3A%20join%20the%20SELECTs%20with%20%22%20UNION%20ALL%20BY%20NAME%20%22%2C%20run%2C%20and%20return%20a%20pandas%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.7%22)%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%202.7%22%2C%20flat_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(%22the%20three%20regions%22%2C%20(attach_regions()%2C%20DATABASES))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20small%20regions%3A%20codes%20as%20numbers%20and%20as%20text%2C%20household%20ids%20in%20both%2C%20removed%20households%20and%20rows%22%2C%20(two_regions()%2C%20%5B%22sfh2025_r03%22%2C%20%22sfh2025_r13%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004%20alone%2C%20codes%20as%20text%22%2C%20(attach_regions()%2C%20%5B%22sfh2025_r04%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22regions%2001%20and%2007%2C%20codes%20as%20numbers%22%2C%20(attach_regions()%2C%20%5B%22sfh2025_r01%22%2C%20%22sfh2025_r07%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20databases%22%2C%20summarize%3Das_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Join%20each%20region's%20units%20to%20its%20own%20household%20and%20selection%20tables%20inside%20its%20SELECT%3B%20lpad%20the%20codes%3B%20IS%20NULL%20and%20IS%20DISTINCT%20FROM%201%20for%20the%20marks.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20flat%20table%20of%20the%20three%20regions%0A%20%20%20%20try_it(flat_table%2C%20survey_con%2C%20DATABASES)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.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%20Many%20more%20rows%20than%20units%20%7C%20The%20regions%20were%20stacked%20first%20and%20joined%20on%20%60hh_id%60%20alone.%20%7C%20the%20joins%20inside%20each%20region's%20%60SELECT%60%20%7C%0A%20%20%20%20%7C%20%60province_code%60%20reads%20%6033%60%20or%20%6012%60%20%7C%20The%20codes%20of%20regions%2001%20and%2007%20were%20not%20padded.%20%7C%20%60lpad(CAST(h.province_code%20AS%20VARCHAR)%2C%203%2C%20'0')%60%20%7C%0A%20%20%20%20%7C%20No%20rows%20%7C%20A%20mark%20tested%20with%20%60%3C%3E%201%60%2C%20or%20the%20date%20with%20%60%3D%20NULL%60.%20%7C%20%60IS%20DISTINCT%20FROM%201%60%2C%20%60IS%20NULL%60%20%7C%0A%20%20%20%20%7C%20Removed%20households%20still%20there%20%7C%20One%20of%20the%20four%20marks%20is%20not%20tested.%20%7C%20%60h.hh_deleted_on%60%2C%20%60h.row_void%60%2C%20%60s.sel_excluded%60%2C%20%60u.row_void%60%20%7C%0A%20%20%20%20%7C%20*BinderException%3A%20Ambiguous%20reference%20to%20column%20name*%20%7C%20A%20column%20two%20joined%20tables%20share%20(%60hh_id%60%2C%20%60row_void%60)%2C%20named%20without%20its%20table.%20%7C%20%60u.hh_id%60%2C%20%60h.row_void%60%20%7C%0A%20%20%20%20%7C%20A%20column%20missing%20or%20misnamed%20%7C%20The%20%60SELECT%60%20list%20differs%20from%20%60FLAT_COLUMNS%60.%20%7C%20%60AS%20province_code%60%20after%20each%20%60lpad%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%20Saving%20it%3A%20Parquet%0A%0A%20%20%20%20**Parquet**%20is%20a%20file%20format%20for%20tables%20that%20stores%20each%20column%20with%20its%20type%3A%20text%20stays%20text%20with%20its%20zeros%2C%20a%20nullable%20integer%20stays%20whole%20with%20its%20missing%20values%2C%20a%20weight%20stays%20a%20decimal.%20pandas%2C%20polars%2C%20DuckDB%20and%20R%20(%60arrow%3A%3Aread_parquet%60)%20all%20read%20it.%20An%20RDS%20file%20is%20read%20by%20R%20alone%2C%20and%20a%20CSV%2C%20as%20section%203%20showed%2C%20by%20everyone%2C%20with%20every%20type%20to%20guess%20again.%0A%0A%20%20%20%20%7C%20write%20%7C%20read%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20pandas%3A%20%60flat.to_parquet(path%2C%20index%3DFalse)%60%20%7C%20%60pd.read_parquet(path)%60%20%7C%0A%20%20%20%20%7C%20polars%3A%20%60flat.write_parquet(path)%60%20%7C%20%60pl.read_parquet(path)%60%2C%20or%20%60pl.scan_parquet(path)%60%20to%20read%20only%20what%20a%20query%20needs%20%7C%0A%20%20%20%20%7C%20DuckDB%3A%20%60con.sql(query).write_parquet(path)%60%20%7C%20%60SELECT%20*%20FROM%20'flat.parquet'%60%20%7C%0A%20%20%20%20%7C%20R%3A%20%60arrow%3A%3Awrite_parquet(flat%2C%20path)%60%20%7C%20%60arrow%3A%3Aread_parquet(path)%60%20%7C%0A%0A%20%20%20%20A%20Parquet%20file%20also%20records%20how%20many%20rows%20it%20holds%2C%20in%20its%20**footer**%2C%20the%20part%20at%20the%20end%20that%20describes%20the%20file.%20%60pl.scan_parquet(path).select(pl.len()).collect().item()%60%20reads%20that%20count%20without%20reading%20a%20single%20row.%0A%0A%20%20%20%20**The%20check.**%20Count%20before%20you%20write%20and%20after%20you%20read%20back%3A%20the%20table%20must%20have%20the%20rows%20you%20expected%2C%20and%20the%20file%20the%20rows%20you%20wrote.%20A%20save%20that%20refuses%20to%20write%20a%20table%20of%20the%20wrong%20size%20stops%20the%20next%20module%20from%20tabulating%20a%20broken%20file.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%202.8.%20Save%20the%20flat%20table%2C%20checked%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20flat%20table%20is%20handed%20to%20the%20tabulation%2C%20maybe%20on%20another%20day%20or%20to%20another%20person.%20A%20file%20that%20says%20it%20holds%20the%20survey's%20fishing%20units%20must%20hold%20exactly%20them%3B%20a%20refusal%20now%20costs%20a%20minute%2C%20a%20wrong%20file%20found%20in%20the%20published%20tables%20costs%20a%20correction.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%2C%20a%20path%20ending%20in%20%60.parquet%60%2C%20and%20the%20number%20of%20rows%20it%20should%20have%20%E2%86%92%20the%20number%20of%20rows%20in%20the%20written%20file%2C%20**read%20back%20from%20the%20file**.%20If%20the%20table%20does%20not%20have%20the%20expected%20number%20of%20rows%2C%20write%20nothing%20and%20raise%20a%20%60ValueError%60%20with%20exactly%20this%20message%20(numbers%20with%20thousands%20separators)%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20the%20flat%20table%20has%201%2C240%20rows%2C%20but%201%2C234%20were%20expected%3A%20nothing%20was%20written%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%7C%20table's%20rows%20%7C%20expected%20%7C%20result%20%7C%0A%20%20%20%20%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%201%2C234%20%7C%201%2C234%20%7C%20the%20file%20is%20written%3B%20%601234%60%2C%20read%20back%20%7C%0A%20%20%20%20%7C%201%2C240%20%7C%201%2C234%20%7C%20*ValueError*%2C%20as%20above%3B%20no%20file%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60len(table)%60%2C%20%60raise%20ValueError(f%22...%22)%60%20with%20%60%7Bn%3A%2C%7D%60%20for%20the%20separators%2C%20%60to_parquet(path%2C%20index%3DFalse)%60%2C%20and%20%60pl.scan_parquet(path).select(pl.len()).collect().item()%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20save_flat(table%3A%20pd.DataFrame%2C%20path%3A%20Path%2C%20expected%3A%20int)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Write%20the%20table%20to%20Parquet%20only%20if%20it%20has%20the%20expected%20number%20of%20rows%3B%20return%20the%20rows%20read%20back%20from%20the%20file.%22%22%22%0A%20%20%20%20%23%20Exercise%202.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20if%20the%20table's%20rows%20are%20not%20the%20expected%20number%2C%20raise%20ValueError%20with%20the%20message%20in%20the%20text%0A%20%20%20%20%23%20Step%202%3A%20write%20the%20table%20to%20the%20path%20as%20Parquet%2C%20without%20the%20index%0A%20%20%20%20%23%20Step%203%3A%20read%20the%20number%20of%20rows%20back%20from%20the%20file%2C%20and%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%202.8%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_three%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%2C%20%2207%22%5D%2C%20%22hh_id%22%3A%20%5B5013%2C%205205%2C%205089%5D%2C%20%22sel_final_weight%22%3A%20%5B1.03%2C%204.82%2C%2015.2%5D%7D)%0A%20%20%20%20_flat%20%3D%20course.units()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%202.8%22%2C%20save_flat%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20rows%2C%20three%20expected%22%2C%20(_three%2C%20SCRATCH%20%2F%20%22check_three.parquet%22%2C%203))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20rows%2C%20four%20expected%22%2C%20(_three%2C%20SCRATCH%20%2F%20%22check_refused.parquet%22%2C%204))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20units%2C%20as%20many%20as%20expected%22%2C%20(_flat%2C%20SCRATCH%20%2F%20%22check_units.parquet%22%2C%20len(_flat)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20units%2C%201%2C000%20fewer%20expected%22%2C%20(_flat%2C%20SCRATCH%20%2F%20%22check_units_refused.parquet%22%2C%20len(_flat)%20-%201000))%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%20raises%3DTrue%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Raise%20ValueError%20with%20the%20message%20in%20the%20text%20when%20len(table)%20is%20not%20expected%3B%20otherwise%20write%20with%20index%3DFalse%20and%20count%20the%20rows%20in%20the%20file.%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%20three%20rows%20where%20four%20are%20expected%3B%20the%20error%20should%20name%20both%20counts%0A%20%20%20%20try_it(save_flat%2C%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%2C%20%2207%22%5D%7D)%2C%20SCRATCH%20%2F%20%22try_refused.parquet%22%2C%204)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%202.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%60ValueError%3A%20the%20flat%20table%20has%203%20rows%2C%20but%204%20were%20expected...%60%20is%20red%20%7C%20The%20message%20differs%20by%20a%20word%2C%20a%20comma%20or%20a%20space.%20%7C%20copy%20the%20message%20from%20the%20text%2C%20with%20%60%7Bn%3A%2C%7D%60%20%7C%0A%20%20%20%20%7C%20%601240%20rows%60%20without%20a%20comma%20%7C%20The%20numbers%20were%20written%20without%20their%20separators.%20%7C%20%60%7Blen(table)%3A%2C%7D%60%20%7C%0A%20%20%20%20%7C%20A%20number%20where%20a%20*ValueError*%20was%20expected%20%7C%20The%20count%20is%20not%20checked%2C%20or%20checked%20after%20writing.%20%7C%20%60if%20len(table)%20!%3D%20expected%3A%20raise%20...%60%20first%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'LazyFrame'%20object%20has%20no%20attribute%20'item'*%20%7C%20The%20query%20was%20not%20run.%20%7C%20%60.collect().item()%60%20%7C%0A%20%20%20%20%7C%20One%20more%20column%20when%20the%20file%20is%20read%20back%20%7C%20The%20index%20was%20written.%20%7C%20%60index%3DFalse%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%20flat%20table%2C%20end%20to%20end%0A%0A%20%20%20%20The%20cell%20below%20runs%20your%20Exercises%202.7%20and%202.8%20on%20the%20three%20regions%2C%20with%20Exercise%202.3's%20count%20as%20the%20number%20expected%2C%20and%20prints%20a%20**timing%20line**%3A%20how%20long%20the%20query%2C%20the%20save%20and%20the%20read-back%20took.%20Time%20is%20worth%20a%20line%20in%20a%20pipeline's%20output%3A%20a%20step%20that%20took%20one%20second%20last%20round%20and%20a%20minute%20this%20round%20has%20changed%2C%20even%20if%20nothing%20failed.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey_con)%3A%0A%20%20%20%20%23%20The%20flat%20table%20of%20the%20three%20regions%2C%20saved%20as%20Parquet%2C%20its%20rows%20counted%20twice%2C%20and%20the%20time%20it%20took%0A%20%20%20%20_start%20%3D%20time.perf_counter()%0A%20%20%20%20_flat%20%3D%20attempt(flat_table%2C%20survey_con%2C%20DATABASES)%0A%20%20%20%20_live%20%3D%20attempt(live_units%2C%20survey_con%2C%20DATABASES)%0A%20%20%20%20mo.stop(_flat%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.7%22))%0A%20%20%20%20mo.stop(_live%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.3%22))%0A%20%20%20%20_queried%20%3D%20time.perf_counter()%0A%20%20%20%20_flat_path%20%3D%20SCRATCH%20%2F%20%22flat_units.parquet%22%0A%20%20%20%20_written%20%3D%20attempt(save_flat%2C%20_flat%2C%20_flat_path%2C%20len(_live))%0A%20%20%20%20mo.stop(_written%20is%20NOT_YET%2C%20waiting_for(%22Exercise%202.8%22))%0A%20%20%20%20_done%20%3D%20time.perf_counter()%0A%20%20%20%20_back%20%3D%20pd.read_parquet(_flat_path)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20count%20%7C%20rows%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20fishing%20units%20still%20in%20the%20survey%20(Exercise%202.3)%20%7C%20%7Blen(_live)%3A%2C%7D%20%7C%0A%20%20%20%20%7C%20the%20flat%20table%20(Exercise%202.7)%20%7C%20%7Blen(_flat)%3A%2C%7D%20%7C%0A%20%20%20%20%7C%20the%20Parquet%20file%2C%20read%20back%20(Exercise%202.8)%20%7C%20%7B_written%3A%2C%7D%20%7C%0A%20%20%20%20%7C%20%60course.units()%60%2C%20the%20helper%20later%20modules%20use%20%7C%20%7Blen(course.units())%3A%2C%7D%20%7C%0A%0A%20%20%20%20**Timing%3A**%20query%20%7B(_queried%20-%20_start)%20*%201000%3A%2C.0f%7D%20ms%2C%20save%20and%20read-back%20%7B(_done%20-%20_queried)%20*%201000%3A%2C.0f%7D%20ms.%20File%3A%20%60%7B_flat_path.name%7D%60%2C%20%7B_flat_path.stat().st_size%20%2F%201024%3A%2C.0f%7D%20KB%2C%20in%20this%20notebook's%20scratch%20folder.%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20types%20it%20comes%20back%20with%2C%20read%20with%20%60pd.read_parquet%60%20and%20nothing%20else%3A%20codes%20still%20text%2C%20weights%20still%20decimals.%22)%2C%0A%20%20%20%20%20%20%20%20_back.dtypes.astype(str).to_frame(%22type%22).T%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%20Your%20own%20work%20writes%20the%20file%20where%20the%20next%20step%20reads%20it%2C%20in%20a%20project%20folder%2C%20rather%20than%20in%20a%20scratch%20folder%20that%20disappears.%20The%20file%20name%20can%20carry%20the%20survey%20and%20the%20date%20of%20the%20run%20(%60sfh2025_flat_units_2026-06-01.parquet%60)%2C%20so%20a%20later%20module%20never%20reads%20last%20month's%20table%20by%20mistake.%0A%0A%20%20%20%20%23%23%205%20%C2%B7%20The%20real%20thing%3A%20reading%20a%20MySQL%20server%20with%20DuckDB's%20mysql%20extension%0A%0A%20%20%20%20**The%20problem.**%20Many%20surveys%20keep%20their%20regional%20databases%20on%20a%20**database%20server**%20such%20as%20MySQL%3A%20a%20program%20on%20another%20machine%20that%20holds%20the%20databases%20and%20answers%20queries%20over%20the%20network%2C%20from%20anyone%20with%20an%20account.%20In%20R%20you%20open%20each%20one%20with%20%60dbConnect(RMySQL%3A%3AMySQL()%2C%20host%20%3D%20...%2C%20user%20%3D%20...%2C%20password%20%3D%20...%2C%20dbname%20%3D%20...)%60%2C%20inside%20the%20loop.%20The%20DuckDB%20files%20of%20this%20course%20stand%20in%20for%20such%20a%20server%3B%20this%20section%20reads%20a%20server%20itself.%0A%0A%20%20%20%20**The%20tool.**%20DuckDB's%20**mysql%20extension**%20attaches%20a%20MySQL%20database%20the%20way%20section%202%20attached%20a%20file.%20An%20**extension**%20is%20an%20add-on%20DuckDB%20downloads%20and%20loads%20on%20request.%20Every%20query%20of%20sections%202%20to%204%20then%20runs%20unchanged%2C%20because%20the%20databases%20have%20the%20same%20names%3A%0A%0A%20%20%20%20%60%60%60sql%0A%20%20%20%20INSTALL%20mysql%3B%20%20%20--%20once%20per%20computer%3A%20downloads%20the%20extension%0A%20%20%20%20LOAD%20mysql%3B%20%20%20%20%20%20--%20once%20per%20connection%0A%20%20%20%20CREATE%20SECRET%20survey%20(TYPE%20mysql%2C%20HOST%20'localhost'%2C%20PORT%203306%2C%20USER%20'learner'%2C%20PASSWORD%20'...')%3B%0A%20%20%20%20ATTACH%20'database%3Dsfh2025_r01'%20AS%20sfh2025_r01%20(TYPE%20mysql%2C%20SECRET%20survey%2C%20READ_ONLY)%3B%0A%20%20%20%20ATTACH%20'database%3Dsfh2025_r04'%20AS%20sfh2025_r04%20(TYPE%20mysql%2C%20SECRET%20survey%2C%20READ_ONLY)%3B%0A%20%20%20%20ATTACH%20'database%3Dsfh2025_r07'%20AS%20sfh2025_r07%20(TYPE%20mysql%2C%20SECRET%20survey%2C%20READ_ONLY)%3B%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20No%20Python%20driver%20is%20needed%3A%20the%20extension%20talks%20to%20MySQL%20itself.%0A%0A%20%20%20%20%7C%20piece%20%7C%20what%20it%20does%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60INSTALL%20mysql%60%20%7C%20downloads%20the%20extension%20from%20DuckDB's%20site%20into%20your%20home%20folder%20(%60.duckdb%2Fextensions%2F%60)%2C%20once.%20The%20extension%20must%20match%20DuckDB's%20version%20(this%20course%20pins%20duckdb%202.0.0).%20In%20a%20room%20with%20no%20internet%2C%20the%20trainer%20hands%20out%20the%20file%20for%202.0.0%20and%20your%20system%20(%60mysql_scanner.duckdb_extension.gz%60%2C%20from%20DuckDB's%20extension%20site)%3B%20decompressed%2C%20%60INSTALL%20'path%2Fto%2Fmysql_scanner.duckdb_extension'%60%20installs%20it%20from%20there%20%7C%0A%20%20%20%20%7C%20%60CREATE%20SECRET%60%20%7C%20keeps%20the%20server's%20address%2C%20user%20and%20password%20in%20the%20connection's%20memory%2C%20under%20a%20name.%20The%20secret%20lives%20as%20long%20as%20the%20connection%20(it%20is%20**temporary**%3A%20nothing%20is%20written%20to%20disk)%20%7C%0A%20%20%20%20%7C%20%60ATTACH%20'database%3Dsfh2025_r01'%20...%20(TYPE%20mysql%2C%20SECRET%20survey%2C%20READ_ONLY)%60%20%7C%20one%20MySQL%20database%2C%20under%20a%20name%2C%20through%20the%20secret.%20%60READ_ONLY%60%20refuses%20any%20change%20from%20this%20connection%2C%20whatever%20the%20account%20may%20do%20%7C%0A%0A%20%20%20%20**Why%20a%20secret%2C%20and%20not%20the%20password%20in%20the%20%60ATTACH%60%20text.**%20%60ATTACH%20'host%3Dlocalhost%20user%3Dlearner%20password%3D...%20database%3Dsfh2025_r01'%60%20works%20too.%20When%20it%20fails%2C%20though%2C%20DuckDB's%20error%20repeats%20the%20text%20it%20was%20given%2C%20password%20included%3A%20*IO%20Error%3A%20Failed%20to%20connect%20to%20MySQL%20database%20with%20parameters%20%22host%3D...%20password%3D...%20database%3Dsfh2025_r01%22*.%20Errors%20end%20up%20in%20notebooks%2C%20logs%20and%20messages%20to%20colleagues.%20With%20a%20secret%2C%20the%20same%20error%20shows%20only%20%60%22database%3Dsfh2025_r01%22%60.%20A%20notebook%20you%20share%20should%20not%20hold%20a%20real%20password%20either%3A%20read%20it%20from%20an%20environment%20variable%20(%60os.environ%5B%22SURVEY_PASSWORD%22%5D%60)%20or%20ask%20for%20it%2C%20as%20the%20form%20below%20does.%0A%0A%20%20%20%20**Each%20query%20goes%20to%20the%20server.**%20An%20attached%20MySQL%20table%20is%20read%20over%20the%20network%20every%20time%20a%20query%20uses%20it.%20For%20work%20that%20reads%20the%20same%20tables%20many%20times%2C%20copy%20them%20once%20into%20a%20DuckDB%20file%20(%60CREATE%20TABLE%20copy01.household%20AS%20SELECT%20*%20FROM%20sfh2025_r01.household%60%20with%20a%20file%20attached%20as%20%60copy01%60)%2C%20and%20work%20from%20the%20copy.%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%20Connect%2C%20if%20a%20server%20answers%0A%0A%20%20%20%20The%20cells%20below%20connect%20only%20when%20you%20press%20**Connect**.%20You%20need%20a%20MySQL%20server%20with%20the%20survey%20loaded%3A%20in%20a%20training%2C%20the%20trainer's%20room%20server.%20On%20your%20own%2C%20you%20have%20none%2C%20and%20that%20is%20fine%3A%20the%20code%20is%20the%20same%20for%20any%20MySQL%20server%20you%20may%20have%20at%20work.%20The%20defaults%20are%20port%203306%20and%20the%20read-only%20user%20%60learner%60%2C%20with%20the%20host%20set%20to%20%60localhost%60%3B%20in%20a%20training%2C%20replace%20%60localhost%60%20with%20the%20room%20server's%20address%2C%20which%20the%20trainer%20gives%20you.%20Without%20a%20server%2C%20read%20on%3A%20the%20cell%20shows%20what%20it%20would%20run%2C%20and%20nothing%20in%20the%20rest%20of%20the%20course%20needs%20one.%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%20server's%20address%20and%20account%3B%20nothing%20connects%20until%20you%20press%20Connect%0A%20%20%20%20server_form%20%3D%20mo.ui.dictionary(%7B%0A%20%20%20%20%20%20%20%20%22host%22%3A%20mo.ui.text(value%3D%22localhost%22%2C%20label%3D%22host%22)%2C%0A%20%20%20%20%20%20%20%20%22port%22%3A%20mo.ui.number(value%3D3306%2C%20start%3D1%2C%20stop%3D65535%2C%20label%3D%22port%22)%2C%0A%20%20%20%20%20%20%20%20%22user%22%3A%20mo.ui.text(value%3D%22learner%22%2C%20label%3D%22user%22)%2C%0A%20%20%20%20%20%20%20%20%22password%22%3A%20mo.ui.text(value%3D%22learner%22%2C%20kind%3D%22password%22%2C%20label%3D%22password%22)%2C%0A%20%20%20%20%7D).form(submit_button_label%3D%22Connect%22%2C%20bordered%3DTrue)%0A%20%20%20%20server_form%0A%20%20%20%20return%20(server_form%2C)%0A%0A%0A%40app.function%0Adef%20connect_server(host%3A%20str%2C%20port%3A%20int%2C%20user%3A%20str%2C%20password%3A%20str%2C%20databases%3A%20list%5Bstr%5D%20%3D%20DATABASES)%20-%3E%20duckdb.DuckDBPyConnection%3A%0A%20%20%20%20%22%22%22A%20connection%20with%20each%20listed%20MySQL%20database%20attached%2C%20read-only%2C%20under%20its%20own%20name%2C%20through%20a%20secret.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.execute(%22INSTALL%20mysql%22)%0A%20%20%20%20con.execute(%22LOAD%20mysql%22)%0A%20%20%20%20con.execute(%22CREATE%20SECRET%20survey%20(TYPE%20mysql%2C%20HOST%20%3F%2C%20PORT%20%3F%2C%20USER%20%3F%2C%20PASSWORD%20%3F)%22%2C%20%5Bhost%2C%20int(port)%2C%20user%2C%20password%5D)%0A%20%20%20%20for%20db%20in%20databases%3A%0A%20%20%20%20%20%20%20%20con.execute(f%22ATTACH%20'database%3D%7Bdb%7D'%20AS%20%7Bdb%7D%20(TYPE%20mysql%2C%20SECRET%20survey%2C%20READ_ONLY)%22)%0A%20%20%20%20return%20con%0A%0A%0A%40app.function%0Adef%20compare_counts(server%3A%20duckdb.DuckDBPyConnection%2C%20files%3A%20duckdb.DuckDBPyConnection%2C%20databases%3A%20list%5Bstr%5D%20%3D%20DATABASES)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20table's%20rows%20on%20the%20server%20and%20in%20the%20course's%20DuckDB%20files%2C%20region%20by%20region.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20db%20in%20databases%3A%0A%20%20%20%20%20%20%20%20for%20(name%2C)%20in%20files.sql(f%22SELECT%20table_name%20FROM%20duckdb_tables()%20WHERE%20database_name%20%3D%20'%7Bdb%7D'%20ORDER%20BY%20table_name%22).fetchall()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20on_server%20%3D%20server.sql(f'SELECT%20count(*)%20FROM%20%7Bdb%7D.%22%7Bname%7D%22').fetchall()%5B0%5D%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20in_file%20%3D%20files.sql(f'SELECT%20count(*)%20FROM%20%7Bdb%7D.%22%7Bname%7D%22').fetchall()%5B0%5D%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22database%22%3A%20db%2C%20%22table%22%3A%20name%2C%20%22on%20the%20server%22%3A%20on_server%2C%20%22in%20the%20file%22%3A%20in_file%2C%20%22same%22%3A%20on_server%20%3D%3D%20in_file%7D)%0A%20%20%20%20return%20pd.DataFrame(rows)%0A%0A%0A%40app.cell%0Adef%20_(server_form%2C%20survey_con)%3A%0A%20%20%20%20%23%20Connect%20when%20the%20form%20is%20sent%3A%20attach%20the%20three%20regions%2C%20count%20every%20table%20on%20the%20server%20and%20in%20the%20files%2C%0A%20%20%20%20%23%20and%20run%20your%20Exercise%202.7%20on%20the%20server%0A%20%20%20%20if%20server_form.value%20is%20None%3A%0A%20%20%20%20%20%20%20%20_sql%20%3D%20%22%5Cn%22.join(%5B%22INSTALL%20mysql%3B%22%2C%20%22LOAD%20mysql%3B%22%2C%20%22CREATE%20SECRET%20survey%20(TYPE%20mysql%2C%20HOST%20'localhost'%2C%20PORT%203306%2C%20USER%20'learner'%2C%20PASSWORD%20'...')%3B%22%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*(f%22ATTACH%20'database%3D%7B_db%7D'%20AS%20%7B_db%7D%20(TYPE%20mysql%2C%20SECRET%20survey%2C%20READ_ONLY)%3B%22%20for%20_db%20in%20DATABASES)%5D)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(%5Bmo.callout(mo.md(%22**Not%20connected.**%20Press%20**Connect**%20above%20when%20a%20server%20is%20running.%20This%20is%20what%20the%20cell%20will%20run%20first%2C%20then%20a%20count%20of%20every%20table%3A%22)%2C%20kind%3D%22neutral%22)%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%20mo.md(f%22%60%60%60sql%5Cn%7B_sql%7D%5Cn%60%60%60%22)%5D)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_form%20%3D%20server_form.value%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_start%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20%20%20%20%20_server%20%3D%20connect_server(_form%5B%22host%22%5D%2C%20_form%5B%22port%22%5D%2C%20_form%5B%22user%22%5D%2C%20_form%5B%22password%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20_counts%20%3D%20compare_counts(_server%2C%20survey_con)%0A%20%20%20%20%20%20%20%20%20%20%20%20_parts%20%3D%20%5Bmo.md(f%22**Connected**%20to%20%60%7B_form%5B'host'%5D%7D%3A%7Bint(_form%5B'port'%5D)%7D%60%20as%20%60%7B_form%5B'user'%5D%7D%60%2C%20in%20%7B(time.perf_counter()%20-%20_start)%20*%201000%3A%2C.0f%7D%20ms%20with%20the%20counts.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22Every%20table%20has%20the%20same%20rows%20on%20the%20server%20as%20in%20the%20course's%20files.%22%20if%20_counts%5B%22same%22%5D.all()%20else%20%22**Some%20tables%20differ**%20from%20the%20course's%20files%3A%20the%20server%20holds%20another%20load%20of%20the%20survey.%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_counts%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20%20%20%20%20_flat_server%20%3D%20attempt(flat_table%2C%20_server%2C%20DATABASES)%0A%20%20%20%20%20%20%20%20%20%20%20%20_t1%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_flat_server%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_parts.append(waiting_for(%22Exercise%202.7%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_flat_files%20%3D%20flat_table(survey_con%2C%20DATABASES)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_t2%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_parts.append(mo.md(f%22Your%20%60flat_table%60%2C%20unchanged%2C%20on%20the%20server%3A%20**%7Blen(_flat_server)%3A%2C%7D%20rows**%20in%20%7B(_t1%20-%20_t0)%20*%201000%3A%2C.0f%7D%20ms%3B%20on%20the%20files%3A%20%7Blen(_flat_files)%3A%2C%7D%20rows%20in%20%7B(_t2%20-%20_t1)%20*%201000%3A%2C.0f%7D%20ms.%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20_out%20%3D%20mo.vstack(_parts)%0A%20%20%20%20%20%20%20%20except%20duckdb.Error%20as%20_error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_last%20%3D%20str(_error).strip().splitlines()%5B-1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22**No%20connection.**%20The%20error's%20last%20line%3A%5Cn%5Cn%60%60%60%5Cn%7Btype(_error).__name__%7D%3A%20%7B_last%7D%5Cn%60%60%60%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22*Can't%20connect%20to%20server*%3A%20nothing%20answers%20at%20that%20address%20and%20port%3B%20start%20the%20server%2C%20or%20check%20the%20address.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22*Access%20denied*%3A%20the%20user%20or%20password.%20*Failed%20to%20download%20extension*%3A%20no%20internet%3B%20install%20the%20extension%20from%20its%20file.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20counts%20are%20the%20module's%20check%2C%20as%20a%20pipeline%20would%20run%20it%3A%20the%20server%20and%20the%20files%20must%20hold%20the%20same%20survey%20before%20anything%20built%20on%20one%20is%20compared%20with%20the%20other.%20Your%20%60flat_table%60%20ran%20on%20the%20server%20without%20a%20line%20changed%3A%20the%20regions%20have%20the%20same%20names%2C%20and%20the%20SQL%20is%20the%20same.%20That%20is%20the%20reason%20to%20write%20the%20regional%20loop%20as%20a%20query%20over%20attached%20databases%20rather%20than%20as%20a%20loop%20over%20connections%3A%20the%20place%20the%20data%20lives%20changes%2C%20and%20the%20code%20does%20not.%0A%0A%20%20%20%20Whichever%20way%20a%20table%20reaches%20you%20(a%20DuckDB%20file%2C%20a%20MySQL%20server%2C%20the%20CSV%20export%2C%20a%20SQLite%20copy)%2C%20the%20checks%20of%20this%20module%20say%20whether%20it%20is%20the%20same%20survey%3A%20the%20same%20rows%20per%20table%2C%20the%20codes%20whole%2C%20the%20types%20the%20codebook%20gives.%0A%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%20open%20a%20regional%20database%20%7C%20%60duckdb.connect(path%2C%20read_only%3DTrue)%60%3B%20%60.df()%60%2C%20%60.pl()%60%2C%20%60.fetchall()%60%20%7C%20names%20in%20double%20quotes%3B%20values%20as%20%60params%3D%60%20%7C%0A%20%20%20%20%7C%20keep%20the%20households%20still%20in%20the%20survey%20%7C%20%60hh_deleted_on%20IS%20NULL%20AND%20row_void%20IS%20DISTINCT%20FROM%201%60%20%7C%20a%20blank%20mark%20is%20%60NULL%60%3A%20%60%3C%3E%201%60%20and%20%60%3D%20NULL%60%20drop%20it%20%7C%0A%20%20%20%20%7C%20read%20every%20region%20in%20one%20query%20%7C%20%60ATTACH%20...%20AS%20sfh2025_r01%20(READ_ONLY)%60%2C%20%60UNION%20ALL%20BY%20NAME%60%20%7C%20the%20region%20on%20every%20row%3B%20filter%20and%20join%20inside%20each%20region's%20%60SELECT%60%20%7C%0A%20%20%20%20%7C%20keep%20codes%20whole%20%7C%20%60lpad%60%20in%20SQL%2C%20%60.astype(%22Int64%22).astype(%22string%22).str.zfill()%60%20in%20pandas%20%7C%20the%20widths%20in%20%60AREA_CODE_WIDTH%60%3B%20%60Int64%60%20before%20text%20%7C%0A%20%20%20%20%7C%20read%20the%20CSV%20export%20%7C%20%60encoding%3D%22cp1252%22%60%2C%20%60na_values%3D%5Br%22%5CN%22%5D%60%2C%20%60keep_default_na%3DFalse%60%2C%20%60dtype%3D%60%2C%20%60dtype_backend%3D%22numpy_nullable%22%60%20%7C%20codes%20as%20text%3B%20%60.str.rstrip()%60%20the%20padding%20%7C%0A%20%20%20%20%7C%20keep%20whole%20numbers%20whole%20%7C%20the%20codebook's%20types%3A%20%60INTEGER%60%20as%20%60Int64%60%20%7C%20types%20that%20do%20not%20depend%20on%20the%20rows%20%7C%0A%20%20%20%20%7C%20build%20and%20save%20the%20flat%20table%20%7C%20joins%20inside%20each%20region%2C%20%60to_parquet%60%2C%20%60pl.scan_parquet(...).select(pl.len())%60%20%7C%20rows%20in%20%3D%20rows%20out%2C%20counted%20twice%20%7C%0A%20%20%20%20%7C%20read%20a%20MySQL%20server%20%7C%20DuckDB's%20mysql%20extension%2C%20%60CREATE%20SECRET%60%2C%20%60ATTACH%20...%20(TYPE%20mysql%2C%20READ_ONLY)%60%20%7C%20the%20same%20queries%2C%20the%20same%20counts%20%7C%0A%0A%20%20%20%20Next%2C%20module%203%20(%5B%6020_key_matching.py%60%5D(%2F%3Ffile%3D20_key_matching.py))%20matches%20the%20fishing%20units%20to%20their%20catch%2C%20boats%20and%20gears%3A%20orphan%20rows%20both%20ways%2C%20keys%20of%20several%20parts%2C%20and%20which%20part%20of%20a%20key%20is%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(%222.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
b96bb7b4f934eeaeff84699129f9fe7f