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%22Debugging%20drill%3A%20the%20report%20that%20goes%20wrong%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey's%20tables%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20PATHS%2C%20attempt%2C%20check_cases%2C%20try_it%0A%0A%20%20%20%20%23%20The%20households%20as%20read%2C%20before%20their%20duplicates%20are%20dropped%3A%206%20households%20are%20in%20it%20twice%0A%20%20%20%20households%20%3D%20course.households(clean%3DFalse)%0A%20%20%20%20%23%20The%20plots%20as%20read%20(production%20as%20numbers)%2C%20their%204%20repeated%20rows%20dropped%3B%20each%20area%20in%20the%20unit%20area_unit%20gives%0A%20%20%20%20plots%20%3D%20course.plots(clean%3DFalse).drop_duplicates()%0A%20%20%20%20%23%20The%20interview%20dates%20as%20the%20file%20writes%20them%2C%20as%20text%2C%20one%20per%20household%0A%20%20%20%20interview_dates%20%3D%20pd.read_csv(PATHS.survey%20%2F%20%22households.csv%22).drop_duplicates()%5B%22interview_date%22%5D%0A%20%20%20%20%23%20The%20rice%20plots%2C%20cleaned%20as%20module%205%20cleans%20them%3A%20each%20with%20its%20yield%20and%20its%20household's%20province%20and%20weight%0A%20%20%20%20rice%20%3D%20(%0A%20%20%20%20%20%20%20%20course.plots()%0A%20%20%20%20%20%20%20%20.query(%22crop%20%3D%3D%20'Rice'%22)%0A%20%20%20%20%20%20%20%20.merge(course.households()%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%20%20%20%20.assign(yield_t_ha%3Dlambda%20t%3A%20t%5B%22production_kg%22%5D%20%2F%201000%20%2F%20t%5B%22area_ha%22%5D)%0A%20%20%20%20%20%20%20%20%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22province%22%2C%20%22weight%22%2C%20%22yield_t_ha%22%5D%5D%0A%20%20%20%20)%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%20Debugging%20drill%3A%20the%20report%20that%20goes%20wrong%0A%0A%20%20%20%20The%20extension%20office%20asked%20for%20five%20figures%20from%20the%202025%20farm%20survey%2C%20and%20someone%20wrote%20a%20function%20for%20each.%20None%20of%20them%20stops%20with%20an%20error%2C%20and%20every%20one%20of%20them%20gives%20a%20wrong%20figure.%20Each%20mistake%20comes%20from%20modules%204%20and%205%3A%20the%20kind%20that%20turns%20up%20when%20a%20real%20survey%20meets%20code%20that%20looks%20right.%0A%0A%20%20%20%20Work%20through%20it%20as%20you%20did%20Debugging%20drill%20D1%3A%20**predict**%20what%20a%20function%20gives%2C%20**run**%20it%2C%20**trace**%20the%20line%20that%20makes%20the%20difference%2C%20**fix**%20it%20and%20watch%20its%20check%20turn%20green%2C%20and%20**say**%20what%20the%20mistake%20was%2C%20in%20one%20sentence.%20Each%20check%20starts%20red.%20The%20hints%20say%20what%20you%20see%2C%20never%20what%20to%20change.%0A%0A%20%20%20%20The%20figures%20were%20worked%20out%20once%20with%20code%20known%20to%20be%20right%2C%20so%20you%20know%20what%20the%20report%20should%20say%3A%0A%0A%20%20%20%20%7C%20figure%20%7C%20what%20it%20should%20say%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201.%20Rice%20yield%2C%20all%20farm%20households%20(weighted)%20%7C%204.50%20t%2Fha%20%7C%0A%20%20%20%20%7C%202.%20Rice%20harvest%2C%20all%20farm%20households%20(weighted)%20%7C%203%2C752%2C516%20t%20%7C%0A%20%20%20%20%7C%203.%20Harvest%20of%20the%20survey's%20plots%2C%20by%20crop%20%7C%20Banana%202%2C627.40%20t%2C%20Corn%202%2C044.36%20t%2C%20Rice%203%2C840.65%20t%2C%20Vegetables%201%2C046.39%20t%2C%20crop%20not%20recorded%2064.79%20t%3A%209%2C623.59%20t%20in%20all%20%7C%0A%20%20%20%20%7C%204.%20Interviews%20by%20month%20%7C%20October%202025%3A%20381%2C%20November%202025%3A%20384%2C%20December%202025%3A%20425%20%7C%0A%20%20%20%20%7C%205.%20Rice%20yield%2C%20the%20average%20plot%20(unweighted)%20%7C%204.35%20t%2Fha%20%7C%0A%0A%20%20%20%20The%20whole%20report%2C%20as%20the%20functions%20give%20it%2C%20is%20at%20the%20end%20of%20the%20notebook.%20Watch%20it%20come%20right%20as%20you%20fix%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20The%20report%20that%20goes%20wrong%3A%20five%20bugs%20to%20find%20and%20fix%0A%0A%20%20%20%20The%20setup%20cell%20at%20the%20top%20reads%20what%20the%20report%20works%20from%3A%0A%0A%20%20%20%20%7C%20name%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60households%60%20%7C%20%60households.csv%60%20as%20read%2C%20%60psgc%60%20as%20text%20and%20dates%20parsed%2C%20**before**%20its%20duplicates%20are%20dropped%3A%206%20households%20are%20in%20it%20twice%20%7C%0A%20%20%20%20%7C%20%60plots%60%20%7C%20%60plots.csv%60%20as%20read%2C%20production%20as%20numbers%2C%20its%204%20repeated%20plots%20dropped%3B%20each%20%60area%60%20in%20the%20unit%20its%20%60area_unit%60%20gives%2C%20hectares%20or%20square%20meters%20%7C%0A%20%20%20%20%7C%20%60interview_dates%60%20%7C%20the%20interview%20dates%20as%20the%20file%20writes%20them%2C%20as%20text%2C%20one%20per%20household%20%7C%0A%20%20%20%20%7C%20%60rice%60%20%7C%20the%20rice%20plots%2C%20cleaned%20as%20module%205%20cleans%20them%2C%20each%20with%20its%20%60yield_t_ha%60%20(tonnes%20per%20hectare)%2C%20its%20household's%20%60province%60%20and%20its%20%60weight%60%20%7C%0A%0A%20%20%20%20A%20household's%20**weight**%20is%20the%20number%20of%20farm%20households%20it%20stands%20for%20(%5Bmodule%204%2C%20section%203%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%3B%20a%20plot%20carries%20its%20household's%20weight.%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%20Drill%20D2.1%20%C2%B7%20A%20mean%20of%20means%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20headline%20of%20a%20survey%20report%20is%20a%20figure%20for%20everyone%20the%20survey%20stands%20for.%20A%20figure%20that%20gives%20a%20small%20province%20the%20same%20say%20as%20a%20large%20one%20describes%20no%20one.%0A%0A%20%20%20%20The%20report%20has%20a%20table%20of%20each%20province's%20weighted%20rice%20yield%2C%20made%20by%20%60province_yields(rice)%60%2C%20below%3B%20that%20table%20is%20right.%20Under%20it%2C%20%60weighted_yield(rice)%60%20should%20give%20the%20headline%3A%20the%20weighted%20average%20yield%20of%20**all**%20the%20rice%20plots%20of%20the%20seven%20provinces%2C%20each%20plot's%20yield%20counted%20as%20many%20times%20as%20its%20weight%20says.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60rice%60%20table%20%E2%86%92%20the%20weighted%20average%20of%20%60yield_t_ha%60%2C%20weighted%20by%20%60weight%60%2C%20in%20tonnes%20per%20hectare%2C%20to%20two%20decimal%20places.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60weighted_yield(rice)%60%20%7C%20%604.5%60%20(the%20report's%204.50%20t%2Fha)%20%7C%0A%0A%20%20%20%20Tools%3A%20weighted%20means%20(%5Bmodule%204%2C%20section%203%5D(%2F%3Ffile%3D30_summarize_and_combine.py)).%0A%0A%20%20%20%20**Predict.**%20The%20function%20averages%20the%20province%20table.%20Nueva%20Ecija%20has%20the%20highest%20yield%20and%20by%20far%20the%20largest%20weights.%20Will%20its%20answer%20be%20above%20or%20below%20the%20right%20one%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20province_yields(rice%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20province's%20weighted%20average%20rice%20yield%2C%20in%20tonnes%20per%20hectare%3A%20the%20report's%20table.%20It%20is%20right.%22%22%22%0A%20%20%20%20weighted%20%3D%20rice.assign(weighted%3Drice%5B%22yield_t_ha%22%5D%20*%20rice%5B%22weight%22%5D).groupby(%22province%22)%5B%5B%22weighted%22%2C%20%22weight%22%5D%5D.sum()%0A%20%20%20%20return%20(weighted%5B%22weighted%22%5D%20%2F%20weighted%5B%22weight%22%5D).rename(%22yield_t_ha%22)%0A%0A%0A%40app.function%0Adef%20weighted_yield(rice%3A%20pd.DataFrame)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20weighted%20average%20rice%20yield%20of%20all%20the%20plots%2C%20in%20tonnes%20per%20hectare%2C%20to%20two%20decimal%20places.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.1%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20by_province%20%3D%20province_yields(rice)%0A%20%20%20%20return%20round(by_province.mean()%2C%202)%0A%0A%0A%40app.function%0Adef%20drill_rice()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20rice%20plots%20in%20two%20provinces%20of%20very%20different%20weights%2C%20for%20the%20check%20of%20Drill%20D2.1.%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%5B%22HH-0001%22%2C%20%22HH-0002%22%2C%20%22HH-0003%22%2C%20%22HH-0004%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%201%2C%201%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Nueva%20Ecija%22%2C%20%22Nueva%20Ecija%22%2C%20%22Nueva%20Ecija%22%2C%20%22Ilocos%20Norte%22%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B100.0%2C%20100.0%2C%20100.0%2C%2010.0%5D%2C%0A%20%20%20%20%20%20%20%20%22yield_t_ha%22%3A%20%5B5.0%2C%205.0%2C%205.0%2C%203.0%5D%2C%0A%20%20%20%20%7D)%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%22Drill%20D2.1%22%2C%20weighted_yield%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20rice%20plots%22%2C%20(rice%2C))%2C%20(%22four%20plots%3A%20three%20of%20weight%20100%20in%20one%20province%2C%20one%20of%20weight%2010%20in%20another%22%2C%20(drill_rice()%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20three%20plots%20of%20one%20province%22%2C%20(drill_rice().head(3)%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20hint%3D%22With%20one%20province%2C%20the%20answer%20is%20right.%20With%20two%20of%20very%20different%20weights%2C%20what%20does%20averaging%20their%20yields%20leave%20out%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20report's%20headline%20yield.%0A%20%20%20%20try_it(weighted_yield%2C%20rice)%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**Trace.**%20The%20function%20averages%20the%20seven%20figures%20of%20the%20province%20table.%20Look%20at%20that%20table%20beside%20how%20much%20weight%20each%20province%20carries%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%20Trace%3A%20each%20province's%20weighted%20yield%2C%20and%20the%20total%20weight%20of%20its%20rice%20plots%0A%20%20%20%20pd.DataFrame(%7B%22yield_t_ha%22%3A%20province_yields(rice)%2C%20%22weight%22%3A%20rice.groupby(%22province%22)%5B%22weight%22%5D.sum()%7D).sort_values(%22weight%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%20Nueva%20Ecija's%20plots%20stand%20for%20seven%20times%20as%20many%20farm%20households%20as%20Ilocos%20Norte's%2C%20yet%20%60.mean()%60%20of%20the%20seven%20yields%20counts%20each%20province%20once.%20That%20is%20a%20**mean%20of%20means**.%20The%20weighted%20mean%20of%20module%204%2C%20section%203%20is%20taken%20over%20the%20plots%20themselves.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.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%604.25%60%20%7C%20The%20mean%20of%20the%20seven%20province%20yields%3A%20each%20province%20counts%20once.%20%7C%20the%20weighted%20mean%20over%20the%20plots%20%7C%0A%20%20%20%20%7C%20%604.35%60%20%7C%20The%20plain%20mean%20of%20the%20plots'%20yields%3A%20the%20weights%20are%20left%20out.%20%7C%20multiply%20by%20%60weight%60%2C%20divide%20by%20its%20sum%20%7C%0A%20%20%20%20%7C%20A%20number%20far%20above%205%20%7C%20The%20weighted%20sum%20was%20divided%20by%20the%20number%20of%20plots%2C%20not%20by%20the%20sum%20of%20the%20weights.%20%7C%20%60rice%5B%22weight%22%5D.sum()%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%20Drill%20D2.2%20%C2%B7%20A%20join%20that%20multiplies%20rows%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20weighted%20total%20multiplies%20every%20plot%20by%20its%20household's%20weight.%20A%20plot%20counted%20twice%20adds%20its%20harvest%20twice%2C%20and%20nobody%20reading%20the%20total%20can%20tell.%0A%0A%20%20%20%20%60rice_harvest_t(plots%2C%20households)%60%20should%20give%20the%20estimated%20rice%20harvest%20of%20all%20the%20farm%20households%20the%20survey%20stands%20for%3A%20each%20rice%20plot's%20%60production_kg%60%20times%20its%20household's%20%60weight%60%2C%20added%20up%2C%20in%20tonnes.%20The%20weights%20come%20from%20%60households%60%20with%20a%20join%20on%20%60hh_id%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60plots%60%20and%20%60households%60%20tables%20%E2%86%92%20the%20weighted%20total%20rice%20harvest%2C%20in%20whole%20tonnes.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60rice_harvest_t(plots%2C%20households)%60%20%7C%20%603752516%60%20%7C%0A%0A%20%20%20%20Tools%3A%20joins%20and%20%60validate%3D%60%20(%5Bmodule%204%2C%20section%205%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%3B%20duplicates%20(%5Bmodule%205%2C%20section%205%5D(%2F%3Ffile%3D40_cleaning_and_time.py)).%0A%0A%20%20%20%20**Predict.**%20There%20are%201%2C034%20rice%20plots.%20How%20many%20rows%20will%20the%20join%20give%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20rice_harvest_t(plots%3A%20pd.DataFrame%2C%20households%3A%20pd.DataFrame)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22The%20estimated%20rice%20harvest%20of%20all%20the%20farm%20households%20the%20survey%20stands%20for%2C%20in%20tonnes.%22%22%22%0A%20%20%20%20rice_plots%20%3D%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20%23%20Drill%20D2.2%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20joined%20%3D%20rice_plots.merge(households%5B%5B%22hh_id%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22)%0A%20%20%20%20return%20round((joined%5B%22production_kg%22%5D%20*%20joined%5B%22weight%22%5D).sum()%20%2F%201000)%0A%0A%0A%40app.function%0Adef%20drill_join()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Three%20rice%20plots%20and%20a%20corn%20plot%20of%20two%20households%2C%20one%20of%20which%20is%20in%20the%20households%20table%20twice.%22%22%22%0A%20%20%20%20small_plots%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0001%22%2C%20%22HH-0002%22%2C%20%22HH-0002%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%2C%20%22Corn%22%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B1000%2C%20500%2C%202000%2C%20800%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20small_households%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0001%22%2C%20%22HH-0002%22%5D%2C%20%22weight%22%3A%20%5B10.0%2C%2010.0%2C%2020.0%5D%7D)%0A%20%20%20%20return%20small_plots%2C%20small_households%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%22Drill%20D2.2%22%2C%20rice_harvest_t%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20plots%20and%20households%22%2C%20(plots%2C%20households))%2C%20(%22two%20households%2C%20HH-0001%20in%20the%20households%20table%20twice%22%2C%20drill_join())%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20with%20HH-0001%20in%20it%20once%22%2C%20(drill_join()%5B0%5D%2C%20drill_join()%5B1%5D.drop_duplicates()))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20hint%3D%22Count%20the%20rows%20before%20and%20after%20the%20join.%20Which%20households%20match%20more%20than%20one%20row%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20report's%20rice%20harvest.%0A%20%20%20%20try_it(rice_harvest_t%2C%20plots%2C%20households)%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**Trace.**%20A%20weighted%20total%20that%20is%20a%20little%20too%20big.%20Count%20the%20rows%20on%20each%20side%20of%20the%20join%2C%20and%20after%20it%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%20Trace%3A%20rows%20before%20and%20after%20the%20join%2C%20and%20households%20whose%20hh_id%20is%20in%20the%20table%20more%20than%20once%0A%20%20%20%20_rice_plots%20%3D%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20_joined%20%3D%20_rice_plots.merge(households%5B%5B%22hh_id%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22)%0A%20%20%20%20%7B%22rice%20plots%22%3A%20len(_rice_plots)%2C%20%22rows%20after%20the%20join%22%3A%20len(_joined)%2C%0A%20%20%20%20%20%22households%20in%20twice%22%3A%20households.loc%5Bhouseholds%5B%22hh_id%22%5D.duplicated()%2C%20%22hh_id%22%5D.tolist()%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%20Three%20rows%20more%20after%20the%20join%20than%20before%3A%20three%20rice%20plots%20belong%20to%20households%20that%20are%20in%20%60households%60%20twice%2C%20and%20each%20matched%20both%20copies.%20A%20key%20that%20should%20be%20unique%20on%20one%20side%20is%20not%2C%20so%20the%20join%20is%20**many-to-many**.%20Module%204%2C%20section%205%20has%20the%20argument%20that%20makes%20a%20join%20say%20so%3B%20module%205%2C%20section%205%20how%20to%20keep%20one%20row%20per%20household.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20A%20total%20a%20little%20too%20big%20%7C%20Households%20in%20the%20table%20twice%3A%20their%20plots%20are%20joined%2C%20and%20counted%2C%20twice.%20%7C%20%60drop_duplicates()%60%20before%20the%20join%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20right%20dataset%3B%20not%20a%20many-to-one%20merge*%20%7C%20%60validate%3D%60%20found%20the%20households%20in%20twice%2C%20as%20it%20should.%20Now%20remove%20them.%20%7C%20%60drop_duplicates()%60%20on%20the%20weights%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'weight'*%20%7C%20Only%20%60hh_id%60%20was%20kept%20from%20%60households%60%2C%20or%20the%20weights%20were%20renamed%20on%20the%20way.%20%7C%20%60households%5B%5B%22hh_id%22%2C%20%22weight%22%5D%5D%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D2.3%20%C2%B7%20Plots%20that%20drop%20out%20of%20the%20totals%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20table%20of%20totals%20by%20group%20should%20add%20up%20to%20the%20grand%20total.%20When%20it%20doesn't%2C%20a%20reader%20either%20loses%20trust%20in%20the%20table%20or%2C%20worse%2C%20doesn't%20notice.%0A%0A%20%20%20%20The%20enumerators%20left%20%60crop%60%20blank%20for%2014%20plots.%20%60harvest_by_crop(plots)%60%20should%20give%20the%20harvest%20of%20the%20survey's%20plots%20by%20crop%2C%20in%20tonnes%2C%20with%20the%20plots%20whose%20crop%20was%20not%20recorded%20in%20a%20row%20of%20their%20own%2C%20its%20crop%20missing%20(%60NaN%60)%2C%20so%20the%20rows%20add%20up%20to%20every%20plot's%20harvest.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60plots%60%20table%20%E2%86%92%20one%20value%20per%20crop%2C%20in%20tonnes%2C%20plus%20one%20for%20the%20missing%20crop.%0A%0A%20%20%20%20%7C%20crop%20%7C%20harvest%20(t)%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20Banana%20%7C%202%2C627.40%20%7C%0A%20%20%20%20%7C%20Corn%20%7C%202%2C044.36%20%7C%0A%20%20%20%20%7C%20Rice%20%7C%203%2C840.65%20%7C%0A%20%20%20%20%7C%20Vegetables%20%7C%201%2C046.39%20%7C%0A%20%20%20%20%7C%20NaN%20%7C%2064.79%20%7C%0A%0A%20%20%20%20Tools%3A%20grouping%20and%20missing%20keys%20(%5Bmodule%204%2C%20section%201%5D(%2F%3Ffile%3D30_summarize_and_combine.py)).%0A%0A%20%20%20%20**Predict.**%20How%20many%20rows%20will%20the%20function%20give%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20harvest_by_crop(plots%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20plots'%20harvest%20by%20crop%2C%20in%20tonnes%2C%20with%20the%20plots%20whose%20crop%20was%20not%20recorded%20in%20a%20row%20of%20their%20own.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.3%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20return%20plots.groupby(%22crop%22)%5B%22production_kg%22%5D.sum()%20%2F%201000%0A%0A%0A%40app.function%0Adef%20drill_crops()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20plots%2C%20one%20of%20them%20with%20no%20crop%20recorded%2C%20for%20the%20check%20of%20Drill%20D2.3.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0001%22%2C%20%22HH-0002%22%2C%20%22HH-0003%22%5D%2C%20%22crop%22%3A%20%5B%22Rice%22%2C%20None%2C%20%22Rice%22%2C%20%22Corn%22%5D%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%22production_kg%22%3A%20%5B1640%2C%20900%2C%202000%2C%201500%5D%7D)%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%22Drill%20D2.3%22%2C%20harvest_by_crop%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20plots%22%2C%20(plots%2C))%2C%20(%22four%20plots%2C%20one%20with%20no%20crop%22%2C%20(drill_crops()%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20three%20plots%20with%20a%20crop%22%2C%20(drill_crops().dropna()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20hint%3D%22Add%20up%20the%20rows%20the%20function%20gives%2C%20and%20compare%20with%20the%20harvest%20of%20every%20plot.%20Which%20plots%20are%20in%20no%20row%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20report's%20harvest%20by%20crop.%0A%20%20%20%20try_it(harvest_by_crop%2C%20plots)%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**Trace.**%20Four%20crops%2C%20and%20the%20plots%20without%20one%20are%20nowhere.%20Add%20up%20the%20table's%20rows%2C%20and%20compare%20with%20the%20harvest%20of%20all%20the%20plots%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%20Trace%3A%20the%20table's%20rows%20added%20up%2C%20every%20plot's%20harvest%2C%20and%20the%20plots%20with%20no%20crop%0A%20%20%20%20%7B%22rows%20added%20up%20(t)%22%3A%20attempt(harvest_by_crop%2C%20plots).sum()%2C%20%22every%20plot%20(t)%22%3A%20plots%5B%22production_kg%22%5D.sum()%20%2F%201000%2C%0A%20%20%20%20%20%22plots%20with%20no%20crop%22%3A%20int(plots%5B%22crop%22%5D.isna().sum())%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%2064.79%20t%20short%3A%20the%20harvest%20of%20the%2014%20plots%20with%20no%20crop.%20A%20row's%20key%20decides%20its%20group%2C%20and%20a%20row%20whose%20key%20is%20missing%20belongs%20to%20no%20group%2C%20so%20%60groupby%60%20leaves%20it%20out%20unless%20it%20is%20told%20to%20keep%20it%20(module%204%2C%20section%201).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.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%20Four%20rows%2C%20no%20%60NaN%60%20%7C%20%60groupby%60%20dropped%20the%20plots%20with%20no%20crop.%20%7C%20%60groupby%60's%20%60dropna%3D%60%20%7C%0A%20%20%20%20%7C%20A%20row%20named%20%60not%20recorded%60%20%7C%20The%20blanks%20were%20filled%20with%20a%20label.%20The%20task%20keeps%20the%20missing%20key%20as%20%60NaN%60.%20%7C%20%60dropna%3DFalse%60%20instead%20of%20%60fillna%60%20%7C%0A%20%20%20%20%7C%20Values%20in%20kilograms%20%7C%20The%20division%20by%201%2C000%20was%20lost.%20%7C%20%60%2F%201000%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%20Drill%20D2.4%20%C2%B7%20Dates%20read%20month-first%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20survey's%20fieldwork%20ran%20from%20October%20to%20December%202025.%20A%20table%20that%20shows%20interviews%20in%20March%20describes%20fieldwork%20that%20never%20happened.%0A%0A%20%20%20%20The%20file%20writes%20each%20interview%20date%20day%20first%3A%20%6019%2F12%2F2025%60%20is%2019%20December%202025%2C%20as%20the%20codebook%20says.%20%60interviews_by_month(dates)%60%20should%20turn%20the%20texts%20into%20dates%20and%20count%20the%20interviews%20in%20each%20month.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20interview%20dates%20as%20text%20%E2%86%92%20the%20number%20of%20interviews%20in%20each%20month%2C%20months%20in%20order.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60interviews_by_month(interview_dates).to_dict()%60%20%7C%20%60%7BPeriod('2025-10'%2C%20'M')%3A%20381%2C%20Period('2025-11'%2C%20'M')%3A%20384%2C%20Period('2025-12'%2C%20'M')%3A%20425%7D%60%20%7C%0A%0A%20%20%20%20Tools%3A%20parsing%20day-first%20dates%2C%20and%20periods%20(%5Bmodule%205%2C%20section%203%5D(%2F%3Ffile%3D40_cleaning_and_time.py)).%0A%0A%20%20%20%20**Predict.**%20%6009%2F11%2F2025%60%20and%20%6019%2F12%2F2025%60%20are%20two%20of%20the%20dates.%20Which%20month%20will%20the%20function%20put%20each%20one%20in%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20interviews_by_month(dates%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22How%20many%20interviews%20in%20each%20month%2C%20from%20the%20dates%20as%20the%20file%20writes%20them.%22%22%22%0A%20%20%20%20%23%20Drill%20D2.4%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20parsed%20%3D%20pd.to_datetime(dates%2C%20format%3D%22%25m%2F%25d%2F%25Y%22%2C%20errors%3D%22coerce%22)%0A%20%20%20%20return%20parsed.dt.to_period(%22M%22).value_counts().sort_index()%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%22Drill%20D2.4%22%2C%20interviews_by_month%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20interview%20dates%22%2C%20(interview_dates%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%2219%2F12%2F2025%2C%2009%2F11%2F2025%2C%2001%2F10%2F2025%20and%2012%2F10%2F2025%22%2C%20(pd.Series(%5B%2219%2F12%2F2025%22%2C%20%2209%2F11%2F2025%22%2C%20%2201%2F10%2F2025%22%2C%20%2212%2F10%2F2025%22%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%2213%2F10%2F2025%20alone%22%2C%20(pd.Series(%5B%2213%2F10%2F2025%22%5D)%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20dates%22%2C%20hint%3D%22Which%20months%20does%20the%20function%20find%2C%20and%20how%20many%20interviews%20does%20it%20count%20in%20all%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20report's%20interviews%20by%20month.%0A%20%20%20%20try_it(interviews_by_month%2C%20interview_dates)%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**Trace.**%20Interviews%20from%20January%20to%20December%2C%20and%20far%20fewer%20than%201%2C190%20of%20them.%20Type%20a%20date%20as%20the%20file%20writes%20it%2C%20and%20see%20how%20each%20order%20of%20day%20and%20month%20reads%20it%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%20date_box%20%3D%20mo.ui.text(value%3D%2209%2F11%2F2025%22%2C%20label%3D%22A%20date%20from%20the%20file%3A%22)%0A%20%20%20%20date_box%0A%20%20%20%20return%20(date_box%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(date_box)%3A%0A%20%20%20%20def%20_read(order)%3A%0A%20%20%20%20%20%20%20%20when%20%3D%20pd.to_datetime(pd.Series(%5Bdate_box.value.strip()%5D)%2C%20format%3Dorder%2C%20errors%3D%22coerce%22).iloc%5B0%5D%0A%20%20%20%20%20%20%20%20return%20%22**cannot%20be%20read**%20(%60NaT%60%2C%20then%20left%20out%20of%20the%20count)%22%20if%20pd.isna(when)%20else%20f%22**%7Bwhen%3A%25d%20%25B%20%25Y%7D**%22%0A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20read%20as%20%7C%20%60%7Bdate_box.value.strip()%7D%60%20becomes%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20month%20first%2C%20%60%25m%2F%25d%2F%25Y%60%20%7C%20%7B_read(%22%25m%2F%25d%2F%25Y%22)%7D%20%7C%0A%20%20%20%20%7C%20day%20first%2C%20%60%25d%2F%25m%2F%25Y%60%20%7C%20%7B_read(%22%25d%2F%25m%2F%25Y%22)%7D%20%7C%0A%0A%20%20%20%20Try%20%6019%2F12%2F2025%60%2C%20then%20%6013%2F10%2F2025%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Read%20month%20first%2C%20a%20date%20whose%20first%20number%20is%2013%20or%20more%20cannot%20be%20read%20at%20all%2C%20and%20%60errors%3D%22coerce%22%60%20turns%20it%20into%20%60NaT%60%20without%20a%20word%2C%20so%20it%20is%20never%20counted.%20A%20date%20whose%20first%20number%20is%2012%20or%20less%20is%20read%20without%20complaint%2C%20as%20the%20wrong%20day%3A%209%20November%20becomes%2011%20September.%20Module%205%2C%20section%203%20reads%20the%20dates%20in%20the%20order%20the%20file%20writes%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.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%20Months%20from%20January%20to%20December%20%7C%20The%20dates%20are%20read%20month%20first.%20%7C%20%60format%3D%60%20%7C%0A%20%20%20%20%7C%20Still%20wrong%20with%20%60dayfirst%3DTrue%60%20%7C%20A%20%60format%3D%60%20is%20given%20too%2C%20and%20the%20format%20wins.%20%7C%20the%20order%20of%20%60%25d%60%20and%20%60%25m%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20time%20data%20%22%E2%80%A6%22%20doesn't%20match%20format*%20%7C%20%60errors%3D%22coerce%22%60%20was%20removed%20while%20the%20format%20is%20still%20wrong.%20The%20error%20names%20a%20date%20the%20format%20cannot%20read.%20%7C%20%60format%3D%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%20Drill%20D2.5%20%C2%B7%20Square%20meters%20mixed%20with%20hectares%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20yield%20in%20tonnes%20per%20hectare%20needs%20every%20area%20in%20hectares.%20A%20few%20areas%20in%20another%20unit%20move%20the%20average%20without%20making%20it%20look%20strange.%0A%0A%20%20%20%20%60mean_yield(plots%2C%20crop)%60%20should%20give%20the%20average%20yield%20of%20one%20crop's%20plots%2C%20unweighted%3A%20each%20plot's%20harvest%20in%20tonnes%20divided%20by%20its%20area%20in%20hectares%2C%20then%20the%20mean%20of%20those%20yields%2C%20to%20two%20decimal%20places.%20In%20%60plots%60%2C%20151%20areas%20are%20in%20square%20meters%20(%60area_unit%60%20is%20%60%22sqm%22%60)%3B%2010%2C000%20square%20meters%20make%20a%20hectare.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60plots%60%20table%20and%20a%20crop%20%E2%86%92%20the%20mean%20yield%20of%20that%20crop's%20plots%2C%20in%20tonnes%20per%20hectare.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60mean_yield(plots%2C%20%22Rice%22)%60%20%7C%20%604.35%60%20%7C%0A%0A%20%20%20%20Tools%3A%20one%20unit%20for%20a%20measure%20(%5Bmodule%205%2C%20section%202%5D(%2F%3Ffile%3D40_cleaning_and_time.py)).%0A%0A%20%20%20%20**Predict.**%20Run%20**Try%20it**.%20Does%20its%20answer%20look%20like%20a%20rice%20yield%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20mean_yield(plots%3A%20pd.DataFrame%2C%20crop%3A%20str)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20average%20yield%20of%20one%20crop's%20plots%2C%20in%20tonnes%20per%20hectare%2C%20to%20two%20decimal%20places.%22%22%22%0A%20%20%20%20crop_plots%20%3D%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20crop%5D%0A%20%20%20%20%23%20Drill%20D2.5%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20area_ha%20%3D%20crop_plots%5B%22area%22%5D%0A%20%20%20%20return%20round((crop_plots%5B%22production_kg%22%5D%20%2F%201000%20%2F%20area_ha).mean()%2C%202)%0A%0A%0A%40app.function%0Adef%20drill_units()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Two%20rice%20plots%20with%20the%20same%20yield%2C%20one%20measured%20in%20hectares%20and%20one%20in%20square%20meters.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0002%22%5D%2C%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Rice%22%5D%2C%20%22area%22%3A%20%5B1.0%2C%205000.0%5D%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%22area_unit%22%3A%20%5B%22ha%22%2C%20%22sqm%22%5D%2C%20%22production_kg%22%3A%20%5B4000%2C%202000%5D%7D)%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%22Drill%20D2.5%22%2C%20mean_yield%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20plots%2C%20rice%22%2C%20(plots%2C%20%22Rice%22))%2C%20(%22the%20survey's%20plots%2C%20corn%22%2C%20(plots%2C%20%22Corn%22))%2C%0A%20%20%20%20%20%20%20%20%20(%22two%20rice%20plots%20of%204%20t%2Fha%20each%2C%201%20ha%20and%205%2C000%20square%20meters%22%2C%20(drill_units()%2C%20%22Rice%22))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%201%20ha%20plot%20alone%22%2C%20(drill_units().head(1)%2C%20%22Rice%22))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22calls%22%2C%20hint%3D%22The%20check%20names%20the%20first%20case%20that%20is%20not%20right.%20What%20unit%20is%20each%20plot's%20area%20in%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20average%20rice%20yield.%0A%20%20%20%20try_it(mean_yield%2C%20plots%2C%20%22Rice%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**It%20looks%20right.**%20A%20little%20over%204%20tonnes%20per%20hectare%20is%20a%20believable%20rice%20yield%3A%20no%20error%2C%20and%20nothing%20strange%20about%20the%20number.%20Its%20check%20is%20red%20all%20the%20same.%20Look%20at%20the%20rice%20plots'%20yields%20from%20the%20lowest%20up%2C%20with%20the%20unit%20of%20each%20plot's%20area%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%20Trace%3A%20the%20rice%20plots'%20yields%20as%20the%20function%20works%20them%20out%2C%20lowest%20first%2C%20with%20each%20area's%20unit%0A%20%20%20%20_rice_plots%20%3D%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20_rice_plots.assign(yield_as_computed%3D_rice_plots%5B%22production_kg%22%5D%20%2F%201000%20%2F%20_rice_plots%5B%22area%22%5D).sort_values(%22yield_as_computed%22)%5B%0A%20%20%20%20%20%20%20%20%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22area%22%2C%20%22area_unit%22%2C%20%22production_kg%22%2C%20%22yield_as_computed%22%5D%5D.head(6)%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%20lowest%20yields%20are%20a%20few%20ten-thousandths%20of%20a%20tonne%20per%20hectare%2C%20and%20every%20one%20is%20a%20plot%20in%20%60%22sqm%22%60%3A%20their%20area%20was%20used%20as%20if%20it%20were%20hectares.%20A%20mean%20of%201%2C034%20yields%20hides%2058%20that%20are%20nearly%200%2C%20and%20only%20moves%20by%20a%20quarter%20of%20a%20tonne.%20Module%205%2C%20section%202%20puts%20a%20measure%20in%20one%20unit%20before%20using%20it.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D2.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%604.1%60%20for%20rice%20%7C%20Areas%20in%20square%20meters%20are%20used%20as%20hectares.%20%7C%20%60area_unit%60%2C%20and%20divide%20those%20areas%20by%2010%2C000%20%7C%0A%20%20%20%20%7C%20Thousands%20of%20tonnes%20per%20hectare%20%7C%20Every%20area%20was%20divided%20by%2010%2C000%2C%20hectares%20too.%20%7C%20divide%20only%20where%20%60area_unit%60%20is%20%60%22sqm%22%60%20%7C%0A%20%20%20%20%7C%20%600.0%60%20or%20a%20tiny%20yield%20%7C%20Areas%20multiplied%20by%2010%2C000%20instead%20of%20divided.%20%7C%2010%2C000%20square%20meters%20make%201%20hectare%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20whole%20report%0A%0A%20%20%20%20Each%20figure%20as%20the%20functions%20above%20give%20it%20now%2C%20beside%20what%20it%20should%20say.%20A%20figure%20whose%20function%20stops%20with%20an%20error%20shows%20the%20error%20instead.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20report()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20report's%20five%20figures%2C%20as%20the%20functions%20give%20them%2C%20beside%20what%20each%20should%20say.%22%22%22%0A%0A%20%20%20%20def%20figure(make)%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20make()%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20a%20figure%20whose%20function%20stops%20shows%20its%20error%2C%20so%20the%20rest%20of%20the%20report%20still%20shows%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20f%22%7Btype(error).__name__%7D%3A%20%7Berror%7D%22.splitlines()%5B0%5D%0A%0A%20%20%20%20def%20crops()%3A%0A%20%20%20%20%20%20%20%20by_crop%20%3D%20harvest_by_crop(plots)%0A%20%20%20%20%20%20%20%20parts%20%3D%20%5Bf%22%7B'crop%20not%20recorded'%20if%20pd.isna(name)%20else%20name%7D%20%7Bvalue%3A%2C.2f%7D%20t%22%20for%20name%2C%20value%20in%20by_crop.items()%5D%0A%20%20%20%20%20%20%20%20return%20f%22%7B'%2C%20'.join(parts)%7D%3A%20%7Bby_crop.sum()%3A%2C.2f%7D%20t%20in%20all%22%0A%0A%20%20%20%20def%20months()%3A%0A%20%20%20%20%20%20%20%20return%20%22%2C%20%22.join(f%22%7Bmonth.strftime('%25B%20%25Y')%7D%3A%20%7Bcount%3A%2C%7D%22%20for%20month%2C%20count%20in%20interviews_by_month(interview_dates).items())%0A%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%221.%20Rice%20yield%2C%20all%20farm%20households%20(weighted)%22%2C%20lambda%3A%20f%22%7Bweighted_yield(rice)%3A.2f%7D%20t%2Fha%22%2C%20%224.50%20t%2Fha%22)%2C%0A%20%20%20%20%20%20%20%20(%222.%20Rice%20harvest%2C%20all%20farm%20households%20(weighted)%22%2C%20lambda%3A%20f%22%7Brice_harvest_t(plots%2C%20households)%3A%2C%7D%20t%22%2C%20%223%2C752%2C516%20t%22)%2C%0A%20%20%20%20%20%20%20%20(%223.%20Harvest%20of%20the%20survey's%20plots%2C%20by%20crop%22%2C%20crops%2C%0A%20%20%20%20%20%20%20%20%20%22Banana%202%2C627.40%20t%2C%20Corn%202%2C044.36%20t%2C%20Rice%203%2C840.65%20t%2C%20Vegetables%201%2C046.39%20t%2C%20crop%20not%20recorded%2064.79%20t%3A%209%2C623.59%20t%20in%20all%22)%2C%0A%20%20%20%20%20%20%20%20(%224.%20Interviews%20by%20month%22%2C%20months%2C%20%22October%202025%3A%20381%2C%20November%202025%3A%20384%2C%20December%202025%3A%20425%22)%2C%0A%20%20%20%20%20%20%20%20(%225.%20Rice%20yield%2C%20the%20average%20plot%20(unweighted)%22%2C%20lambda%3A%20f%22%7Bmean_yield(plots%2C%20'Rice')%3A.2f%7D%20t%2Fha%22%2C%20%224.35%20t%2Fha%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(%5B(name%2C%20figure(make)%2C%20should)%20for%20name%2C%20make%2C%20should%20in%20rows%5D%2C%20columns%3D%5B%22figure%22%2C%20%22the%20report%20says%22%2C%20%22it%20should%20say%22%5D)%0A%20%20%20%20return%20table.assign(right%3Dtable%5B%22the%20report%20says%22%5D%20%3D%3D%20table%5B%22it%20should%20say%22%5D)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20whole%20report%3A%20run%20this%20cell%20again%20after%20each%20fix%0A%20%20%20%20report()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**What%20the%20five%20mistakes%20have%20in%20common.**%20None%20of%20them%20stopped%20with%20an%20error.%20Each%20gave%20a%20figure%20that%20looked%20like%20an%20answer%3A%20a%20yield%2C%20a%20total%2C%20a%20table%2C%20a%20count%20by%20month%2C%20another%20yield.%20Two%20were%20found%20by%20a%20number%20that%20disagreed%20with%20something%20you%20knew%3A%20months%20the%20survey%20never%20worked%20in%2C%20a%20table%20that%20did%20not%20add%20up%20to%20its%20own%20column.%20Two%20needed%20a%20count%3A%20rows%20before%20and%20after%20a%20join%2C%20provinces%20weighing%20differently.%20The%20last%20looked%20plausible%20in%20every%20way%2C%20and%20only%20a%20figure%20worked%20out%20another%20way%2C%20or%20a%20check%2C%20could%20show%20it.%20A%20report%20is%20only%20as%20right%20as%20the%20checks%20run%20on%20it%2C%20which%20is%20why%20module%205%20ended%20with%20checks%20you%20can%20rerun.%0A%0A%20%20%20%20Next%3A%20module%206%2C%20**polars**%2C%20in%20%5B50_polars.py%5D(%2F%3Ffile%3D50_polars.py).%20Or%2C%20for%20harder%20exercises%20on%20DataFrames%20first%2C%20the%20stretch%20exercises%20in%20%5B81_stretch_dataframes.py%5D(%2F%3Ffile%3D81_stretch_dataframes.py).%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%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
162da11f0d6617ea4d6d5f3d8862d7cc