import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22DataFrames%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20tempfile%0A%20%20%20%20import%20warnings%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20seaborn%20as%20sns%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20where%20the%20files%20are%2C%20the%20survey's%20tables%2C%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20PATHS%2C%20check_cases%2C%20try_it%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%22adf-dataframes-%22))%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%20DataFrames%0A%0A%20%20%20%20In%20module%202%20you%20worked%20with%20**arrays**%3A%20values%20in%20a%20row%2C%20found%20by%20their%20**position**.%20A%20survey%20table%20is%20more%20than%20that.%20Each%20column%20has%20a%20name%20and%20a%20type%20of%20its%20own%2C%20and%20each%20row%20is%20a%20household%20or%20a%20plot%20you%20want%20to%20find%20by%20its%20**ID**%2C%20not%20by%20where%20it%20happens%20to%20sit.%20A%20pandas%20**DataFrame**%20is%20that%20table%3A%20columns%20with%20names%20and%20types%2C%20and%20rows%20with%20**labels**.%0A%0A%20%20%20%20This%20module%20works%20on%20the%20farm%20survey%20files%20directly%2C%20as%20you%20would%20receive%20them.%20In%20it%20you%20will%3A%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%20Series%2C%20the%20index%20and%20alignment%20%7C%20see%20how%20labels%20keep%20each%20household's%20values%20together%2C%20even%20when%20two%20tables%20list%20them%20in%20different%20orders%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Reading%20and%20writing%20with%20the%20right%20types%20%7C%20read%20the%20survey's%20CSV%20and%20Excel%20files%20so%20codes%20stay%20codes%2C%20numbers%20are%20numbers%20and%20dates%20are%20dates%3B%20write%20CSV%2C%20Excel%20and%20Parquet%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Selecting%2C%20filtering%20and%20sorting%20%7C%20pick%20the%20rows%20and%20columns%20a%20question%20needs%2C%20and%20put%20them%20in%20order%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20New%20columns%20under%20copy-on-write%20%7C%20add%20and%20change%20columns%2C%20and%20see%20why%20one%20common%20way%20of%20changing%20values%20never%20works%20in%20pandas%203%20%7C%0A%0A%20%20%20%20Exercises%20work%20as%20in%20module%202%3A%20write%20the%20function%2C%20delete%20its%20%60raise%20NotImplementedError%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20and%20read%20the%20check.%20When%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%223.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Series%2C%20the%20index%20and%20alignment%0A%0A%20%20%20%20In%20module%202%2C%20%60sizes%5B2%5D%60%20meant%20%22the%20third%20household%22.%20That%20only%20works%20while%20every%20array%20lists%20the%20households%20in%20the%20same%20order.%20Survey%20tables%20make%20no%20such%20promise%3A%20a%20back-check%20visit%20covers%20a%20few%20households%20in%20its%20own%20order%2C%20a%20sorted%20table%20has%20a%20new%20order%2C%20and%20the%20plots%20file%20lists%20a%20household%20once%20for%20each%20of%20its%20plots.%20Subtracting%20two%20columns%20by%20position%20would%20then%20subtract%20one%20household's%20number%20from%20another's%2C%20with%20no%20error%20to%20warn%20you.%0A%0A%20%20%20%20pandas%20gives%20every%20value%20a%20**label**%20as%20well%20as%20a%20position.%20A%20**Series**%20is%20one%20column%3A%20its%20values%2C%20plus%20an%20**index**%20that%20holds%20a%20label%20for%20each%20value.%20Here%20is%20household%20size%2C%20labeled%20by%20household%20ID%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%20households%20%3D%20course.households()%20%20%23%20the%20households%2C%20read%20with%20the%20right%20types%20(section%202%20shows%20how)%0A%20%20%20%20size%20%3D%20households.set_index(%22hh_id%22)%5B%22hh_size%22%5D%20%20%23%20one%20column%2C%20labeled%20by%20household%20ID%0A%20%20%20%20size.head()%0A%20%20%20%20return%20(households%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%20left%20column%20is%20the%20index%20(the%20labels)%3B%20the%20right%20column%20is%20the%20values.%20Each%20part%20can%20be%20reached%20on%20its%20own%3A%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20you%20get%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60size.index%60%20%7C%20the%20labels%3A%20%60HH-0001%60%2C%20%60HH-0002%60%2C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%60size.to_numpy()%60%20%7C%20the%20values%2C%20as%20the%20NumPy%20array%20of%20module%202%20%7C%0A%20%20%20%20%7C%20%60size.name%60%20%7C%20the%20Series'%20name%2C%20%60%22hh_size%22%60%3A%20the%20column%20it%20came%20from%20%7C%0A%20%20%20%20%7C%20%60size%5B%22HH-0005%22%5D%60%20%7C%20the%20value%20labeled%20%60HH-0005%60%2C%20wherever%20it%20sits%20%7C%0A%0A%20%20%20%20A%20**DataFrame**%20is%20several%20Series%20that%20share%20one%20index%3A%20one%20Series%20per%20column.%20%60households%60%20above%20has%20the%20default%20index%2C%20the%20row%20numbers%200%2C%201%2C%202%2C%20%E2%80%A6%2C%20which%20pandas%20gives%20any%20table%20read%20from%20a%20file.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(households)%3A%0A%20%20%20%20households.head()%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%20Alignment%3A%20arithmetic%20lines%20values%20up%20by%20label%0A%0A%20%20%20%20A%20supervisor%20goes%20back%20to%20a%20few%20households%20and%20asks%20the%20same%20questions%20again%2C%20to%20check%20the%20interviews%3A%20a%20**back-check**.%20Below%2C%20the%20household%20sizes%20from%20the%20interview%20and%20from%20the%20back-check.%20The%20back-check%20lists%20its%20households%20in%20another%20order%2C%20and%20it%20skipped%20%60HH-0004%60%20but%20visited%20%60HH-0009%60%2C%20which%20the%20interview%20list%20here%20leaves%20out.%0A%0A%20%20%20%20When%20pandas%20subtracts%20one%20Series%20from%20another%2C%20it%20pairs%20values%20**by%20label**%2C%20not%20by%20position.%20This%20is%20called%20**alignment**.%20Every%20label%20in%20either%20Series%20gets%20a%20row%20in%20the%20result%3B%20where%20a%20label%20is%20in%20only%20one%20of%20them%2C%20there%20is%20nothing%20to%20subtract%2C%20so%20the%20result%20is%20%60NaN%60%20(%22not%20a%20number%22%2C%20pandas'%20mark%20for%20a%20missing%20value).%20Because%20%60NaN%60%20is%20a%20decimal%20number%2C%20the%20result's%20type%20becomes%20%60float64%60%3B%20module%205%20shows%20whole-number%20types%20that%20can%20hold%20missing%20values.%0A%0A%20%20%20%20Switch%20between%20the%20two%20ways%20of%20lining%20up%20the%20values%2C%20and%20compare%20the%20households%20in%20each%20row.%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%20interview%20%3D%20pd.Series(%7B%22HH-0001%22%3A%207%2C%20%22HH-0002%22%3A%202%2C%20%22HH-0003%22%3A%208%2C%20%22HH-0004%22%3A%201%7D%2C%20name%3D%22hh_size%22)%0A%20%20%20%20back_check%20%3D%20pd.Series(%7B%22HH-0003%22%3A%208%2C%20%22HH-0001%22%3A%206%2C%20%22HH-0002%22%3A%202%2C%20%22HH-0009%22%3A%205%7D%2C%20name%3D%22hh_size%22)%0A%20%20%20%20back_check%20-%20interview%20%20%23%20pandas%20lines%20the%20two%20up%20by%20label%0A%20%20%20%20return%20back_check%2C%20interview%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20line_up%20%3D%20mo.ui.radio(options%3D%5B%22by%20label%20(pandas)%22%2C%20%22by%20position%20(NumPy%20arrays)%22%5D%2C%20value%3D%22by%20label%20(pandas)%22%2C%20label%3D%22Line%20the%20two%20up%22)%0A%20%20%20%20line_up%0A%20%20%20%20return%20(line_up%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(back_check%2C%20interview%2C%20line_up)%3A%0A%20%20%20%20if%20line_up.value.startswith(%22by%20label%22)%3A%0A%20%20%20%20%20%20%20%20_table%20%3D%20pd.DataFrame(%7B%22back-check%22%3A%20back_check%2C%20%22interview%22%3A%20interview%7D)%0A%20%20%20%20%20%20%20%20_table%5B%22back-check%20%E2%88%92%20interview%22%5D%20%3D%20back_check%20-%20interview%0A%20%20%20%20%20%20%20%20_note%20%3D%20(%22Each%20row%20is%20one%20household.%20%60HH-0001%60%20lost%20one%20member%3B%20%60HH-0002%60%20and%20%60HH-0003%60%20are%20unchanged.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%60HH-0004%60%20and%20%60HH-0009%60%20are%20each%20in%20one%20list%20only%2C%20so%20their%20difference%20is%20%60NaN%60%3A%20honest%2C%20because%20there%20is%20nothing%20to%20compare.%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_table%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22back-check%20household%22%3A%20back_check.index%2C%20%22back-check%22%3A%20back_check.to_numpy()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22interview%20household%22%3A%20interview.index%2C%20%22interview%22%3A%20interview.to_numpy()%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%20%20%20%20%20%20%20%20_table%5B%22back-check%20%E2%88%92%20interview%22%5D%20%3D%20back_check.to_numpy()%20-%20interview.to_numpy()%0A%20%20%20%20%20%20%20%20_note%20%3D%20(%22Each%20row%20pairs%20the%20n-th%20value%20of%20one%20list%20with%20the%20n-th%20value%20of%20the%20other.%20Row%200%20subtracts%20%60HH-0001%60's%207%20from%20%60HH-0003%60's%208%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22and%20so%20on%3A%20four%20differences%2C%20no%20error%2C%20and%20every%20one%20of%20them%20meaningless.%22)%0A%20%20%20%20mo.vstack(%5B_table%2C%20mo.md(_note)%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%60NaN%60%20is%20usually%20the%20right%20answer%3A%20a%20household%20that%20was%20not%20back-checked%20has%20no%20change%20to%20report.%20Sometimes%20a%20missing%20label%20truly%20means%20zero%20(a%20household%20with%20no%20plots%20of%20a%20crop%20harvested%200%20kg%20of%20it).%20Then%20the%20method%20form%20of%20the%20operator%20takes%20a%20%60fill_value%60%2C%20used%20for%20a%20label%20missing%20on%20one%20side%3A%20%60a.add(b%2C%20fill_value%3D0)%60%2C%20%60a.sub(b%2C%20fill_value%3D0)%60.%20Use%20it%20only%20when%20zero%20is%20true%3B%20a%20household's%20size%20is%20never%200.%0A%0A%20%20%20%20%23%23%23%20Moving%20labels%20in%20and%20out%3A%20set_index%20and%20reset_index%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20what%20it%20does%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60households.set_index(%22hh_id%22)%60%20%7C%20moves%20the%20%60hh_id%60%20column%20into%20the%20index%3A%20rows%20are%20now%20labeled%20by%20household%20%7C%0A%20%20%20%20%7C%20%60table.loc%5B%22HH-0005%22%5D%60%20%7C%20the%20row%20labeled%20%60HH-0005%60%20(section%203%20has%20more%20on%20%60.loc%60)%20%7C%0A%20%20%20%20%7C%20%60table.reset_index()%60%20%7C%20moves%20the%20index%20back%20into%20a%20column%2C%20and%20numbers%20the%20rows%200%2C%201%2C%202%2C%20%E2%80%A6%20again%20%7C%0A%20%20%20%20%7C%20%60table.reset_index(drop%3DTrue)%60%20%7C%20numbers%20the%20rows%20again%20and%20throws%20the%20old%20labels%20away%20(after%20filtering%2C%20say)%20%7C%0A%20%20%20%20%7C%20%60table.index.is_unique%60%20%7C%20%60True%60%20when%20no%20label%20repeats%20%7C%0A%0A%20%20%20%20Labels%20do%20not%20have%20to%20be%20unique.%20The%20raw%20households%20file%20has%20six%20households%20entered%20twice%2C%20so%20a%20table%20indexed%20by%20%60hh_id%60%20from%20it%20has%20six%20labels%20that%20each%20find%20two%20rows%3B%20%60course.households()%60%20has%20dropped%20those%20copies%20(module%205%20shows%20how).%20Check%20%60is_unique%60%20before%20you%20rely%20on%20one%20label%20meaning%20one%20row.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(households)%3A%0A%20%20%20%20_by_household%20%3D%20households.set_index(%22hh_id%22)%0A%20%20%20%20_by_household.loc%5B%5B%22HH-0005%22%2C%20%22HH-0001%22%5D%2C%20%5B%22province%22%2C%20%22hh_size%22%2C%20%22weight%22%5D%5D%20%20%23%20rows%20by%20label%2C%20in%20the%20order%20asked%20for%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%203.1.%20Yield%20by%20household%2C%20lined%20up%20by%20label%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20yield%20(tonnes%20harvested%20per%20hectare)%20is%20the%20survey's%20main%20result.%20Production%20and%20area%20often%20come%20from%20different%20steps%20of%20the%20work%2C%20each%20listing%20households%20in%20its%20own%20order.%20Dividing%20them%20by%20position%20mixes%20households%20without%20any%20error%3B%20dividing%20them%20by%20label%20cannot.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20two%20Series%20indexed%20by%20%60hh_id%60%2C%20%60production_kg%60%20(kilograms%20harvested)%20and%20%60area_ha%60%20(hectares%20planted)%20%E2%86%92%20one%20Series%20of%20yield%20in%20**tonnes%20per%20hectare**%2C%20%60production_kg%20%2F%201000%20%2F%20area_ha%60%2C%20for%20the%20households%20that%20are%20in%20**both**%20Series.%20The%20two%20may%20list%20households%20in%20different%20orders%2C%20and%20each%20may%20have%20a%20household%20the%20other%20lacks.%0A%0A%20%20%20%20%7C%20hh_id%20%7C%20production_kg%20%7C%20area_ha%20%7C%20yield%20(t%2Fha)%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20HH-0001%20%7C%202%2C340%20%7C%200.633%20%7C%203.697%20%7C%0A%20%20%20%20%7C%20HH-0002%20%7C%205%2C990%20%7C%201.396%20%7C%204.291%20%7C%0A%20%20%20%20%7C%20HH-0005%20%7C%201%2C500%20%7C%20(not%20in%20area_ha)%20%7C%20left%20out%20%7C%0A%0A%20%20%20%20**Tools%3A**%20arithmetic%20between%20two%20Series%20lines%20them%20up%20by%20label%20(alignment%2C%20above)%3B%20a%20label%20in%20only%20one%20of%20them%20gives%20%60NaN%60%2C%20and%20%60.dropna()%60%20drops%20those%20values.%20Name%20the%20result%20%60%22yield_t_ha%22%60%20with%20%60.rename(%22yield_t_ha%22)%60%20if%20you%20like%3B%20the%20check%20does%20not%20look%20at%20the%20name.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20yield_by_household(production_kg%3A%20pd.Series%2C%20area_ha%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Yield%20in%20tonnes%20per%20hectare%20for%20each%20household%20in%20both%20Series%2C%20lined%20up%20by%20household%20ID.%22%22%22%0A%20%20%20%20%23%20Exercise%203.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20divide%20production_kg%20by%201000%20and%20by%20area_ha%20(pandas%20lines%20them%20up%20by%20hh_id)%0A%20%20%20%20%23%20Step%202%3A%20drop%20the%20households%20whose%20yield%20is%20NaN%20(those%20in%20only%20one%20Series)%2C%20and%20return%20the%20result%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.1%22)%0A%0A%0A%40app.function%0Adef%20yield_fixture(case%3A%20str)%20-%3E%20tuple%5Bpd.Series%2C%20pd.Series%5D%3A%0A%20%20%20%20%22%22%22Production%20(kg)%20and%20area%20(ha)%20by%20household%20for%20the%20check%20of%20Exercise%203.1%3A%20the%20same%20households%20in%20the%20same%20order%2C%0A%20%20%20%20in%20another%20order%2C%20or%20with%20a%20household%20on%20each%20side%20that%20the%20other%20lacks.%22%22%22%0A%20%20%20%20production%20%3D%20pd.Series(%7B%22HH-0001%22%3A%202340%2C%20%22HH-0002%22%3A%205990%2C%20%22HH-0003%22%3A%201200%7D%2C%20name%3D%22production_kg%22)%0A%20%20%20%20area%20%3D%20pd.Series(%7B%22HH-0001%22%3A%200.633%2C%20%22HH-0002%22%3A%201.396%2C%20%22HH-0003%22%3A%200.4%7D%2C%20name%3D%22area_ha%22)%0A%20%20%20%20if%20case%20%3D%3D%20%22same%20order%22%3A%0A%20%20%20%20%20%20%20%20return%20production%2C%20area%0A%20%20%20%20if%20case%20%3D%3D%20%22another%20order%22%3A%0A%20%20%20%20%20%20%20%20return%20production%2C%20area.iloc%5B%5B2%2C%200%2C%201%5D%5D%0A%20%20%20%20production%20%3D%20pd.concat(%5Bproduction%2C%20pd.Series(%7B%22HH-0005%22%3A%201500%7D)%5D).rename(%22production_kg%22)%0A%20%20%20%20area%20%3D%20pd.concat(%5Barea.iloc%5B%5B2%2C%200%2C%201%5D%5D%2C%20pd.Series(%7B%22HH-0004%22%3A%200.25%7D)%5D).rename(%22area_ha%22)%0A%20%20%20%20return%20production%2C%20area%0A%0A%0A%40app.function%0Adef%20household_rice()%20-%3E%20tuple%5Bpd.Series%2C%20pd.Series%5D%3A%0A%20%20%20%20%22%22%22The%20survey's%20rice%20production%20(kg)%20and%20rice%20area%20(ha)%20by%20household%2C%20the%20area%20listed%20from%20the%20largest%20down.%0A%20%20%20%20(Grouping%2C%20which%20makes%20these%20totals%2C%20is%20module%204.)%22%22%22%0A%20%20%20%20plots%20%3D%20course.plots()%0A%20%20%20%20rice%20%3D%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20production%20%3D%20rice.groupby(%22hh_id%22)%5B%22production_kg%22%5D.sum()%0A%20%20%20%20area%20%3D%20rice.groupby(%22hh_id%22)%5B%22area_ha%22%5D.sum().sort_values(ascending%3DFalse)%0A%20%20%20%20return%20production%2C%20area%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20def%20_without_name(result)%3A%0A%20%20%20%20%20%20%20%20%23%20The%20name%20is%20not%20the%20question%3A%20compare%20the%20labels%20and%20the%20values%2C%20to%206%20decimal%20places%20(so%20dividing%20in%0A%20%20%20%20%20%20%20%20%23%20another%20order%2C%20which%20changes%20the%20last%20digits%20of%20a%20decimal%2C%20gives%20the%20same%20answer)%0A%20%20%20%20%20%20%20%20return%20result.rename(None).round(6)%20if%20isinstance(result%2C%20pd.Series)%20else%20result%0A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.1%22%2C%20yield_by_household%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%2C%20listed%20in%20the%20same%20order%20in%20both%22%2C%20yield_fixture(%22same%20order%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20three%2C%20area%20listed%20in%20another%20order%22%2C%20yield_fixture(%22another%20order%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20household%20in%20production%20only%2C%20and%20another%20in%20area%20only%22%2C%20yield_fixture(%22one%20on%20each%20side%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20rice%2C%20area%20sorted%20from%20the%20largest%22%2C%20household_rice())%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20Series%22%2C%20summarize%3D_without_name%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20second%20case%20lists%20area%20in%20another%20order%3A%20a%20result%20that%20pairs%20values%20by%20position%20goes%20wrong%20there.%20The%20third%20has%20a%20household%20on%20each%20side%20that%20the%20other%20lacks.%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%20your%20function%20on%20the%20survey's%20rice.%20Compare%20a%20few%20households%20with%20the%20table%20in%20the%20exercise.%0A%20%20%20%20try_it(yield_by_household%2C%20*household_rice())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20first%20case%20is%20right%2C%20the%20second%20is%20not%20%7C%20The%20values%20were%20divided%20by%20position%2C%20through%20%60.to_numpy()%60%20or%20%60.values%60.%20%7C%20divide%20the%20two%20Series%20themselves%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20operands%20could%20not%20be%20broadcast%20together*%20%7C%20Arrays%20of%20different%20lengths%20were%20divided%20by%20position.%20%7C%20divide%20the%20two%20Series%20themselves%20%7C%0A%20%20%20%20%7C%20The%20third%20case%20is%20not%20right%2C%20and%20your%20result%20has%20%60NaN%60%20in%20it%20%7C%20The%20households%20in%20only%20one%20Series%20were%20kept.%20%7C%20%60.dropna()%60%20%7C%0A%20%20%20%20%7C%20Every%20yield%20is%201%2C000%20times%20too%20large%20%7C%20Kilograms%20were%20not%20turned%20into%20tonnes.%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%20Exercise%203.2.%20Back-check%3A%20what%20changed%2C%20household%20by%20household%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20back-check%20is%20how%20a%20survey%20team%20finds%20interviews%20that%20went%20wrong.%20Its%20tables%20arrive%20as%20files%20with%20an%20%60hh_id%60%20column%20and%20row%20numbers%200%2C%201%2C%202%2C%20%E2%80%A6%2C%20so%20the%20rows%20of%20two%20tables%20do%20not%20line%20up%20until%20you%20make%20%60hh_id%60%20the%20index.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20two%20tables%2C%20%60interview%60%20and%20%60back_check%60%2C%20each%20with%20columns%20%60hh_id%60%20and%20%60hh_size%60%2C%20rows%20in%20any%20order%20%E2%86%92%20a%20table%20with%20columns%20%60hh_id%60%20and%20%60change%60%20(back-check%20size%20minus%20interview%20size)%2C%20one%20row%20for%20**every%20household%20in%20either%20table**%2C%20in%20%60hh_id%60%20order%2C%20rows%20numbered%200%2C%201%2C%202%2C%20%E2%80%A6%20A%20household%20in%20only%20one%20table%20has%20%60change%60%20%60NaN%60.%0A%0A%20%20%20%20%7C%20interview%20%7C%20%7C%20back_check%20%7C%20%7C%20%E2%86%92%20%7C%20result%20%7C%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%3A--%7C--%3A%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20**hh_id**%20%7C%20**hh_size**%20%7C%20**hh_id**%20%7C%20**hh_size**%20%7C%20%7C%20**hh_id**%20%7C%20**change**%20%7C%0A%20%20%20%20%7C%20HH-0001%20%7C%207%20%7C%20HH-0003%20%7C%208%20%7C%20%7C%20HH-0001%20%7C%20-1.0%20%7C%0A%20%20%20%20%7C%20HH-0002%20%7C%202%20%7C%20HH-0001%20%7C%206%20%7C%20%7C%20HH-0002%20%7C%20NaN%20%7C%0A%20%20%20%20%7C%20HH-0003%20%7C%208%20%7C%20%7C%20%7C%20%7C%20HH-0003%20%7C%200.0%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60set_index(%22hh_id%22)%60%20on%20each%20table%2C%20then%20subtract%20their%20%60hh_size%60%20columns%20(alignment%20pairs%20the%20households%2C%20and%20puts%20the%20labels%20in%20order%20when%20the%20two%20tables%20list%20different%20ones)%3B%20%60.rename(%22change%22)%60%2C%20then%20%60.reset_index()%60%20to%20turn%20the%20labels%20back%20into%20an%20%60hh_id%60%20column%20(all%20in%20section%201%20above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20size_change(interview%3A%20pd.DataFrame%2C%20back_check%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22For%20every%20household%20in%20either%20table%3A%20back-check%20size%20minus%20interview%20size%2C%20as%20columns%20hh_id%20and%20change.%22%22%22%0A%20%20%20%20%23%20Exercise%203.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20from%20each%20table%2C%20take%20hh_size%20labeled%20by%20hh_id%20(set_index%2C%20then%20the%20column)%0A%20%20%20%20%23%20Step%202%3A%20subtract%20the%20interview's%20sizes%20from%20the%20back-check's%2C%20and%20name%20the%20result%20%22change%22%0A%20%20%20%20%23%20Step%203%3A%20turn%20the%20labels%20back%20into%20an%20hh_id%20column%20(reset_index)%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.2%22)%0A%0A%0A%40app.function%0Adef%20back_check_fixture(case%3A%20str)%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22An%20interview%20table%20and%20a%20back-check%20table%20for%20the%20check%20of%20Exercise%203.2.%22%22%22%0A%20%20%20%20interview%20%3D%20pd.DataFrame(%7B%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%20%22hh_size%22%3A%20%5B7%2C%202%2C%208%2C%201%5D%7D)%0A%20%20%20%20if%20case%20%3D%3D%20%22same%20order%22%3A%0A%20%20%20%20%20%20%20%20back_check%20%3D%20pd.DataFrame(%7B%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%20%22hh_size%22%3A%20%5B6%2C%202%2C%208%2C%202%5D%7D)%0A%20%20%20%20elif%20case%20%3D%3D%20%22another%20order%22%3A%0A%20%20%20%20%20%20%20%20back_check%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0003%22%2C%20%22HH-0001%22%2C%20%22HH-0004%22%2C%20%22HH-0002%22%5D%2C%20%22hh_size%22%3A%20%5B8%2C%206%2C%202%2C%202%5D%7D)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20back_check%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0004%22%2C%20%22HH-0002%22%2C%20%22HH-0009%22%5D%2C%20%22hh_size%22%3A%20%5B2%2C%203%2C%205%5D%7D)%0A%20%20%20%20return%20interview%2C%20back_check%0A%0A%0A%40app.function%0Adef%20back_check_visits()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20back-check%20of%2060%20of%20the%20survey's%20households%2C%20in%20the%20order%20the%20supervisor%20visited%20them%3A%20most%20sizes%20as%20in%20the%0A%20%20%20%20interview%2C%20some%20one%20member%20more%20or%20less.%20(Made%20up%20for%20this%20notebook%3B%20the%20survey%20files%20have%20no%20back-check.)%22%22%22%0A%20%20%20%20households%20%3D%20course.households()%0A%20%20%20%20visited%20%3D%20households.sample(60%2C%20random_state%3D7)%5B%5B%22hh_id%22%2C%20%22hh_size%22%5D%5D%0A%20%20%20%20change%20%3D%20np.random.default_rng(7).choice(%5B-1%2C%200%2C%200%2C%200%2C%200%2C%201%5D%2C%20size%3Dlen(visited))%0A%20%20%20%20return%20visited.assign(hh_size%3D(visited%5B%22hh_size%22%5D%20%2B%20change).clip(lower%3D1)).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%203.2%22%2C%20size_change%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20households%2C%20listed%20in%20the%20same%20order%20in%20both%22%2C%20back_check_fixture(%22same%20order%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20four%2C%20the%20back-check%20in%20another%20order%22%2C%20back_check_fixture(%22another%20order%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20back-check%20that%20skipped%20two%20households%20and%20visited%20one%20more%22%2C%20back_check_fixture(%22one%20more%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%201%2C190%20households%20and%20a%20back-check%20of%2060%22%2C%20(course.households()%5B%5B%22hh_id%22%2C%20%22hh_size%22%5D%5D%2C%20back_check_visits()))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20second%20case%20lists%20the%20back-check%20in%20another%20order%2C%20so%20subtracting%20row%20by%20row%20pairs%20the%20wrong%20households.%20The%20result%20has%20columns%20hh_id%20and%20change%2C%20rows%20numbered%200%2C%201%2C%202%2C%20%E2%80%A6.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20interviews%20against%20the%20back-check%2C%20showing%20the%20households%20whose%20size%20changed.%0A%20%20%20%20def%20_try_size_change()%3A%0A%20%20%20%20%20%20%20%20interview_sizes%2C%20visits%20%3D%20course.households()%5B%5B%22hh_id%22%2C%20%22hh_size%22%5D%5D%2C%20back_check_visits()%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20result%20%3D%20size_change(interview_sizes%2C%20visits)%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(size_change%2C%20interview_sizes%2C%20visits)%20%20%23%20explains%20a%20%22not%20written%20yet%22%20or%20an%20error%0A%20%20%20%20%20%20%20%20if%20not%20isinstance(result%2C%20pd.DataFrame)%20or%20%22change%22%20not%20in%20result.columns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20try_it(size_change%2C%20interview_sizes%2C%20visits)%0A%20%20%20%20%20%20%20%20checked%20%3D%20result.dropna(subset%3D%5B%22change%22%5D)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(result)%3A%2C%7D%20rows**%2C%20one%20per%20household%3B%20**%7Blen(checked)%7D**%20were%20back-checked%20(the%20others%20have%20%60NaN%60)%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22and%20**%7Bint((checked%5B'change'%5D%20!%3D%200).sum())%7D**%20of%20those%20changed%20size%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20checked%5Bchecked%5B%22change%22%5D%20!%3D%200%5D%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_try_size_change()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20first%20case%20is%20right%2C%20the%20second%20is%20not%20%7C%20The%20%60hh_size%60%20columns%20were%20subtracted%20without%20%60set_index(%22hh_id%22)%60%2C%20so%20rows%20were%20paired%20by%20number.%20%7C%20step%201%20%7C%0A%20%20%20%20%7C%20The%20result's%20labels%20are%20%60HH-0001%60%2C%20%E2%80%A6%20and%20it%20has%20no%20%60hh_id%60%20column%20%7C%20The%20labels%20were%20not%20turned%20back%20into%20a%20column.%20%7C%20%60.reset_index()%60%20%7C%0A%20%20%20%20%7C%20The%20columns%20are%20%60hh_id%60%20and%20%60hh_size%60%2C%20or%20%60index%60%20and%20%60change%60%20%7C%20The%20Series%20was%20not%20renamed%2C%20or%20was%20made%20without%20%60set_index(%22hh_id%22)%60.%20%7C%20%60.rename(%22change%22)%60%2C%20step%201%20%7C%0A%20%20%20%20%7C%20Every%20change%20has%20the%20wrong%20sign%20%7C%20Interview%20minus%20back-check%2C%20instead%20of%20back-check%20minus%20interview.%20%7C%20step%202%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'hh_id'*%20%7C%20%60set_index%60%20was%20called%20twice%2C%20or%20after%20the%20column%20was%20already%20the%20index.%20%7C%20the%20order%20of%20the%20steps%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%202%20%C2%B7%20Reading%20and%20writing%20with%20the%20right%20types%3A%20codes%20as%20text%2C%20thousands%20separators%2C%20Excel%20header%20rows%20and%20sheets%2C%20CSV%2C%20Excel%2C%20Parquet%0A%0A%20%20%20%20A%20CSV%20file%20stores%20only%20text.%20When%20pandas%20reads%20one%2C%20it%20guesses%20each%20column's%20type%20from%20what%20its%20values%20look%20like.%20Most%20guesses%20are%20right.%20The%20ones%20that%20are%20wrong%20are%20wrong%20**silently**%3A%20no%20error%2C%20just%20a%20column%20that%20holds%20something%20other%20than%20what%20the%20file%20meant.%20The%20farm%20survey's%20files%20have%20four%20such%20traps%2C%20and%20each%20has%20a%20fix%20you%20write%20on%20the%20way%20in.%0A%0A%20%20%20%20%7C%20file%20%7C%20column%20%7C%20read%20plainly%2C%20you%20get%20%7C%20what%20is%20wrong%20%7C%20the%20fix%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60households.csv%60%20%7C%20%60psgc%60%20%7C%20whole%20numbers%3A%20%60102820000%60%20%7C%20854%20codes%20lose%20their%20leading%20zero%20%7C%20%60dtype%3D%7B%22psgc%22%3A%20str%7D%60%20%7C%0A%20%20%20%20%7C%20%60households.csv%60%20%7C%20%60interview_date%60%20%7C%20text%3A%20%60%2219%2F12%2F2025%22%60%20%7C%20text%2C%20not%20dates%3B%20and%20day%20and%20month%20can%20be%20swapped%20%7C%20%60pd.to_datetime(...%2C%20format%3D%22%25d%2F%25m%2F%25Y%22)%60%20%7C%0A%20%20%20%20%7C%20%60plots.csv%60%20%7C%20%60production_kg%60%20%7C%20text%3A%20%60%221%2C640%22%60%20%7C%20adding%20them%20glues%20text%20together%20%7C%20%60thousands%3D%22%2C%22%60%20%7C%0A%20%20%20%20%7C%20%60farm_survey_2025.xlsx%60%20%7C%20every%20column%20%7C%20a%20title%20row%20as%20the%20header%20%7C%20three%20title%20rows%20sit%20above%20the%20table%20%7C%20%60sheet_name%3D%22Households%22%2C%20header%3D3%60%20%7C%0A%20%20%20%20%7C%20%60farm_survey_2025.xlsx%60%20%7C%20%60psgc%60%20%7C%20whole%20numbers%20again%20%7C%20Excel%20stored%20the%20codes%20as%20numbers%20%7C%20%60.astype(str).str.zfill(10)%60%20%7C%0A%0A%20%20%20%20The%20files%20are%20in%20the%20course%20folder%2C%20at%20%60PATHS.survey%60.%20Read%20them%20yourself%20as%20you%20go%3B%20each%20step%20below%20shows%20the%20plain%20read%20first%2C%20then%20the%20fix.%0A%0A%20%20%20%20%23%23%23%20Codes%20are%20labels%2C%20not%20quantities%0A%0A%20%20%20%20A%20**PSGC%20code**%20(Philippine%20Standard%20Geographic%20Code)%20names%20a%20city%20or%20municipality%3A%20its%20digits%20say%20the%20region%2C%20province%20and%20municipality%2C%20so%20%600102820000%60%20is%20in%20region%2001.%20Every%20code%20has%20ten%20digits.%20Read%20plainly%2C%20pandas%20sees%20digits%20and%20makes%20the%20column%20whole%20numbers%2C%20and%20a%20number%20has%20no%20leading%20zero%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%20plain_households%20%3D%20pd.read_csv(PATHS.survey%20%2F%20%22households.csv%22)%20%20%23%20no%20options%3A%20pandas%20guesses%20every%20type%0A%20%20%20%20plain_households.dtypes%0A%20%20%20%20return%20(plain_households%2C)%0A%0A%0A%40app.cell%0Adef%20_(plain_households)%3A%0A%20%20%20%20_short%20%3D%20plain_households%5B%22psgc%22%5D.astype(str).str.len()%20%3C%2010%20%20%23%20how%20many%20codes%20have%20fewer%20than%2010%20digits%20now%0A%20%20%20%20mo.md(f%22The%20first%20code%20reads%20as%20%60%7Bplain_households%5B'psgc'%5D.iloc%5B0%5D%7D%60%3B%20the%20file%20says%20%600102820000%60.%20**%7B_short.sum()%3A%2C%7D%20of%20%7Blen(plain_households)%3A%2C%7D**%20codes%20have%20lost%20their%20leading%20zero%2C%20and%20a%20join%20to%20the%20PSGC%20code%20list%20on%20them%20would%20find%20nothing.%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%20Tell%20pandas%20the%20column%20is%20text%20with%20%60dtype%3D%60%2C%20a%20dictionary%20of%20column%20names%20and%20types.%20Name%20only%20the%20columns%20that%20need%20it%3A%20%60dtype%3Dstr%60%20for%20the%20whole%20file%20would%20make%20%60hh_size%60%20and%20%60weight%60%20text%20too.%20A%20useful%20rule%3A%20if%20you%20would%20never%20add%2C%20subtract%20or%20average%20it%2C%20it%20is%20a%20code%2C%20and%20it%20should%20be%20text.%20Household%20IDs%2C%20area%20codes%20and%20phone%20numbers%20are%20codes%3B%20household%20size%20is%20a%20number.%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%20pd.read_csv(PATHS.survey%20%2F%20%22households.csv%22%2C%20dtype%3D%7B%22psgc%22%3A%20str%7D)%5B%5B%22hh_id%22%2C%20%22psgc%22%5D%5D.head(3)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20you%20know%20pandas%202%3A%20text%20columns%20are%20now%20%60str%60%2C%20not%20%60object%60%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20In%20pandas%203%2C%20a%20column%20of%20text%20gets%20its%20own%20type%2C%20shown%20as%20**%60str%60**.%20In%20pandas%202%20text%20was%20stored%20as%20%60object%60%2C%20a%20type%20that%20holds%20any%20Python%20value.%20What%20changes%20for%20you%3A%0A%0A%20%20%20%20%7C%20%7C%20pandas%202%20%7C%20pandas%203%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20type%20of%20a%20text%20column%20%7C%20%60object%60%20%7C%20%60str%60%20%7C%0A%20%20%20%20%7C%20%60table%5B%22psgc%22%5D.dtype%20%3D%3D%20object%60%20for%20a%20text%20column%20%7C%20%60True%60%20%7C%20%60False%60%3A%20test%20for%20%60%22str%22%60%20instead%20%7C%0A%20%20%20%20%7C%20a%20missing%20value%20in%20a%20text%20column%20%7C%20%60None%60%20or%20%60NaN%60%20%7C%20always%20%60NaN%60%20%7C%0A%20%20%20%20%7C%20putting%20a%20number%20into%20a%20text%20column%20%7C%20allowed%3A%20the%20column%20holds%20a%20mix%20%7C%20*TypeError%3A%20Invalid%20value%20'1'%20for%20dtype%20'str'*%20%7C%0A%20%20%20%20%7C%20%60dtype%3D%7B%22psgc%22%3A%20str%7D%60%20when%20reading%20%7C%20gives%20%60object%60%20%7C%20gives%20%60str%60%20%7C%0A%0A%20%20%20%20%60.str%60%20methods%20(%60.str.len()%60%2C%20%60.str.zfill()%60%2C%20%60.str.startswith()%60)%20work%20as%20before.%20Every%20difference%20is%20listed%20in%20%5Bprimer%20P2%2C%20pandas%202%20to%203%5D(%2F%3Ffile%3D91_primer_pandas_2_to_3.py).%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%20Numbers%20with%20thousands%20separators%0A%0A%20%20%20%20The%20survey%20system%20writes%20production%20with%20a%20comma%20for%20thousands%3A%20%60%221%2C640%22%60.%20To%20pandas%20a%20comma%20is%20not%20part%20of%20a%20number%2C%20so%20the%20whole%20column%20stays%20text.%20Nothing%20fails%20until%20you%20compute%2C%20and%20even%20then%20a%20sum%20of%20text%20does%20not%20fail%3A%20it%20glues%20the%20texts%20together.%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_plain%20%3D%20pd.read_csv(PATHS.survey%20%2F%20%22plots.csv%22)%0A%20%20%20%20_total%20%3D%20_plain%5B%22production_kg%22%5D.sum()%20%20%23%20a%20sum%20of%20text%3A%20one%20long%20text%0A%20%20%20%20mo.md(f%22%60production_kg%60%20reads%20as%20**%60%7B_plain%5B'production_kg'%5D.dtype%7D%60**%2C%20and%20its%20sum%20begins%20%60%7B_total%5B%3A40%5D%7D%E2%80%A6%60%3A%20every%20value%20written%20one%20after%20another%2C%20%7Blen(_total)%3A%2C%7D%20characters%20long.%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%60thousands%3D%22%2C%22%60%20tells%20the%20reader%20that%20a%20comma%20inside%20a%20number%20separates%20thousands.%20It%20applies%20to%20every%20column%2C%20so%20any%20number%20the%20system%20wrote%20with%20a%20comma%20reads%20as%20a%20number%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_plots%20%3D%20pd.read_csv(PATHS.survey%20%2F%20%22plots.csv%22%2C%20thousands%3D%22%2C%22)%0A%20%20%20%20mo.md(f%22%60production_kg%60%20is%20now%20**%60%7B_plots%5B'production_kg'%5D.dtype%7D%60**%2C%20and%20the%20survey's%20plots%20harvested%20**%7B_plots%5B'production_kg'%5D.sum()%3A%2C%7D%20kg**%20in%20all.%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%20Dates%20on%20the%20way%20in%3A%20day%20first%0A%0A%20%20%20%20%60interview_date%60%20is%20written%20day%20first%2C%20%6019%2F12%2F2025%60%3A%20the%2019th%20of%20December.%20Read%20plainly%20it%20is%20text%2C%20and%20text%20sorts%20letter%20by%20letter%3A%20%6001%2F10%2F2025%60%2C%20%6003%2F11%2F2025%60%2C%20%6009%2F11%2F2025%60%2C%20%6010%2F12%2F2025%60%2C%20%6014%2F10%2F2025%60%E2%80%A6%20The%20months%20are%20shuffled.%0A%0A%20%20%20%20%60pd.to_datetime%60%20turns%20text%20into%20dates.%20Give%20it%20the%20**format**%2C%20so%20it%20never%20has%20to%20guess%3A%0A%0A%20%20%20%20%7C%20code%20%7C%20means%20%7C%20in%20%6019%2F12%2F2025%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%25d%60%20%7C%20day%2C%20two%20digits%20%7C%20%6019%60%20%7C%0A%20%20%20%20%7C%20%60%25m%60%20%7C%20month%2C%20two%20digits%20%7C%20%6012%60%20%7C%0A%20%20%20%20%7C%20%60%25Y%60%20%7C%20year%2C%20four%20digits%20%7C%20%602025%60%20%7C%0A%0A%20%20%20%20%60dayfirst%3DTrue%60%20is%20the%20shorter%20way%20to%20say%20%22day%20before%20month%22.%20Without%20either%2C%20pandas%20guesses%20the%20format%20from%20the%20**first**%20date%20in%20the%20column.%20Pick%20a%20column%20below%20to%20see%20what%20that%20guess%20does.%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_samples%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22the%20survey's%3A%20its%20first%20date%20is%2019%2F12%2F2025%22%3A%20%5B%2219%2F12%2F2025%22%2C%20%2230%2F11%2F2025%22%2C%20%2231%2F12%2F2025%22%5D%2C%0A%20%20%20%20%20%20%20%20%22every%20day%2012%20or%20less%3A%2009%2F11%2F2025%2C%2003%2F12%2F2025%2C%2011%2F10%2F2025%22%3A%20%5B%2209%2F11%2F2025%22%2C%20%2203%2F12%2F2025%22%2C%20%2211%2F10%2F2025%22%5D%2C%0A%20%20%20%20%20%20%20%20%22first%20date%2009%2F11%2F2025%2C%20then%2025%2F10%2F2025%22%3A%20%5B%2209%2F11%2F2025%22%2C%20%2225%2F10%2F2025%22%2C%20%2203%2F12%2F2025%22%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20date_column%20%3D%20mo.ui.dropdown(options%3Dlist(date_samples)%2C%20value%3D%22every%20day%2012%20or%20less%3A%2009%2F11%2F2025%2C%2003%2F12%2F2025%2C%2011%2F10%2F2025%22%2C%20label%3D%22A%20date%20column%22)%0A%20%20%20%20date_column%0A%20%20%20%20return%20date_column%2C%20date_samples%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(date_column%2C%20date_samples)%3A%0A%20%20%20%20_text%20%3D%20pd.Series(date_samples%5Bdate_column.value%5D)%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20_caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_guess%20%3D%20pd.to_datetime(_text).dt.strftime(%22%25d%20%25B%20%25Y%22).tolist()%0A%20%20%20%20%20%20%20%20except%20ValueError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_guess%20%3D%20%5Bf%22ValueError%3A%20%7Bstr(_error).splitlines()%5B0%5D%7D%22%5D%0A%20%20%20%20_right%20%3D%20pd.to_datetime(_text%2C%20format%3D%22%25d%2F%25m%2F%25Y%22).dt.strftime(%22%25d%20%25B%20%25Y%22).tolist()%0A%20%20%20%20_story%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22the%20survey's%3A%20its%20first%20date%20is%2019%2F12%2F2025%22%3A%20%22No%20month%20is%2019%2C%20so%20pandas%20guesses%20day%20first%2C%20and%20is%20right.%20It%20still%20warns%20(*UserWarning%3A%20Parsing%20dates%20in%20%25d%2F%25m%2F%25Y%20format%20when%20dayfirst%3DFalse%E2%80%A6*)%3A%20it%20was%20a%20guess.%22%2C%0A%20%20%20%20%20%20%20%20%22every%20day%2012%20or%20less%3A%2009%2F11%2F2025%2C%2003%2F12%2F2025%2C%2011%2F10%2F2025%22%3A%20%22Every%20date%20could%20be%20month%20first%2C%20so%20pandas%20reads%20them%20that%20way%3A%209%20November%20becomes%2011%20September.%20**No%20error%20and%20no%20warning.**%20This%20is%20the%20dangerous%20case.%22%2C%0A%20%20%20%20%20%20%20%20%22first%20date%2009%2F11%2F2025%2C%20then%2025%2F10%2F2025%22%3A%20%22The%20first%20date%20makes%20pandas%20guess%20month%20first%3B%20then%2025%2F10%2F2025%20has%20no%20month%2025%2C%20and%20the%20whole%20read%20fails.%20An%20error%20is%20better%20than%20a%20silent%20swap%2C%20but%20the%20format%20fixes%20both.%22%2C%0A%20%20%20%20%7D%5Bdate_column.value%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22in%20the%20file%22%3A%20_text%2C%20%22pd.to_datetime(column)%22%3A%20_guess%20if%20len(_guess)%20%3D%3D%20len(_text)%20else%20_guess%20*%20len(_text)%2C%20'format%3D%22%25d%2F%25m%2F%25Y%22'%3A%20_right%7D)%2C%0A%20%20%20%20%20%20%20%20mo.md(_story)%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%20You%20can%20convert%20after%20reading%2C%20or%20let%20the%20reader%20do%20it%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20table%20%3D%20pd.read_csv(path%2C%20dtype%3D%7B%22psgc%22%3A%20str%7D)%0A%20%20%20%20table%5B%22interview_date%22%5D%20%3D%20pd.to_datetime(table%5B%22interview_date%22%5D%2C%20format%3D%22%25d%2F%25m%2F%25Y%22)%0A%0A%20%20%20%20%23%20or%2C%20in%20one%20step%0A%20%20%20%20table%20%3D%20pd.read_csv(path%2C%20dtype%3D%7B%22psgc%22%3A%20str%7D%2C%20parse_dates%3D%5B%22interview_date%22%5D%2C%20date_format%3D%22%25d%2F%25m%2F%25Y%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Both%20give%20the%20type%20%60datetime64%5Bus%5D%60%3A%20a%20date%20and%20time%2C%20to%20the%20microsecond.%20Module%205%20does%20more%20with%20dates.%0A%0A%20%20%20%20%23%23%23%20Excel%3A%20sheets%20and%20header%20rows%0A%0A%20%20%20%20The%20same%20survey%20also%20comes%20as%20a%20workbook%2C%20the%20way%20an%20office%20shares%20it.%20A%20workbook%20has%20**sheets**%2C%20and%20people%20put%20titles%20above%20their%20tables.%20%60pd.ExcelFile(path).sheet_names%60%20lists%20the%20sheets%20without%20reading%20them%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%20workbook%20%3D%20PATHS.survey%20%2F%20%22farm_survey_2025.xlsx%22%0A%20%20%20%20pd.ExcelFile(workbook).sheet_names%0A%20%20%20%20return%20(workbook%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%60pd.read_excel(workbook)%60%20reads%20the%20**first**%20sheet%2C%20About%2C%20which%20is%20notes%2C%20not%20a%20table.%20%60sheet_name%3D%22Households%22%60%20picks%20the%20sheet.%20But%20the%20Households%20sheet%20has%20a%20title%2C%20a%20source%20line%20and%20a%20blank%20row%20above%20its%20column%20names%2C%20so%20pandas%20takes%20the%20title%20as%20the%20header%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(workbook)%3A%0A%20%20%20%20pd.read_excel(workbook%2C%20sheet_name%3D%22Households%22).head(4)%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%60header%3D%60%20says%20which%20row%20holds%20the%20column%20names%2C%20counting%20from%20**0**.%20The%20names%20are%20on%20Excel's%20row%204%2C%20so%20%60header%3D3%60%3B%20the%20rows%20above%20it%20are%20skipped.%20(%60header%3D2%60%20gives%20columns%20called%20%60Unnamed%3A%200%60%2C%20%E2%80%A6%3B%20%60header%3D4%60%20makes%20the%20first%20household%20the%20header%2C%20and%20that%20household%20is%20lost.)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(workbook)%3A%0A%20%20%20%20from_excel%20%3D%20pd.read_excel(workbook%2C%20sheet_name%3D%22Households%22%2C%20header%3D3)%0A%20%20%20%20mo.vstack(%5Bfrom_excel.head(3)%2C%20from_excel.dtypes.astype(str).to_frame(%22type%22).T%5D)%0A%20%20%20%20return%20(from_excel%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%20Two%20differences%20from%20the%20CSV.%20%60interview_date%60%20already%20reads%20as%20dates%2C%20because%20Excel%20stores%20dates%20as%20dates.%20And%20%60psgc%60%20is%20whole%20numbers%20again%2C%20and%20this%20time%20%60dtype%3D%7B%22psgc%22%3A%20str%7D%60%20does%20not%20help%3A%20the%20workbook%20stored%20the%20codes%20**as%20numbers**%2C%20so%20the%20zero%20was%20never%20in%20the%20file.%20Excel%20only%20showed%20it%2C%20through%20a%20cell%20format.%20%60dtype%3Dstr%60%20gives%20%60%22102820000%22%60%2C%20nine%20digits.%0A%0A%20%20%20%20Every%20code%20has%20ten%20digits%2C%20so%20the%20zero%20can%20be%20put%20back%3A%20turn%20the%20numbers%20into%20text%20with%20%60.astype(str)%60%2C%20then%20%60.str.zfill(10)%60%20pads%20each%20text%20on%20the%20left%20with%20zeros%20to%20ten%20characters.%20A%20code%20that%20already%20has%20ten%20digits%20is%20left%20as%20it%20is.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(from_excel)%3A%0A%20%20%20%20_codes%20%3D%20from_excel%5B%22psgc%22%5D.head(3)%0A%20%20%20%20pd.DataFrame(%7B%22in%20the%20workbook%22%3A%20_codes%2C%20%22.astype(str)%22%3A%20_codes.astype(str)%2C%20%22.astype(str).str.zfill(10)%22%3A%20_codes.astype(str).str.zfill(10)%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%20To%20read%20several%20sheets%20that%20share%20a%20layout%20at%20once%2C%20give%20a%20list%3A%20%60pd.read_excel(workbook%2C%20sheet_name%3D%5B%22Households%22%2C%20%22Plots%22%5D%2C%20header%3D3)%60%20returns%20a%20dictionary%20of%20tables%2C%20one%20per%20sheet.%20The%20Codebook%20sheet%20has%20no%20title%20rows%2C%20so%20it%20is%20read%20on%20its%20own%20with%20the%20default%20%60header%3D0%60.%0A%0A%20%20%20%20%23%23%23%20Writing%3A%20CSV%2C%20Excel%20and%20Parquet%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20it%20makes%20%7C%20notes%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60table.to_csv(path%2C%20index%3DFalse)%60%20%7C%20a%20CSV%20file%20%7C%20any%20program%20reads%20it%3B%20every%20type%20is%20lost%20%7C%0A%20%20%20%20%7C%20%60table.to_excel(path%2C%20sheet_name%3D%22Households%22%2C%20index%3DFalse)%60%20%7C%20an%20Excel%20workbook%20%7C%20for%20people%20who%20work%20in%20Excel%3B%20several%20sheets%20need%20%60pd.ExcelWriter%60%20%7C%0A%20%20%20%20%7C%20%60table.to_parquet(path)%60%20%7C%20a%20Parquet%20file%20%7C%20a%20column%20format%20that%20stores%20each%20column's%20type%20with%20it%3B%20smaller%20and%20faster%20%7C%0A%0A%20%20%20%20%60index%3DFalse%60%20leaves%20out%20the%20row%20numbers.%20Without%20it%2C%20they%20are%20written%20as%20a%20first%20column%2C%20which%20comes%20back%20as%20%60Unnamed%3A%200%60%20when%20the%20file%20is%20read%20again.%0A%0A%20%20%20%20Which%20types%20survive%20a%20trip%20out%20and%20back%3F%20Pick%20a%20format%3A%20the%20cell%20writes%20the%20clean%20households%20(codes%20as%20text%2C%20dates%20as%20dates)%20to%20a%20temporary%20folder%2C%20reads%20the%20file%20back%20with%20no%20options%2C%20and%20compares.%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%20file_format%20%3D%20mo.ui.dropdown(options%3D%5B%22CSV%22%2C%20%22Excel%22%2C%20%22Parquet%22%5D%2C%20value%3D%22CSV%22%2C%20label%3D%22Write%20the%20households%20as%22)%0A%20%20%20%20file_format%0A%20%20%20%20return%20(file_format%2C)%0A%0A%0A%40app.cell%0Adef%20_(file_format)%3A%0A%20%20%20%20%23%20Write%20the%20clean%20households%2C%20read%20the%20file%20back%20with%20no%20options%2C%20and%20compare%20the%20types%0A%20%20%20%20_households%20%3D%20course.households()%0A%20%20%20%20if%20file_format.value%20%3D%3D%20%22CSV%22%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20SCRATCH%20%2F%20%22households.csv%22%0A%20%20%20%20%20%20%20%20_households.to_csv(_path%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20_back%20%3D%20pd.read_csv(_path)%0A%20%20%20%20elif%20file_format.value%20%3D%3D%20%22Excel%22%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20SCRATCH%20%2F%20%22households.xlsx%22%0A%20%20%20%20%20%20%20%20_households.to_excel(_path%2C%20sheet_name%3D%22Households%22%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20_back%20%3D%20pd.read_excel(_path%2C%20sheet_name%3D%22Households%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20SCRATCH%20%2F%20%22households.parquet%22%0A%20%20%20%20%20%20%20%20_households.to_parquet(_path)%0A%20%20%20%20%20%20%20%20_back%20%3D%20pd.read_parquet(_path)%0A%20%20%20%20_first%20%3D%20_back%5B%22psgc%22%5D.iloc%5B0%5D%0A%20%20%20%20_first_code%20%3D%20f%22the%20text%20%60%7B_first%7D%60%22%20if%20isinstance(_first%2C%20str)%20else%20f%22the%20number%20%60%7B_first%7D%60%22%0A%20%20%20%20_types%20%3D%20pd.DataFrame(%7B%22type%20written%22%3A%20_households.dtypes.astype(str)%2C%20%22type%20read%20back%22%3A%20_back.dtypes.astype(str)%7D)%0A%20%20%20%20_types%5B%22kept%22%5D%20%3D%20np.where(_types%5B%22type%20written%22%5D%20%3D%3D%20_types%5B%22type%20read%20back%22%5D%2C%20%22yes%22%2C%20%22no%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%7B_path.name%7D%60%3A%20%7B_path.stat().st_size%20%2F%201024%3A%2C.0f%7D%20KB.%20The%20first%20code%20was%20written%20as%20the%20text%20%600102820000%60%3B%20it%20reads%20back%20as%20%7B_first_code%7D.%22)%2C%0A%20%20%20%20%20%20%20%20_types%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%7C%20format%20%7C%20codes%20%7C%20dates%20%7C%20use%20it%20for%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20CSV%20%7C%20lose%20their%20zero%20again%20%7C%20come%20back%20as%20text%20%7C%20sharing%20with%20anyone%20and%20any%20program%3B%20read%20it%20with%20the%20fixes%20above%20every%20time%20%7C%0A%20%20%20%20%7C%20Excel%20%7C%20lose%20their%20zero%20again%2C%20even%20though%20they%20were%20written%20as%20text%20%7C%20kept%20%7C%20people%20who%20will%20open%20the%20file%20in%20Excel%20%7C%0A%20%20%20%20%7C%20Parquet%20%7C%20kept%20%7C%20kept%20%7C%20your%20own%20work%20between%20notebooks%3A%20what%20you%20read%20back%20is%20what%20you%20wrote%20%7C%0A%0A%20%20%20%20A%20CSV%20or%20Excel%20file%20does%20not%20remember%20that%20a%20column%20was%20text%3B%20the%20next%20reader%20guesses%20again.%20That%20is%20why%20reading%20with%20the%20right%20types%20is%20a%20habit%2C%20not%20a%20one-off%20fix.%20You%20will%20write%20your%20own%20reading%20functions%20now.%0A%0A%20%20%20%20%23%23%23%20Exercise%203.3.%20Read%20the%20households%20with%20their%20types%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20analysis%20of%20the%20households%20starts%20with%20this%20read.%20If%20the%20codes%20lose%20their%20zeros%20here%2C%20joins%20fail%20later%3B%20if%20the%20dates%20are%20swapped%20here%2C%20every%20month%20is%20wrong%20later%2C%20with%20no%20error%20to%20tell%20you.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20a%20households%20CSV%20in%20the%20survey's%20layout%20%E2%86%92%20a%20table%20with%20every%20column%20of%20the%20file%2C%20where%20%60psgc%60%20is%20**text**%20(ten%20digits%2C%20zeros%20kept)%20and%20%60interview_date%60%20is%20**dates**%2C%20read%20day%20first.%20The%20other%20columns%20keep%20the%20types%20pandas%20gives%20them.%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%60psgc%60%20%7C%20%600102820000%60%20%7C%20%60%220102820000%22%60%2C%20type%20%60str%60%20%7C%0A%20%20%20%20%7C%20%60interview_date%60%20%7C%20%6009%2F11%2F2025%60%20%7C%209%20November%202025%2C%20type%20%60datetime64%5Bus%5D%60%20%7C%0A%20%20%20%20%7C%20%60hh_size%60%20%7C%20%604%60%20%7C%20%604%60%2C%20type%20%60int64%60%20%7C%0A%0A%20%20%20%20The%20check%20reads%20the%20survey's%20file%20and%20two%20small%20files%20whose%20dates%20are%20harder%20to%20guess.%20**Tools%3A**%20%60pd.read_csv%60%20with%20%60dtype%3D%60%20(codes%20as%20text%2C%20above)%3B%20%60pd.to_datetime%60%20with%20%60format%3D%60%20or%20%60dayfirst%3DTrue%60%2C%20or%20%60parse_dates%3D%60%20with%20%60date_format%3D%60%20(dates%2C%20above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_households(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20file%2C%20with%20psgc%20as%20text%20and%20interview_date%20as%20dates%20(day%20first).%22%22%22%0A%20%20%20%20%23%20Exercise%203.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20read%20the%20file%20with%20pd.read_csv%2C%20telling%20it%20that%20psgc%20is%20text%0A%20%20%20%20%23%20Step%202%3A%20turn%20interview_date%20into%20dates%2C%20day%20first%20(give%20the%20format)%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.3%22)%0A%0A%0A%40app.function%0Adef%20column_kinds(table%3A%20pd.DataFrame)%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%20the%20reading%20exercises%3A%20like%0A%20%20%20%20course.with_dtypes%2C%20except%20that%20every%20type%20of%20text%20counts%20as%20text%20(%22str%22%20or%20%22string%22)%2C%20every%20whole-number%20type%20as%20a%0A%20%20%20%20whole%20number%20and%20every%20date%20type%20as%20dates%2C%20so%20any%20correct%20way%20of%20reading%20passes.%20A%20column%20of%20Python%20objects%20that%0A%20%20%20%20are%20all%20text%20(dtype%3Dobject)%20counts%20as%20text%20too%3A%20its%20values%20are%20right%2C%20even%20if%20%60str%60%20is%20the%20type%20to%20prefer.%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%20return%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%7D%0A%0A%0A%40app.function%0Adef%20households_fixture()%20-%3E%20dict%5Bstr%2C%20Path%5D%3A%0A%20%20%20%20%22%22%22Two%20small%20households%20files%20for%20the%20check%20of%20Exercise%203.3%3A%20one%20whose%20days%20are%20all%2012%20or%20less%20(pandas%20would%20guess%0A%20%20%20%20month%20first)%2C%20and%20one%20whose%20first%20date%20has%20a%20day%20of%2012%20or%20less%20and%20a%20later%20one%20a%20day%20above%2012.%22%22%22%0A%20%20%20%20header%20%3D%20%22hh_id%2Cprovince%2Cpsgc%2Cinterview_date%2Chh_size%2Chead_sex%2Chead_age%2Cweight%5Cn%22%0A%20%20%20%20files%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22ambiguous%22%3A%20header%20%2B%20%22HH-0101%2CIlocos%20Norte%2C0102820000%2C09%2F11%2F2025%2C4%2C1%2C51%2C301.2%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%22HH-0102%2CIsabela%2C0203130000%2C03%2F12%2F2025%2C6%2C2%2C38%2C288%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%22HH-0103%2CBukidnon%2C1001317000%2C11%2F10%2F2025%2C3%2C1%2C62%2C410.75%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%22HH-0104%2CNueva%20Ecija%2C0304903000%2C12%2F12%2F2025%2C5%2C1%2C45%2C512.3%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%22mixed%22%3A%20header%20%2B%20%22HH-0201%2CIloilo%2C0603016000%2C09%2F11%2F2025%2C2%2C2%2C70%2C198.4%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%22HH-0202%2CIloilo%2C0603016000%2C25%2F10%2F2025%2C7%2C1%2C33%2C220%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%22HH-0203%2CCamarines%20Sur%2C0501702000%2C03%2F12%2F2025%2C4%2C9%2C999%2C305.1%5Cn%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20paths%20%3D%20%7B%7D%0A%20%20%20%20for%20name%2C%20text%20in%20files.items()%3A%0A%20%20%20%20%20%20%20%20paths%5Bname%5D%20%3D%20SCRATCH%20%2F%20f%22households_%7Bname%7D.csv%22%0A%20%20%20%20%20%20%20%20paths%5Bname%5D.write_text(text%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20return%20paths%0A%0A%0A%40app.function%0Adef%20try_reading(function%2C%20path%3A%20Path)%3A%0A%20%20%20%20%22%22%22Call%20a%20reading%20exercise%20on%20a%20file%2C%20and%20show%20the%20type%20of%20each%20column%2C%20then%20the%20first%20rows.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20function(path)%0A%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20return%20try_it(function%2C%20path)%20%20%23%20explains%20a%20%22not%20written%20yet%22%20or%20an%20error%0A%20%20%20%20if%20not%20isinstance(table%2C%20pd.DataFrame)%3A%0A%20%20%20%20%20%20%20%20return%20try_it(function%2C%20path)%0A%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%7Bfunction.__name__%7D('%7Bpath.name%7D')%60%20gave%20back%20**a%20table%20of%20%7Blen(table)%3A%2C%7D%20rows**.%20The%20type%20of%20each%20column%2C%20then%20the%20first%20rows%3A%22)%2C%0A%20%20%20%20%20%20%20%20table.dtypes.astype(str).to_frame(%22type%22).T%2C%0A%20%20%20%20%20%20%20%20table.head(5)%2C%0A%20%20%20%20%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_files%20%3D%20households_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.3%22%2C%20read_households%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%20survey's%20households.csv%22%2C%20(PATHS.survey%20%2F%20%22households.csv%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20households%20whose%20interview%20days%20are%20all%2012%20or%20less%22%2C%20(_files%5B%22ambiguous%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20households%3A%20the%20first%20interviewed%20on%2009%2F11%2C%20the%20second%20on%2025%2F10%22%2C%20(_files%5B%22mixed%22%5D%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%22The%20check%20compares%20each%20column's%20kind%20(text%2C%20whole%20numbers%2C%20dates%E2%80%A6)%20and%20every%20value.%20In%20the%20second%20file%2C%20every%20date%20could%20also%20be%20read%20month%20first.%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%20read%20the%20survey's%20households%2C%20and%20look%20at%20the%20types%3A%20psgc%20should%20be%20str%2C%20interview_date%20datetime64%5Bus%5D.%0A%20%20%20%20try_reading(read_households%2C%20PATHS.survey%20%2F%20%22households.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%203.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60psgc%60%20is%20%60int64%60%2C%20and%20codes%20have%20nine%20digits%20%7C%20The%20codes%20were%20read%20as%20numbers.%20%7C%20%60dtype%3D%7B%22psgc%22%3A%20str%7D%60%20%7C%0A%20%20%20%20%7C%20%60interview_date%60%20is%20%60str%60%20%7C%20The%20dates%20were%20not%20converted.%20%7C%20%60pd.to_datetime(...)%60%20%7C%0A%20%20%20%20%7C%20The%20first%20file%20is%20right%3B%20the%20second%20has%20dates%20in%20September%20%7C%20The%20dates%20were%20read%20month%20first%3A%20no%20format%2C%20and%20no%20%60dayfirst%3DTrue%60.%20%7C%20%60format%3D%22%25d%2F%25m%2F%25Y%22%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20time%20data%20%2225%2F10%2F2025%22%20doesn't%20match%20format%20%22%25m%2F%25d%2F%25Y%22*%20%7C%20The%20same%3A%20pandas%20guessed%20month%20first%20from%20the%20first%20date.%20%7C%20%60format%3D%22%25d%2F%25m%2F%25Y%22%60%20%7C%0A%20%20%20%20%7C%20%60hh_size%60%20or%20%60weight%60%20is%20%60str%60%20%7C%20%60dtype%3Dstr%60%20made%20every%20column%20text.%20%7C%20name%20only%20%60psgc%60%20in%20%60dtype%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%20Exercise%203.4.%20Read%20the%20plots%20with%20numbers%20as%20numbers%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20production%20is%20what%20yields%20are%20made%20from.%20Read%20as%20text%2C%20it%20cannot%20be%20divided%20or%20averaged%2C%20and%20a%20sum%20gives%20one%20long%20text%20instead%20of%20a%20total.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20a%20plots%20CSV%20in%20the%20survey's%20layout%20%E2%86%92%20a%20table%20with%20every%20column%20of%20the%20file%2C%20where%20every%20number%20is%20a%20number.%20The%20survey%20system%20writes%20**any**%20number%20of%201%2C000%20or%20more%20with%20a%20comma%2C%20in%20whichever%20column%20it%20is%3A%20production%2C%20and%20sometimes%20an%20area%20in%20square%20meters%20or%20a%20fertilizer%20amount.%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%60production_kg%60%20%7C%20%60%221%2C640%22%60%20%7C%20%601640%60%2C%20whole%20numbers%20%7C%0A%20%20%20%20%7C%20%60area%60%20%7C%20%60%222%2C500%22%60%20(square%20meters)%20%7C%20%602500.0%60%2C%20decimal%20numbers%20%7C%0A%20%20%20%20%7C%20%60crop%60%20%7C%20(blank)%20%7C%20%60NaN%60%3A%20not%20recorded%20%7C%0A%0A%20%20%20%20The%20check%20reads%20the%20survey's%20file%20and%20two%20small%20files%3A%20one%20with%20commas%20in%20other%20columns%20too%2C%20and%20one%20whose%20numbers%20are%20all%20under%201%2C000.%20**Tools%3A**%20%60pd.read_csv%60%20with%20%60thousands%3D%22%2C%22%60%20(thousands%20separators%2C%20above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_plots(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20file%2C%20with%20every%20number%20written%20with%20a%20thousands%20separator%20read%20as%20a%20number.%22%22%22%0A%20%20%20%20%23%20Exercise%203.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20read%20the%20file%20with%20pd.read_csv%2C%20telling%20it%20that%20a%20comma%20in%20a%20number%20separates%20thousands%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.4%22)%0A%0A%0A%40app.function%0Adef%20plots_fixture()%20-%3E%20dict%5Bstr%2C%20Path%5D%3A%0A%20%20%20%20%22%22%22Two%20small%20plots%20files%20for%20the%20check%20of%20Exercise%203.4%3A%20one%20with%20separators%20in%20area%20and%20fertilizer%20too%2C%20one%20with%0A%20%20%20%20every%20number%20under%201%2C000%20(so%20no%20separators%20at%20all).%22%22%22%0A%20%20%20%20header%20%3D%20%22hh_id%2Cplot_no%2Ccrop%2Carea%2Carea_unit%2Cirrigated%2Cproduction_kg%2Cfertilizer_kg%5Cn%22%0A%20%20%20%20files%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22separators%22%3A%20header%20%2B%20'HH-0101%2C1%2CRice%2C0.62%2Cha%2CYes%2C%222%2C480%22%2C88.5%5Cn'%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'HH-0101%2C2%2CCorn%2C%222%2C500%22%2Csqm%2CNo%2C410%2C%5Cn'%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%22HH-0102%2C1%2C%2C0.35%2Cha%2CNo%2C%5C%221%2C050%5C%22%2C40%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'HH-0103%2C1%2CBanana%2C2.4%2Cha%2CNo%2C%2252%2C600%22%2C%221%2C120%22%5Cn'%2C%0A%20%20%20%20%20%20%20%20%22small%22%3A%20header%20%2B%20%22HH-0201%2C1%2CVegetables%2C640%2Csqm%2CYes%2C380%2C12.5%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%22HH-0201%2C2%2CCorn%2C0.21%2Cha%2CNo%2C720%2C%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%22HH-0202%2C1%2CRice%2C0.18%2Cha%2CYes%2C810%2C22%5Cn%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20paths%20%3D%20%7B%7D%0A%20%20%20%20for%20name%2C%20text%20in%20files.items()%3A%0A%20%20%20%20%20%20%20%20paths%5Bname%5D%20%3D%20SCRATCH%20%2F%20f%22plots_%7Bname%7D.csv%22%0A%20%20%20%20%20%20%20%20paths%5Bname%5D.write_text(text%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20return%20paths%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_files%20%3D%20plots_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.4%22%2C%20read_plots%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%20survey's%20plots.csv%22%2C%20(PATHS.survey%20%2F%20%22plots.csv%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20plots%2C%20with%20commas%20in%20area%20and%20fertilizer%20too%22%2C%20(_files%5B%22separators%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20plots%20whose%20numbers%20are%20all%20under%201%2C000%22%2C%20(_files%5B%22small%22%5D%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%22The%20check%20compares%20each%20column's%20kind%20(text%2C%20whole%20numbers%E2%80%A6)%20and%20every%20value.%20The%20second%20file%20has%20commas%20in%20area%20and%20fertilizer_kg%20as%20well%20as%20in%20production_kg.%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%20read%20the%20survey's%20plots.%20production_kg%20should%20be%20int64.%0A%20%20%20%20try_reading(read_plots%2C%20PATHS.survey%20%2F%20%22plots.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%203.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60production_kg%60%20is%20%60str%60%20%7C%20The%20file%20was%20read%20without%20%60thousands%3D%22%2C%22%60.%20%7C%20%60pd.read_csv(path%2C%20thousands%3D%22%2C%22)%60%20%7C%0A%20%20%20%20%7C%20The%20first%20file%20is%20right%3B%20in%20the%20second%2C%20%60area%60%20or%20%60fertilizer_kg%60%20is%20%60str%60%20%7C%20Only%20%60production_kg%60%20had%20its%20commas%20removed.%20%7C%20%60thousands%3D%22%2C%22%60%20covers%20every%20column%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20Can%20only%20use%20.str%20accessor%20with%20string%20values*%20on%20the%20third%20file%20%7C%20%60.str.replace%60%20on%20a%20column%20that%20has%20no%20commas%2C%20so%20it%20is%20already%20numbers.%20%7C%20%60thousands%3D%22%2C%22%60%20instead%20%7C%0A%20%20%20%20%7C%20Blank%20crops%20became%20%60%22%22%60%20or%20another%20text%20%7C%20The%20blanks%20were%20filled.%20%7C%20read%20with%20the%20defaults%3A%20a%20blank%20is%20%60NaN%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%203.5.%20Read%20the%20households%20from%20the%20workbook%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20office%20that%20works%20in%20Excel%20sends%20a%20workbook%2C%20not%20a%20CSV.%20The%20same%20survey%20arrives%20with%20titles%20above%20the%20table%20and%20codes%20stored%20as%20numbers%2C%20and%20your%20table%20must%20come%20out%20the%20same%20as%20from%20the%20CSV.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20a%20workbook%20laid%20out%20like%20%60farm_survey_2025.xlsx%60%20(sheets%20About%2C%20Households%2C%20%E2%80%A6%3B%20three%20title%20rows%20above%20each%20table)%20%E2%86%92%20the%20**Households**%20sheet%20as%20a%20table%2C%20with%20%60psgc%60%20as%20**ten-digit%20text**.%20The%20dates%20are%20already%20dates%20in%20the%20workbook.%0A%0A%20%20%20%20%7C%20in%20the%20workbook%20%7C%20in%20your%20table%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20row%204%3A%20%60hh_id%60%2C%20%60province%60%2C%20%60psgc%60%2C%20%E2%80%A6%20%7C%20the%20column%20names%20%7C%0A%20%20%20%20%7C%20%60102820000%60%20(a%20number%2C%20shown%20as%20%600102820000%60)%20%7C%20%60%220102820000%22%60%20%7C%0A%20%20%20%20%7C%20%601001317000%60%20(ten%20digits%20already)%20%7C%20%60%221001317000%22%60%20%7C%0A%0A%20%20%20%20The%20check%20reads%20the%20survey's%20workbook%20and%20a%20small%20one%20of%20the%20same%20layout.%20**Tools%3A**%20%60pd.read_excel%60%20with%20%60sheet_name%3D%60%20and%20%60header%3D%60%3B%20%60.astype(str).str.zfill(10)%60%20(Excel%2C%20above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_workbook_households(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20Households%20sheet%20of%20the%20survey's%20workbook%2C%20with%20psgc%20as%20ten-digit%20text.%22%22%22%0A%20%20%20%20%23%20Exercise%203.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20read%20the%20Households%20sheet%3B%20the%20column%20names%20are%20on%20Excel's%20row%204%20(count%20from%200)%0A%20%20%20%20%23%20Step%202%3A%20turn%20psgc%20into%20text%20and%20pad%20it%20on%20the%20left%20with%20zeros%20to%2010%20characters%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.5%22)%0A%0A%0A%40app.function%0Adef%20workbook_fixture()%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22A%20small%20workbook%20laid%20out%20like%20the%20survey's%20(an%20About%20sheet%20first%2C%20three%20title%20rows%20above%20each%20table%2C%20codes%0A%20%20%20%20stored%20as%20numbers)%2C%20for%20the%20check%20of%20Exercise%203.5.%22%22%22%0A%20%20%20%20path%20%3D%20SCRATCH%20%2F%20%22survey_small.xlsx%22%0A%20%20%20%20households%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22HH-0301%22%2C%20%22HH-0302%22%2C%20%22HH-0303%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Bukidnon%22%2C%20%22Ilocos%20Norte%22%2C%20%22Cotabato%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psgc%22%3A%20%5B1001317000%2C%20102805000%2C%201204709000%5D%2C%0A%20%20%20%20%20%20%20%20%22interview_date%22%3A%20pd.to_datetime(%5B%222025-10-02%22%2C%20%222025-11-18%22%2C%20%222025-12-05%22%5D)%2C%0A%20%20%20%20%20%20%20%20%22hh_size%22%3A%20%5B5%2C%203%2C%206%5D%2C%0A%20%20%20%20%20%20%20%20%22head_sex%22%3A%20%5B1%2C%202%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22head_age%22%3A%20%5B44%2C%2059%2C%20999%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B233.4%2C%20301.0%2C%20275.25%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20plots%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0301%22%2C%20%22HH-0302%22%5D%2C%20%22plot_no%22%3A%20%5B1%2C%201%5D%2C%20%22crop%22%3A%20%5B%22Corn%22%2C%20%22Rice%22%5D%2C%20%22area%22%3A%20%5B1.1%2C%200.4%5D%7D)%0A%20%20%20%20with%20pd.ExcelWriter(path)%20as%20writer%3A%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22About%22%3A%20%5B%22A%20small%20survey%20workbook%2C%20for%20the%20check%20of%20Exercise%203.5.%22%5D%7D).to_excel(writer%2C%20sheet_name%3D%22About%22%2C%20index%3DFalse%2C%20header%3DFalse)%0A%20%20%20%20%20%20%20%20for%20name%2C%20table%20in%20%5B(%22Households%22%2C%20households)%2C%20(%22Plots%22%2C%20plots)%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20table.to_excel(writer%2C%20sheet_name%3Dname%2C%20startrow%3D3%2C%20index%3DFalse)%20%20%23%20the%20header%20on%20Excel's%20row%204%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%20%3D%20writer.sheets%5Bname%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%5B%22A1%22%5D%20%3D%20f%22Table%3A%20%7Bname%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%5B%22A2%22%5D%20%3D%20%22Source%3A%20a%20small%20survey%20workbook%20(made%20up%20for%20this%20check)%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%203.5%22%2C%20read_workbook_households%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%20survey's%20farm_survey_2025.xlsx%22%2C%20(PATHS.survey%20%2F%20%22farm_survey_2025.xlsx%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20workbook%20of%20three%20households%2C%20one%20code%20with%20ten%20digits%20already%22%2C%20(workbook_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22workbooks%22%2C%20summarize%3Dcolumn_kinds%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20compares%20each%20column's%20kind%20and%20every%20value%3A%20the%20column%20names%2C%20the%20number%20of%20households%2C%20and%20psgc%20as%20ten-digit%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%20read%20the%20survey's%20workbook.%20Compare%20with%20the%20CSV%3A%201%2C196%20rows%2C%20psgc%20str%20with%20ten%20digits.%0A%20%20%20%20try_reading(read_workbook_households%2C%20PATHS.survey%20%2F%20%22farm_survey_2025.xlsx%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.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%20One%20column%2C%20named%20after%20the%20survey's%20title%20%7C%20The%20first%20sheet%20(About)%20was%20read.%20%7C%20%60sheet_name%3D%22Households%22%60%20%7C%0A%20%20%20%20%7C%20Columns%20called%20%60Unnamed%3A%200%60%2C%20%60Unnamed%3A%201%60%2C%20%E2%80%A6%20%7C%20The%20header%20row%20is%20wrong%3A%20the%20default%2C%20or%20%60header%3D2%60.%20%7C%20%60header%3D3%60%20%7C%0A%20%20%20%20%7C%20The%20columns%20are%20household%20values%20(%60HH-0001%60%2C%20%E2%80%A6)%20and%20a%20household%20is%20missing%20%7C%20%60header%3D4%60%3A%20the%20first%20household%20became%20the%20header.%20%7C%20%60header%3D3%60%20%7C%0A%20%20%20%20%7C%20%60psgc%60%20is%20text%20with%20nine%20digits%20%7C%20The%20codes%20were%20made%20text%20without%20the%20zeros.%20%7C%20%60.str.zfill(10)%60%20%7C%0A%20%20%20%20%7C%20%60psgc%60%20is%20right%20for%20the%20survey%2C%20but%20the%20small%20workbook%20has%20an%20eleven-digit%20code%20%7C%20A%20%60%220%22%60%20was%20added%20in%20front%20of%20every%20code.%20%7C%20%60.str.zfill(10)%60%20pads%20only%20short%20codes%20%7C%0A%20%20%20%20%7C%20%60psgc%60%20is%20%60int64%60%20%7C%20The%20codes%20were%20left%20as%20numbers.%20%7C%20%60.astype(str).str.zfill(10)%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%20Selecting%2C%20filtering%20and%20sorting%0A%0A%20%20%20%20An%20extension%20office%20asks%3A%20*which%20rice%20plots%20that%20are%20not%20irrigated%20harvested%20the%20least%2C%20among%20those%20of%20half%20a%20hectare%20to%20a%20hectare%3F*%20No%20new%20numbers%20are%20needed%2C%20only%20the%20right%20part%20of%20the%20table%2C%20in%20the%20right%20order%3A%20some%20**rows**%20(rice%2C%20not%20irrigated%2C%20that%20size)%2C%20some%20**columns**%20(who%2C%20which%20plot%2C%20how%20much)%2C%20and%20an%20**order**%20(least%20harvest%20first).%20This%20section%20uses%20%60course.plots()%60%2C%20the%20plots%20with%20the%20right%20types%2C%20duplicates%20dropped%2C%20and%20%60area_ha%60%2C%20every%20area%20in%20hectares%20(section%204%20shows%20how%20%60area_ha%60%20is%20made).%0A%0A%20%20%20%20%23%23%23%20Columns%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20you%20get%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60plots%5B%22crop%22%5D%60%20%7C%20one%20column%2C%20as%20a%20Series%20%7C%0A%20%20%20%20%7C%20%60plots%5B%5B%22hh_id%22%2C%20%22crop%22%5D%5D%60%20%7C%20several%20columns%2C%20as%20a%20DataFrame%3A%20the%20inner%20brackets%20are%20a%20list%20of%20names%20%7C%0A%20%20%20%20%7C%20%60plots.columns%60%20%7C%20the%20column%20names%20%7C%0A%0A%20%20%20%20%23%23%23%20Rows%20by%20condition%0A%0A%20%20%20%20A%20comparison%20on%20a%20column%20gives%20a%20Series%20of%20%60True%60%20and%20%60False%60%20with%20the%20table's%20index%3A%20a%20**boolean%20mask**%2C%20as%20in%20module%202.%20Put%20the%20mask%20in%20brackets%20to%20keep%20the%20rows%20where%20it%20is%20%60True%60.%20The%20rows%20keep%20their%20labels%2C%20so%20you%20can%20see%20which%20rows%20of%20the%20table%20they%20were.%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%20plots%20%3D%20course.plots()%0A%20%20%20%20is_rice%20%3D%20plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%20%20%23%20True%20or%20False%20for%20each%20plot%0A%20%20%20%20plots%5Bis_rice%5D.head()%0A%20%20%20%20return%20is_rice%2C%20plots%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%20Combining%20conditions%3A%20%26%2C%20%7C%20and%20~%2C%20each%20in%20brackets%0A%0A%20%20%20%20%7C%20operator%20%7C%20means%20%7C%20example%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%26%60%20%7C%20and%3A%20both%20are%20true%20%7C%20%60(plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22)%20%26%20(plots%5B%22irrigated%22%5D%20%3D%3D%20%22No%22)%60%20%7C%0A%20%20%20%20%7C%20%3Ccode%3E%26%23124%3B%3C%2Fcode%3E%20%7C%20or%3A%20either%20is%20true%20%7C%20%3Ccode%3E(plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22)%20%26%23124%3B%20(plots%5B%22crop%22%5D%20%3D%3D%20%22Corn%22)%3C%2Fcode%3E%20%7C%0A%20%20%20%20%7C%20%60~%60%20%7C%20not%3A%20the%20opposite%20%7C%20%60~(plots%5B%22irrigated%22%5D%20%3D%3D%20%22Yes%22)%60%20%7C%0A%0A%20%20%20%20Python's%20%60and%60%2C%20%60or%60%20and%20%60not%60%20(from%20Python%20Essentials)%20ask%20one%20question%20of%20one%20value.%20A%20mask%20holds%20a%20thousand%20values%2C%20so%20pandas%20uses%20%60%26%60%2C%20%60%7C%60%20and%20%60~%60%2C%20which%20work%20value%20by%20value.%20And%20each%20condition%20needs%20its%20own%20brackets%3A%20%60%26%60%20is%20applied%20**before**%20%60%3D%3D%60%2C%20so%20without%20brackets%20Python%20first%20tries%20%60%22Rice%22%20%26%20plots%5B%22irrigated%22%5D%60.%20The%20cell%20below%20tries%20three%20ways%20of%20writing%20the%20same%20filter%3B%20read%20each%20error%20from%20its%20last%20line.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots)%3A%0A%20%20%20%20%23%20Three%20ways%20to%20write%20%22rice%20plots%20that%20are%20not%20irrigated%22.%20The%20first%20two%20raise%20errors%3B%20here%20is%20each%20one's%20message.%0A%20%20%20%20_ways%20%3D%20%7B%0A%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20and%20(irrigated%20%3D%3D%20%22No%22)'%3A%20lambda%3A%20plots%5B(plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22)%20and%20(plots%5B%22irrigated%22%5D%20%3D%3D%20%22No%22)%5D%2C%0A%20%20%20%20%20%20%20%20'crop%20%3D%3D%20%22Rice%22%20%26%20irrigated%20%3D%3D%20%22No%22'%3A%20lambda%3A%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%20%26%20plots%5B%22irrigated%22%5D%20%3D%3D%20%22No%22%5D%2C%0A%20%20%20%20%20%20%20%20'(crop%20%3D%3D%20%22Rice%22)%20%26%20(irrigated%20%3D%3D%20%22No%22)'%3A%20lambda%3A%20plots%5B(plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22)%20%26%20(plots%5B%22irrigated%22%5D%20%3D%3D%20%22No%22)%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_written%2C%20_way%20in%20_ways.items()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_rows.append(%7B%22written%20as%22%3A%20_written%2C%20%22what%20happens%22%3A%20f%22%7Blen(_way())%3A%2C%7D%20plots%22%7D)%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20_error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_rows.append(%7B%22written%20as%22%3A%20_written%2C%20%22what%20happens%22%3A%20f%22%7Btype(_error).__name__%7D%3A%20%7B_error%7D%22%7D)%0A%20%20%20%20pd.DataFrame(_rows)%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%20the%20error%20%7C%20what%20it%20means%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous%60%20%7C%20%60and%60%20asked%20whether%20a%20whole%20Series%20is%20true%2C%20which%20has%20no%20single%20answer%3A%20use%20%60%26%60%20%7C%0A%20%20%20%20%7C%20%60TypeError%3A%20operation%20'rand_'%20not%20supported%20for%20dtype%20'str'%20with%20object%20of%20type%20%3Cclass%20'str'%3E%60%20%7C%20%60%26%60%20was%20applied%20to%20the%20text%20%60%22Rice%22%60%20and%20a%20text%20column%20(%60rand_%60%20is%20pandas'%20name%20for%20%60%26%60%20with%20the%20column%20on%20the%20right)%3A%20add%20the%20brackets%20%7C%0A%0A%20%20%20%20%23%23%23%20Shorter%20conditions%3A%20isin%2C%20between%20and%20query%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20keeps%20the%20rows%20where%20%7C%20instead%20of%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60plots%5B%22crop%22%5D.isin(%5B%22Rice%22%2C%20%22Corn%22%5D)%60%20%7C%20the%20crop%20is%20one%20of%20those%20in%20the%20list%20%7C%20%3Ccode%3E(crop%20%3D%3D%20%22Rice%22)%20%26%23124%3B%20(crop%20%3D%3D%20%22Corn%22)%3C%2Fcode%3E%20%7C%0A%20%20%20%20%7C%20%60plots%5B%22area_ha%22%5D.between(0.5%2C%201)%60%20%7C%20the%20area%20is%20from%200.5%20to%201%2C%20**both%20ends%20included**%20%7C%20%60(area_ha%20%3E%3D%200.5)%20%26%20(area_ha%20%3C%3D%201)%60%20%7C%0A%20%20%20%20%7C%20%60plots.query('crop%20%3D%3D%20%22Rice%22%20and%20irrigated%20%3D%3D%20%22No%22')%60%20%7C%20the%20condition%2C%20written%20as%20text%2C%20holds%20%7C%20the%20whole%20mask%20%7C%0A%0A%20%20%20%20%60between%60%20takes%20%60inclusive%3D%22left%22%60%2C%20%60%22right%22%60%20or%20%60%22neither%22%60%20to%20leave%20out%20an%20end.%20Inside%20%60query%60's%20text%2C%20%60and%60%2C%20%60or%60%20and%20%60not%60%20are%20allowed%20and%20columns%20are%20named%20bare%3B%20a%20Python%20variable%20is%20written%20with%20%60%40%60%3A%20%60plots.query(%22crop%20in%20%40crops%20and%20area_ha%20%3E%20%40smallest%22)%60.%0A%0A%20%20%20%20**Missing%20values.**%2014%20plots%20have%20no%20crop%20recorded%20(%60NaN%60).%20A%20comparison%20with%20%60NaN%60%20is%20%60False%60%2C%20so%20those%20plots%20are%20not%20in%20%60crop%20%3D%3D%20%22Rice%22%60%2C%20nor%20in%20%60crop.isin(%5B...%5D)%60.%20But%20they%20**are**%20in%20%60crop%20!%3D%20%22Rice%22%60%20and%20in%20%60~crop.isin(%5B...%5D)%60%3A%20%22not%20rice%22%20includes%20%22not%20recorded%22.%20Module%205%20is%20about%20missing%20values.%0A%0A%20%20%20%20%23%23%23%20loc%20and%20iloc%3A%20by%20label%2C%20by%20position%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20picks%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60plots.loc%5Brows%2C%20columns%5D%60%20%7C%20by%20**label**%3A%20index%20labels%2C%20a%20mask%2C%20or%20column%20names%20%7C%0A%20%20%20%20%7C%20%60plots.iloc%5Brows%2C%20columns%5D%60%20%7C%20by%20**position**%3A%200%20is%20the%20first%20row%20or%20column%2C%20as%20in%20module%202%20%7C%0A%20%20%20%20%7C%20%60plots.loc%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%2C%20%5B%22hh_id%22%2C%20%22production_kg%22%5D%5D%60%20%7C%20rows%20by%20a%20mask%2C%20and%20two%20columns%2C%20in%20one%20step%20%7C%0A%20%20%20%20%7C%20%60plots.iloc%5B%3A5%2C%20%3A3%5D%60%20%7C%20the%20first%20five%20rows%20and%20the%20first%20three%20columns%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(is_rice%2C%20plots)%3A%0A%20%20%20%20plots.loc%5Bis_rice%20%26%20(plots%5B%22production_kg%22%5D%20%3E%209000)%2C%20%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22area_ha%22%2C%20%22production_kg%22%5D%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Sorting%2C%20and%20the%20first%20rows%20after%20it%0A%0A%20%20%20%20%60sort_values(%22production_kg%22)%60%20puts%20rows%20in%20order%2C%20smallest%20first%3B%20%60ascending%3DFalse%60%20reverses%20it.%20To%20sort%20by%20several%20columns%2C%20give%20lists%3A%20%60sort_values(%5B%22crop%22%2C%20%22production_kg%22%5D%2C%20ascending%3D%5BTrue%2C%20False%5D)%60%20sorts%20by%20crop%2C%20and%20within%20a%20crop%20by%20production%2C%20largest%20first.%20Missing%20values%20go%20last%20(%60na_position%3D%22first%22%60%20puts%20them%20first).%20Sorting%20keeps%20each%20row's%20label.%0A%0A%20%20%20%20That%20is%20where%20%60.loc%60%20and%20%60.iloc%60%20part%20ways.%20After%20sorting%2C%20the%20labels%20are%20no%20longer%200%2C%201%2C%202%20from%20the%20top%2C%20so%20%22the%20first%20five%20rows%22%20means%20**positions**%3A%20%60.iloc%5B%3A5%5D%60%20or%20%60.head(5)%60.%20%60.loc%5B%3A5%5D%60%20means%20%22every%20row%20from%20the%20top%20down%20to%20the%20row%20labeled%205%22%2C%20wherever%20that%20row%20has%20moved%20to%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots)%3A%0A%20%20%20%20_by_harvest%20%3D%20plots.sort_values(%22production_kg%22%2C%20ascending%3DFalse)%0A%20%20%20%20_where%20%3D%20_by_harvest.index.get_loc(5)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22Sorted%20by%20production%2C%20the%20first%20labels%20are%20%7B_by_harvest.index%5B%3A5%5D.tolist()%7D.%20%60.head(5)%60%20and%20%60.iloc%5B%3A5%5D%60%20give%20**%7Blen(_by_harvest.iloc%5B%3A5%5D)%7D%20rows**%3B%20%22%0A%20%20%20%20%20%20%20%20f%22%60.loc%5B%3A5%5D%60%20gives%20**%7Blen(_by_harvest.loc%5B%3A5%5D)%3A%2C%7D%20rows**%2C%20because%20the%20row%20labeled%205%20is%20now%20at%20position%20%7B_where%3A%2C%7D.%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20A%20chart%20of%20a%20filtered%20table%0A%0A%20%20%20%20A%20filter%20is%20often%20the%20step%20before%20a%20chart.%20Pick%20a%20crop%3A%20the%20cell%20keeps%20that%20crop's%20plots%20of%20up%20to%203%20hectares%20and%20draws%2C%20with%20seaborn%2C%20each%20plot's%20area%20against%20its%20harvest%2C%20colored%20by%20whether%20it%20is%20irrigated.%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%20chart_crop%20%3D%20mo.ui.dropdown(options%3D%5B%22Rice%22%2C%20%22Corn%22%2C%20%22Vegetables%22%2C%20%22Banana%22%5D%2C%20value%3D%22Rice%22%2C%20label%3D%22Crop%22)%0A%20%20%20%20chart_crop%0A%20%20%20%20return%20(chart_crop%2C)%0A%0A%0A%40app.cell%0Adef%20_(chart_crop%2C%20plots)%3A%0A%20%20%20%20_chosen%20%3D%20plots%5B(plots%5B%22crop%22%5D%20%3D%3D%20chart_crop.value)%20%26%20(plots%5B%22area_ha%22%5D%20%3C%3D%203)%5D%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%204))%0A%20%20%20%20sns.scatterplot(data%3D_chosen%2C%20x%3D%22area_ha%22%2C%20y%3D%22production_kg%22%2C%20hue%3D%22irrigated%22%2C%20hue_order%3D%5B%22Yes%22%2C%20%22No%22%5D%2C%20alpha%3D0.7%2C%20ax%3D_ax)%0A%20%20%20%20_ax.set(title%3Df%22%7Bchart_crop.value%7D%3A%20%7Blen(_chosen)%3A%2C%7D%20plots%20of%20up%20to%203%20ha%22%2C%20xlabel%3D%22Area%20(ha)%22%2C%20ylabel%3D%22Production%20(kg)%22)%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%203.6.%20Plots%20to%20visit%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20extension%20office%20plans%20its%20visits%20from%20a%20list%3A%20plots%20that%20are%20**not%20irrigated**%2C%20of%20the%20crops%20it%20advises%20on%2C%20within%20the%20size%20range%20of%20its%20program.%20Each%20condition%20left%20out%2C%20or%20joined%20the%20wrong%20way%2C%20puts%20the%20wrong%20farms%20on%20the%20list.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%2C%20a%20list%20of%20%60crops%60%2C%20and%20two%20areas%20%60low%60%20and%20%60high%60%20%E2%86%92%20the%20rows%20of%20the%20plots%20whose%20%60crop%60%20is%20in%20%60crops%60%2C%20whose%20%60area_ha%60%20is%20from%20%60low%60%20to%20%60high%60%20(**both%20included**)%2C%20and%20whose%20%60irrigated%60%20is%20%60%22No%22%60.%20Every%20column%3B%20the%20rows%20keep%20their%20labels%20and%20their%20order.%0A%0A%20%20%20%20%7C%20plot%20%7C%20crop%20%7C%20area_ha%20%7C%20irrigated%20%7C%20with%20%60%5B%22Rice%22%2C%20%22Corn%22%5D%60%2C%200.5%20to%201%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%200%20%7C%20Rice%20%7C%200.50%20%7C%20No%20%7C%20kept%3A%200.5%20is%20included%20%7C%0A%20%20%20%20%7C%202%20%7C%20Rice%20%7C%200.70%20%7C%20Yes%20%7C%20left%20out%3A%20irrigated%20%7C%0A%20%20%20%20%7C%203%20%7C%20(not%20recorded)%20%7C%200.70%20%7C%20No%20%7C%20left%20out%3A%20no%20crop%20%7C%0A%20%20%20%20%7C%207%20%7C%20Corn%20%7C%201.01%20%7C%20No%20%7C%20left%20out%3A%20above%201%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60isin%60%2C%20%60between%60%2C%20%60%26%60%20with%20each%20condition%20in%20brackets%2C%20or%20%60query%60%20(all%20in%20this%20section).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20plots_to_visit(plots%3A%20pd.DataFrame%2C%20crops%3A%20list%5Bstr%5D%2C%20low%3A%20float%2C%20high%3A%20float)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20that%20are%20not%20irrigated%2C%20grow%20one%20of%20the%20crops%2C%20and%20have%20from%20low%20to%20high%20hectares.%22%22%22%0A%20%20%20%20%23%20Exercise%203.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20one%20mask%20per%20condition%3A%20crop%20in%20crops%2C%20area_ha%20from%20low%20to%20high%20(both%20included)%2C%20irrigated%20is%20%22No%22%0A%20%20%20%20%23%20Step%202%3A%20combine%20the%20three%20masks%20with%20%26%2C%20each%20in%20brackets%2C%20keep%20those%20rows%2C%20and%20return%20them%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.6%22)%0A%0A%0A%40app.function%0Adef%20visit_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Eight%20plots%20for%20the%20check%20of%20Exercise%203.6%2C%20each%20on%20one%20side%20of%20one%20condition.%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-0201%22%2C%20%22HH-0201%22%2C%20%22HH-0202%22%2C%20%22HH-0202%22%2C%20%22HH-0203%22%2C%20%22HH-0203%22%2C%20%22HH-0204%22%2C%20%22HH-0204%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%202%2C%201%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%20None%2C%20%22Banana%22%2C%20%22Corn%22%2C%20%22Corn%22%2C%20%22Corn%22%5D%2C%0A%20%20%20%20%20%20%20%20%22irrigated%22%3A%20%5B%22No%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22No%22%2C%20%22No%22%2C%20%22No%22%2C%20%22No%22%2C%20%22No%22%5D%2C%0A%20%20%20%20%20%20%20%20%22area_ha%22%3A%20%5B0.5%2C%201.0%2C%200.7%2C%200.7%2C%200.8%2C%200.49%2C%200.75%2C%201.01%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B1900%2C%204100%2C%203200%2C%202500%2C%2015400%2C%201300%2C%202600%2C%203900%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%22Exercise%203.6%22%2C%20plots_to_visit%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22eight%20plots%2C%20rice%20and%20corn%20from%200.5%20to%201%20ha%22%2C%20(visit_fixture()%2C%20%5B%22Rice%22%2C%20%22Corn%22%5D%2C%200.5%2C%201.0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots%2C%20rice%20from%200.5%20to%201%20ha%22%2C%20(course.plots()%2C%20%5B%22Rice%22%5D%2C%200.5%2C%201.0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots%2C%20corn%20and%20vegetables%20from%200.25%20to%200.5%20ha%22%2C%20(course.plots()%2C%20%5B%22Corn%22%2C%20%22Vegetables%22%5D%2C%200.25%2C%200.5))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20plots%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20first%20case%2C%20plot%200%20is%20exactly%200.5%20ha%20and%20plot%201%20exactly%201%20ha%3A%20both%20are%20kept.%20Plot%202%20is%20irrigated%2C%20plot%203%20has%20no%20crop%2C%20plot%204%20is%20banana.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20rice%20plots%20that%20are%20not%20irrigated%2C%20from%200.5%20to%201%20ha.%0A%20%20%20%20try_it(plots_to_visit%2C%20course.plots()%2C%20%5B%22Rice%22%5D%2C%200.5%2C%201.0)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous*%20%7C%20%60and%60%20between%20two%20masks.%20%7C%20%60%26%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20operation%20'rand_'%20not%20supported%E2%80%A6*%20%7C%20A%20condition%20without%20its%20own%20brackets.%20%7C%20%60(...)%20%26%20(...)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20('Lengths%20must%20match%20to%20compare'%2C%20%E2%80%A6)*%20%7C%20%60plots%5B%22crop%22%5D%20%3D%3D%20crops%60%20compares%20with%20a%20list.%20%7C%20%60.isin(crops)%60%20%7C%0A%20%20%20%20%7C%20The%20plots%20of%20exactly%200.5%20and%201%20ha%20are%20missing%20%7C%20%60%3E%60%20and%20%60%3C%60%20leave%20out%20the%20ends.%20%7C%20%60.between(low%2C%20high)%60%2C%20or%20%60%3E%3D%60%20and%20%60%3C%3D%60%20%7C%0A%20%20%20%20%7C%20Irrigated%20plots%2C%20or%20other%20crops%2C%20are%20in%20the%20list%20%7C%20%3Ccode%3E%26%23124%3B%3C%2Fcode%3E%20(or)%20where%20%60%26%60%20(and)%20was%20meant%2C%20or%20a%20condition%20is%20missing.%20%7C%20all%20three%20conditions%2C%20joined%20with%20%60%26%60%20%7C%0A%20%20%20%20%7C%20The%20plot%20with%20no%20crop%20is%20in%20the%20list%20%7C%20The%20crop%20condition%20was%20written%20as%20%22not%20something%20else%22%2C%20which%20includes%20%60NaN%60.%20%7C%20%60.isin(crops)%60%20%7C%0A%20%20%20%20%7C%20The%20rows%20are%20right%2C%20but%20numbered%200%2C%201%2C%202%20%7C%20%60.reset_index(drop%3DTrue)%60%20threw%20the%20labels%20away.%20%7C%20return%20%60plots%5Bmask%5D%60%20as%20it%20is%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%203.7.%20The%20largest%20harvests%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20largest%20harvests%20are%20the%20first%20values%20a%20survey%20team%20checks%20for%20keying%20mistakes%20(a%20production%20with%20one%20zero%20too%20many).%20The%20list%20must%20be%20the%20same%20every%20time%20it%20is%20made%2C%20so%20ties%20need%20a%20rule%3A%20here%2C%20by%20household%2C%20then%20by%20plot.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20and%20a%20number%20%60n%60%20%E2%86%92%20the%20%60n%60%20plots%20with%20the%20largest%20%60production_kg%60%2C%20largest%20first%3B%20plots%20with%20the%20same%20production%20in%20%60hh_id%60%20order%2C%20then%20%60plot_no%60%20order.%20Only%20the%20columns%20%60hh_id%60%2C%20%60plot_no%60%2C%20%60crop%60%20and%20%60production_kg%60%2C%20in%20that%20order%3B%20the%20rows%20keep%20their%20labels.%0A%0A%20%20%20%20%7C%20label%20%7C%20hh_id%20%7C%20plot_no%20%7C%20crop%20%7C%20production_kg%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C--%3A%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%203%20%7C%20HH-0301%20%7C%201%20%7C%20Banana%20%7C%209%2C100%20%7C%0A%20%20%20%20%7C%201%20%7C%20HH-0302%20%7C%202%20%7C%20Corn%20%7C%204%2C200%20%7C%0A%20%20%20%20%7C%204%20%7C%20HH-0302%20%7C%201%20%7C%20Rice%20%7C%203%2C000%20%7C%0A%20%20%20%20%7C%202%20%7C%20HH-0304%20%7C%201%20%7C%20Rice%20%7C%203%2C000%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60sort_values%60%20with%20a%20list%20of%20columns%20and%20a%20list%20for%20%60ascending%60%3B%20the%20first%20%60n%60%20rows%20by%20position%2C%20%60.head(n)%60%20or%20%60.iloc%5B%3An%5D%60%3B%20a%20list%20of%20columns%20in%20brackets%20(all%20in%20this%20section).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20largest_harvests(plots%3A%20pd.DataFrame%2C%20n%3A%20int)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20n%20plots%20with%20the%20largest%20production%2C%20ties%20in%20hh_id%20then%20plot_no%20order%3B%20four%20columns%2C%20labels%20kept.%22%22%22%0A%20%20%20%20%23%20Exercise%203.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20sort%20by%20production_kg%20(largest%20first)%2C%20then%20hh_id%2C%20then%20plot_no%20(both%20smallest%20first)%0A%20%20%20%20%23%20Step%202%3A%20keep%20the%20first%20n%20rows%20by%20position%0A%20%20%20%20%23%20Step%203%3A%20keep%20the%20columns%20hh_id%2C%20plot_no%2C%20crop%2C%20production_kg%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.7%22)%0A%0A%0A%40app.function%0Adef%20harvest_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Six%20plots%20for%20the%20check%20of%20Exercise%203.7%2C%20three%20of%20them%20with%20the%20same%20production%2C%20listed%20against%20hh_id%20order.%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-0305%22%2C%20%22HH-0302%22%2C%20%22HH-0304%22%2C%20%22HH-0301%22%2C%20%22HH-0302%22%2C%20%22HH-0303%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%201%2C%201%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Corn%22%2C%20%22Rice%22%2C%20%22Banana%22%2C%20%22Rice%22%2C%20%22Vegetables%22%5D%2C%0A%20%20%20%20%20%20%20%20%22area_ha%22%3A%20%5B0.8%2C%201.1%2C%200.7%2C%200.6%2C%200.75%2C%200.2%5D%2C%0A%20%20%20%20%20%20%20%20%22irrigated%22%3A%20%5B%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22No%22%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B3000%2C%204200%2C%203000%2C%209100%2C%203000%2C%20800%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%22Exercise%203.7%22%2C%20largest_harvests%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22six%20plots%2C%20the%20largest%20four%20(three%20share%203%2C000%20kg)%22%2C%20(harvest_fixture()%2C%204))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22six%20plots%2C%20the%20largest%20two%22%2C%20(harvest_fixture()%2C%202))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots%2C%20the%20largest%2025%22%2C%20(course.plots()%2C%2025))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20plots%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20first%20case%2C%20three%20plots%20harvested%203%2C000%20kg%3A%20HH-0302%20comes%20before%20HH-0304%2C%20which%20comes%20before%20HH-0305.%20The%20rows%20keep%20their%20labels.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20ten%20largest%20harvests.%20Are%20any%20of%20them%20keying%20mistakes%2C%20do%20you%20think%3F%0A%20%20%20%20try_it(largest_harvests%2C%20course.plots()%2C%2010)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.7%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20smallest%20harvests%20%7C%20Sorted%20smallest%20first.%20%7C%20%60ascending%3D%5BFalse%2C%20True%2C%20True%5D%60%20%7C%0A%20%20%20%20%7C%20The%20right%20plots%2C%20but%20the%203%2C000%20kg%20ones%20in%20the%20wrong%20order%20%7C%20Ties%20not%20broken%20by%20%60hh_id%60%20and%20%60plot_no%60%2C%20or%20%60nlargest%60.%20%7C%20sort%20by%20all%20three%20columns%20%7C%0A%20%20%20%20%7C%20Too%20few%20or%20too%20many%20rows%2C%20or%20*KeyError%3A%20'Cannot%20get%20right%20slice%20bound%E2%80%A6'*%20%7C%20%60.loc%5B%3An%5D%60%20takes%20rows%20down%20to%20the%20**label**%20n.%20%7C%20%60.head(n)%60%20or%20%60.iloc%5B%3An%5D%60%20%7C%0A%20%20%20%20%7C%20Extra%20columns%2C%20or%20the%20columns%20in%20another%20order%20%7C%20The%20columns%20were%20not%20picked%2C%20or%20picked%20in%20another%20order.%20%7C%20%60%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22crop%22%2C%20%22production_kg%22%5D%5D%60%20%7C%0A%20%20%20%20%7C%20The%20rows%20are%20numbered%200%2C%201%2C%202%20%7C%20%60.reset_index(drop%3DTrue)%60%20threw%20the%20labels%20away.%20%7C%20keep%20the%20labels%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%20New%20columns%20under%20copy-on-write%3B%20what%20changed%20from%20pandas%202%0A%0A%20%20%20%20Yield%20needs%20every%20area%20in%20hectares%2C%20and%20151%20of%20the%20survey's%20plots%20were%20measured%20in%20**square%20meters**%20(%60area_unit%60%20is%20%60%22sqm%22%60%3B%2010%2C000%20square%20meters%20make%20a%20hectare).%20You%20need%20a%20new%20column%20that%20converts%20some%20rows%20and%20keeps%20the%20others%2C%20and%20sometimes%20a%20fix%20to%20a%20few%20values%20of%20an%20existing%20column%2C%20without%20losing%20track%20of%20which%20table%20you%20changed.%0A%0A%20%20%20%20%23%23%23%20A%20new%20column%2C%20from%20a%20condition%3A%20where%0A%0A%20%20%20%20%60a.where(condition%2C%20other)%60%20keeps%20each%20value%20of%20%60a%60%20where%20the%20condition%20is%20%60True%60%2C%20and%20takes%20the%20value%20of%20%60other%60%20where%20it%20is%20%60False%60.%20Its%20mirror%2C%20%60a.mask(condition%2C%20other)%60%2C%20replaces%20where%20the%20condition%20is%20%60True%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%20raw_plots%20%3D%20course.plots(clean%3DFalse)%20%20%23%20the%20plots%20as%20read%20in%20section%202%3A%20no%20area_ha%20yet%0A%20%20%20%20_area_ha%20%3D%20raw_plots%5B%22area%22%5D.where(raw_plots%5B%22area_unit%22%5D%20%3D%3D%20%22ha%22%2C%20raw_plots%5B%22area%22%5D%20%2F%2010_000)%0A%20%20%20%20pd.DataFrame(%7B%22area%22%3A%20raw_plots%5B%22area%22%5D%2C%20%22area_unit%22%3A%20raw_plots%5B%22area_unit%22%5D%2C%20%22area_ha%22%3A%20_area_ha%7D).head(4)%0A%20%20%20%20return%20(raw_plots%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%20Adding%20columns%3A%20in%20place%2C%20or%20a%20new%20table%20with%20assign%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20what%20changes%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60table%5B%22area_ha%22%5D%20%3D%20...%60%20%7C%20%60table%60%20itself%20gets%20the%20column%20(or%20the%20column%20is%20replaced)%20%7C%0A%20%20%20%20%7C%20%60table.assign(area_ha%3D...)%60%20%7C%20nothing%3A%20you%20get%20a%20**new**%20table%20with%20the%20column%2C%20and%20%60table%60%20is%20left%20as%20it%20was%20%7C%0A%20%20%20%20%7C%20%60table.loc%5Bmask%2C%20%22area_unit%22%5D%20%3D%20%22sqm%22%60%20%7C%20%60table%60%20itself%3A%20the%20column's%20values%20in%20the%20rows%20of%20the%20mask%2C%20in%20one%20step%20%7C%0A%0A%20%20%20%20%60assign%60%20takes%20one%20argument%20per%20new%20column.%20A%20**lambda**%20(a%20small%20function%20without%20a%20name%2C%20%60lambda%20t%3A%20...%60)%20receives%20the%20table%20as%20it%20is%20at%20that%20point%2C%20so%20a%20column%20made%20earlier%20in%20the%20same%20%60assign%60%20can%20be%20used%20by%20a%20later%20one%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20with_yield%20%3D%20raw_plots.assign(%0A%20%20%20%20%20%20%20%20area_ha%3Draw_plots%5B%22area%22%5D.where(raw_plots%5B%22area_unit%22%5D%20%3D%3D%20%22ha%22%2C%20raw_plots%5B%22area%22%5D%20%2F%2010_000)%2C%0A%20%20%20%20%20%20%20%20yield_t_ha%3Dlambda%20t%3A%20t%5B%22production_kg%22%5D%20%2F%201000%20%2F%20t%5B%22area_ha%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%23%23%23%20Copy-on-write%3A%20a%20table%20you%20got%20from%20another%20is%20its%20own%0A%0A%20%20%20%20In%20pandas%203%2C%20every%20table%20or%20column%20you%20get%20from%20another%20one%20(by%20filtering%2C%20selecting%20columns%2C%20%60head%60%2C%20%60assign%60%2C%20%60sort_values%60%E2%80%A6)%20**behaves%20as%20a%20separate%20copy**.%20Changing%20it%20never%20changes%20the%20table%20it%20came%20from%2C%20and%20changing%20the%20original%20never%20changes%20it.%20pandas%20copies%20the%20data%20only%20when%20one%20of%20them%20is%20actually%20changed%2C%20which%20is%20why%20it%20is%20called%20**copy-on-write**%2C%20and%20why%20it%20costs%20nothing%20until%20then.%0A%0A%20%20%20%20The%20consequence%20is%20a%20rule%20that%20is%20easy%20to%20state%3A%20**to%20change%20a%20table%2C%20change%20it%20in%20one%20step.**%20A%20change%20written%20as%20two%20steps%2C%20a%20selection%20followed%20by%20an%20assignment%2C%20changes%20the%20selection%2C%20which%20is%20a%20temporary%20copy%2C%20and%20is%20lost.%20That%20is%20called%20**chained%20assignment**%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%5B%22fertilizer_kg%22%5D%20%3D%200%20%20%20%20%20%23%20step%201%20makes%20a%20copy%3B%20step%202%20changes%20the%20copy%0A%20%20%20%20plots.loc%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%2C%20%22fertilizer_kg%22%5D%20%3D%200%20%20%23%20one%20step%3A%20plots%20is%20changed%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20cell%20below%20runs%20the%20first%20line%20on%20a%20copy%20of%20the%20plots%20and%20shows%20what%20pandas%203.1.1%20says%2C%20and%20what%20changed.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_plots)%3A%0A%20%20%20%20_plots%20%3D%20raw_plots.copy()%20%20%23%20a%20copy%20of%20its%20own%2C%20so%20the%20other%20cells%20keep%20the%20plots%20as%20read%0A%20%20%20%20_rice%20%3D%20_plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20_caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20_plots%5B_rice%5D%5B%22fertilizer_kg%22%5D%20%3D%200%20%20%23%20chained%20assignment%3A%20two%20steps%0A%20%20%20%20_after_chained%20%3D%20int((_plots.loc%5B_rice%2C%20%22fertilizer_kg%22%5D%20%3D%3D%200).sum())%0A%20%20%20%20_plots.loc%5B_rice%2C%20%22fertilizer_kg%22%5D%20%3D%200%20%20%23%20one%20step%0A%20%20%20%20_after_loc%20%3D%20int((_plots.loc%5B_rice%2C%20%22fertilizer_kg%22%5D%20%3D%3D%200).sum())%0A%20%20%20%20_warning%20%3D%20_caught%5B0%5D%20if%20_caught%20else%20None%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22pandas%20warned%3A%20**%7B_warning.category.__name__%7D**%22%20if%20_warning%20else%20%22pandas%20gave%20no%20warning.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%3E%20%7Bstr(_warning.message).splitlines()%5B0%5D%7D%22%20if%20_warning%20else%20%22%22)%2C%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22written%20as%22%3A%20%5B'plots%5Brice%5D%5B%22fertilizer_kg%22%5D%20%3D%200'%2C%20'plots.loc%5Brice%2C%20%22fertilizer_kg%22%5D%20%3D%200'%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22rice%20plots%20with%20fertilizer%200%20afterwards%22%3A%20%5B_after_chained%2C%20_after_loc%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22of%22%3A%20%5Bint(_rice.sum())%5D%20*%202%2C%0A%20%20%20%20%20%20%20%20%7D)%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%20Despite%20its%20name%2C%20%60ChainedAssignmentError%60%20is%20a%20**warning**%2C%20not%20an%20error%3A%20the%20code%20runs%20to%20the%20end%2C%20and%20nothing%20changes.%20In%20marimo%2C%20a%20warning%20appears%20below%20the%20cell%2C%20in%20its%20console%20output.%20Whenever%20you%20see%20it%2C%20rewrite%20the%20line%20as%20one%20step%20with%20%60.loc%5Brows%2C%20column%5D%20%3D%20value%60%2C%20or%20build%20the%20column%20whole%20with%20%60where%60%20or%20%60assign%60.%0A%0A%20%20%20%20Two%20more%20things%20copy-on-write%20means%3A%0A%0A%20%20%20%20-%20**A%20filtered%20table%20can%20be%20changed%20freely.**%20%60rice%20%3D%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%60%20then%20%60rice%5B%22checked%22%5D%20%3D%20True%60%20changes%20%60rice%60%20only%2C%20without%20any%20warning.%20%60plots%60%20does%20not%20get%20the%20column.%0A%20%20%20%20-%20**A%20second%20name%20is%20not%20a%20copy.**%20%60fixed%20%3D%20plots%60%20gives%20the%20same%20table%20a%20second%20name%3A%20%60fixed.loc%5B...%5D%20%3D%20...%60%20changes%20%60plots%60%20too.%20That%20matters%20inside%20a%20function%3A%20changing%20the%20table%20it%20was%20given%20changes%20the%20caller's%20table.%20Use%20%60assign%60%2C%20which%20returns%20a%20new%20table%2C%20or%20start%20with%20%60fixed%20%3D%20plots.copy()%60%2C%20a%20table%20of%20its%20own.%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%20you%20know%20pandas%202%3A%20copy-on-write%20is%20always%20on%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20pandas%203%20makes%20copy-on-write%20the%20only%20behavior.%20What%20changes%20from%20pandas%202%3A%0A%0A%20%20%20%20%7C%20%7C%20pandas%202%20%7C%20pandas%203%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60df%5Bmask%5D%5B%22col%22%5D%20%3D%20value%60%20%7C%20did%20not%20change%20%60df%60%2C%20and%20warned%20*SettingWithCopyWarning*%20%7C%20never%20changes%20%60df%60%3B%20warns%20*ChainedAssignmentError*%20%7C%0A%20%20%20%20%7C%20%60df%5B%22col%22%5D%5Bmask%5D%20%3D%20value%60%20%7C%20changed%20%60df%60%3B%20pandas%202.2%20warned%20that%20this%20would%20stop%20working%20%7C%20never%20changes%20%60df%60%3B%20warns%20*ChainedAssignmentError*%20%7C%0A%20%20%20%20%7C%20%60df%5B%22col%22%5D.fillna(0%2C%20inplace%3DTrue)%60%20%7C%20changed%20%60df%60%3B%20pandas%202.2%20warned%20that%20this%20would%20stop%20working%20%7C%20never%20changes%20%60df%60%3B%20warns%20*ChainedAssignmentError*.%20Write%20%60df%5B%22col%22%5D%20%3D%20df%5B%22col%22%5D.fillna(0)%60%20%7C%0A%20%20%20%20%7C%20*SettingWithCopyWarning*%20%7C%20could%20appear%20when%20you%20changed%20a%20table%20filtered%20from%20another%20%7C%20removed%3A%20changing%20a%20filtered%20table%20is%20fine%20and%20changes%20only%20it%20%7C%0A%20%20%20%20%7C%20%60.copy()%60%20after%20a%20filter%20%7C%20written%20to%20silence%20the%20warning%20%7C%20not%20needed%20%7C%0A%20%20%20%20%7C%20%60pd.options.mode.copy_on_write%60%20%7C%20turned%20copy-on-write%20on%2C%20to%20try%20it%20early%20%7C%20always%20on%3B%20setting%20it%20does%20nothing%2C%20and%20the%20option%20is%20deprecated%20%7C%0A%0A%20%20%20%20Every%20difference%20is%20listed%20in%20%5Bprimer%20P2%2C%20pandas%202%20to%203%5D(%2F%3Ffile%3D91_primer_pandas_2_to_3.py).%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%20Why%20the%20unit%20matters%3A%20rice%20yields%2C%20two%20ways%0A%0A%20%20%20%20The%20chart%2C%20drawn%20with%20Altair%2C%20shows%20the%20yield%20of%20every%20rice%20plot.%20Switch%20the%20conversion%20off%2C%20and%20the%20plots%20measured%20in%20square%20meters%20are%20divided%20by%20thousands%20of%20%22hectares%22%3A%20their%20yields%20collapse%20to%20nearly%20zero%2C%20and%20pull%20any%20average%20down%20with%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%20convert%20%3D%20mo.ui.switch(value%3DTrue%2C%20label%3D%22Convert%20square%20meters%20to%20hectares%22)%0A%20%20%20%20convert%0A%20%20%20%20return%20(convert%2C)%0A%0A%0A%40app.cell%0Adef%20_(convert%2C%20raw_plots)%3A%0A%20%20%20%20_rice%20%3D%20raw_plots%5Braw_plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20_area%20%3D%20_rice%5B%22area%22%5D.where(_rice%5B%22area_unit%22%5D%20%3D%3D%20%22ha%22%2C%20_rice%5B%22area%22%5D%20%2F%2010_000)%20if%20convert.value%20else%20_rice%5B%22area%22%5D%0A%20%20%20%20_yields%20%3D%20_rice.assign(yield_t_ha%3D_rice%5B%22production_kg%22%5D%20%2F%201000%20%2F%20_area)%0A%20%20%20%20_title%20%3D%20f%22Rice%3A%20%7Blen(_yields)%3A%2C%7D%20plots%3B%20the%20mean%20of%20their%20yields%20is%20%7B_yields%5B'yield_t_ha'%5D.mean()%3A.2f%7D%20t%2Fha%20(unweighted)%22%0A%20%20%20%20alt.Chart(_yields).mark_bar().encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22yield_t_ha%3AQ%22%2C%20bin%3Dalt.Bin(maxbins%3D40)%2C%20title%3D%22Yield%20(t%2Fha)%22)%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22count()%3AQ%22%2C%20title%3D%22Plots%22)%2C%0A%20%20%20%20).properties(width%3D520%2C%20height%3D220%2C%20title%3D_title)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%203.8.%20Area%20in%20hectares%2C%20and%20yield%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20yield%20in%20the%20rest%20of%20the%20course%20is%20computed%20this%20way.%20Getting%20the%20unit%20wrong%20does%20not%20raise%20an%20error%3A%20it%20only%20gives%20smaller%20yields%2C%20as%20the%20chart%20above%20shows.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20as%20read%20from%20the%20file%20(columns%20%60area%60%2C%20%60area_unit%60%2C%20%60production_kg%60%2C%20%E2%80%A6)%20%E2%86%92%20a%20**new**%20table%20with%20every%20column%20of%20the%20input%20and%20two%20more%20at%20the%20end%3A%20%60area_ha%60%20(the%20area%20where%20%60area_unit%60%20is%20%60%22ha%22%60%2C%20the%20area%20divided%20by%2010%2C000%20where%20it%20is%20%60%22sqm%22%60)%20and%20%60yield_t_ha%60%20(%60production_kg%20%2F%201000%20%2F%20area_ha%60).%20**The%20table%20given%20must%20not%20change.**%0A%0A%20%20%20%20%7C%20area%20%7C%20area_unit%20%7C%20production_kg%20%7C%20%E2%86%92%20area_ha%20%7C%20yield_t_ha%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%200.5%20%7C%20ha%20%7C%202%2C000%20%7C%200.5%20%7C%204.0%20%7C%0A%20%20%20%20%7C%202%2C500%20%7C%20sqm%20%7C%201%2C100%20%7C%200.25%20%7C%204.4%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60where%60%20and%20%60assign%60%2C%20with%20a%20lambda%20for%20the%20column%20that%20uses%20%60area_ha%60%20(this%20section).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20add_yield(plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20new%20table%3A%20the%20plots%20with%20area_ha%20(every%20area%20in%20hectares)%20and%20yield_t_ha.%20The%20table%20given%20is%20unchanged.%22%22%22%0A%20%20%20%20%23%20Exercise%203.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20area_ha%3A%20the%20area%20where%20area_unit%20is%20%22ha%22%2C%20else%20the%20area%20divided%20by%2010%2C000%20(where)%0A%20%20%20%20%23%20Step%202%3A%20yield_t_ha%3A%20production_kg%20%2F%201000%20%2F%20area_ha%0A%20%20%20%20%23%20Step%203%3A%20return%20a%20new%20table%20with%20both%20columns%20(assign)%2C%20leaving%20the%20table%20given%20as%20it%20is%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.8%22)%0A%0A%0A%40app.function%0Adef%20units_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20plots%20in%20the%20file's%20layout%2C%20some%20in%20hectares%20and%20some%20in%20square%20meters%2C%20for%20the%20checks%20of%20Exercises%203.8%20and%0A%20%20%20%203.9%20(two%20of%20them%20keyed%20as%20hectares%20though%20their%20areas%20are%20in%20square%20meters).%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-0401%22%2C%20%22HH-0401%22%2C%20%22HH-0402%22%2C%20%22HH-0403%22%2C%20%22HH-0403%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Corn%22%2C%20%22Rice%22%2C%20%22Vegetables%22%2C%20%22Rice%22%5D%2C%0A%20%20%20%20%20%20%20%20%22area%22%3A%20%5B0.5%2C%202500.0%2C%20100.0%2C%205000.0%2C%201940.0%5D%2C%0A%20%20%20%20%20%20%20%20%22area_unit%22%3A%20%5B%22ha%22%2C%20%22sqm%22%2C%20%22ha%22%2C%20%22sqm%22%2C%20%22ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B2000%2C%201100%2C%20410000%2C%201800%2C%20760%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20with_inputs(tables%3A%20dict%5Bstr%2C%20pd.DataFrame%5D)%3A%0A%20%20%20%20%22%22%22A%20summary%20for%20a%20check%20whose%20function%20must%20leave%20its%20input%20alone%3A%20its%20result%2C%20and%20the%20tables%20it%20was%20given%20as%20they%0A%20%20%20%20are%20after%20the%20call.%20A%20function%20that%20changes%20a%20table%20it%20was%20given%20changes%20the%20second%20part.%22%22%22%0A%0A%20%20%20%20def%20summary(result)%3A%0A%20%20%20%20%20%20%20%20%23%20Decimals%20to%206%20places%2C%20so%20computing%20in%20another%20order%20(which%20changes%20their%20last%20digits)%20gives%20the%20same%20answer%0A%20%20%20%20%20%20%20%20rounded%20%3D%20result.round(6)%20if%20isinstance(result%2C%20pd.DataFrame)%20else%20result%0A%20%20%20%20%20%20%20%20return%20%7B%22result%22%3A%20rounded%2C%20%22the%20tables%20given%2C%20after%20the%20call%22%3A%20tables%7D%0A%0A%20%20%20%20return%20summary%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_small%2C%20_survey%20%3D%20units_fixture().head(2)%2C%20course.plots(clean%3DFalse)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.8%22%2C%20add_yield%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%20plots%2C%20one%20in%20hectares%20and%20one%20in%20square%20meters%22%2C%20(_small%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots%2C%20as%20read%20from%20the%20file%22%2C%20(_survey%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%3Dwith_inputs(%7B%22two%20plots%22%3A%20_small%2C%20%22the%20survey's%20plots%22%3A%20_survey%7D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20compares%20your%20new%20table%2C%20and%20also%20the%20tables%20your%20function%20was%20given%2C%20after%20the%20call%3A%20they%20must%20be%20unchanged.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20plots%20with%20area_ha%20and%20yield_t_ha%20(scroll%20right%20to%20see%20them).%0A%20%20%20%20try_it(add_yield%2C%20course.plots(clean%3DFalse))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%203.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%20The%20result%20looks%20right%2C%20but%20the%20check%20is%20red%20%7C%20The%20function%20changed%20the%20table%20it%20was%20given%3A%20%60plots%5B%22area_ha%22%5D%20%3D%20...%60%20adds%20the%20column%20to%20the%20caller's%20table.%20%7C%20%60plots.assign(...)%60%20%7C%0A%20%20%20%20%7C%20The%20hectare%20plots%20are%20divided%20by%2010%2C000%20%7C%20The%20condition%20is%20reversed.%20%7C%20%60where(plots%5B%22area_unit%22%5D%20%3D%3D%20%22ha%22%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20Square-meter%20plots%20have%20yields%20near%200%20%7C%20The%20yield%20used%20%60area%60%2C%20not%20%60area_ha%60.%20%7C%20%60lambda%20t%3A%20...%20%2F%20t%5B%22area_ha%22%5D%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'area_ha'*%20%7C%20%60plots%5B%22area_ha%22%5D%60%20was%20used%20inside%20the%20same%20%60assign%60%20without%20a%20lambda%3A%20the%20column%20does%20not%20exist%20yet.%20%7C%20%60yield_t_ha%3Dlambda%20t%3A%20...%60%20%7C%0A%20%20%20%20%7C%20Yields%201%2C000%20times%20too%20large%20%7C%20Kilograms%20were%20not%20turned%20into%20tonnes.%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%20Exercise%203.9.%20Correct%20the%20unit%20where%20it%20was%20keyed%20wrongly%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20fixing%20a%20few%20values%20of%20a%20column%20is%20the%20most%20common%20change%20of%20all%2C%20and%20the%20one%20chained%20assignment%20breaks%20without%20an%20error.%20The%20survey's%20field%20manual%20says%20no%20plot%20of%20a%20farm%20household%20covers%20100%20hectares%2C%20so%20an%20area%20over%20100%20keyed%20as%20%60%22ha%22%60%20is%20a%20square-meter%20area%20keyed%20with%20the%20wrong%20unit.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20as%20read%20from%20the%20file%20%E2%86%92%20a%20corrected%20table%3A%20wherever%20%60area_unit%60%20is%20%60%22ha%22%60%20and%20%60area%60%20is%20**over%20100**%2C%20%60area_unit%60%20becomes%20%60%22sqm%22%60.%20Everything%20else%20stays%20as%20it%20is%2C%20including%20an%20area%20of%20exactly%20100.%20**The%20table%20given%20must%20not%20change.**%0A%0A%20%20%20%20%7C%20area%20%7C%20area_unit%20%7C%20%E2%86%92%20area_unit%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%200.5%20%7C%20ha%20%7C%20ha%20%7C%0A%20%20%20%20%7C%20100%20%7C%20ha%20%7C%20ha%3A%20not%20over%20100%20%7C%0A%20%20%20%20%7C%201%2C940%20%7C%20ha%20%7C%20sqm%20%7C%0A%20%20%20%20%7C%202%2C500%20%7C%20sqm%20%7C%20sqm%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20mask%20with%20%60%26%60%3B%20%60.loc%5Bmask%2C%20%22area_unit%22%5D%20%3D%20%22sqm%22%60%20on%20a%20table%20of%20its%20own%20(%60.copy()%60)%2C%20or%20%60assign%60%20with%20%60mask%60%20(this%20section).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20fix_keyed_units(plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20with%20area_unit%20set%20to%20%22sqm%22%20where%20it%20says%20%22ha%22%20but%20the%20area%20is%20over%20100.%20The%20table%20given%20is%20unchanged.%22%22%22%0A%20%20%20%20%23%20Exercise%203.9%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20make%20a%20table%20of%20its%20own%20to%20change%20(so%20the%20table%20given%20stays%20as%20it%20is)%0A%20%20%20%20%23%20Step%202%3A%20a%20mask%3A%20area_unit%20is%20%22ha%22%20and%20area%20is%20over%20100%0A%20%20%20%20%23%20Step%203%3A%20in%20one%20step%2C%20set%20area_unit%20to%20%22sqm%22%20in%20the%20rows%20of%20the%20mask%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%203.9%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_five%2C%20_survey%20%3D%20units_fixture()%2C%20course.plots(clean%3DFalse)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%203.9%22%2C%20fix_keyed_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(%22five%20plots%3A%20one%20of%20exactly%20100%20ha%2C%20one%20of%201%2C940%20keyed%20as%20ha%22%2C%20(_five%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots%2C%20as%20read%20from%20the%20file%22%2C%20(_survey%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%3Dwith_inputs(%7B%22five%20plots%22%3A%20_five%2C%20%22the%20survey's%20plots%22%3A%20_survey%7D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20compares%20your%20corrected%20table%2C%20and%20also%20the%20tables%20your%20function%20was%20given%2C%20after%20the%20call%3A%20they%20must%20be%20unchanged.%20In%20the%20first%20case%20only%20the%20plot%20of%201%2C940%20changes.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20five%20plots%20of%20the%20check%2C%20corrected.%20Only%20the%201%2C940%20%22ha%22%20plot%20should%20now%20say%20sqm.%0A%20%20%20%20try_it(fix_keyed_units%2C%20units_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%203.9%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Nothing%20changed%2C%20and%20a%20*ChainedAssignmentError*%20warning%20appears%20%7C%20Chained%20assignment%3A%20%60fixed%5Bmask%5D%5B%22area_unit%22%5D%20%3D%20%22sqm%22%60%20changes%20a%20temporary%20copy.%20%7C%20%60fixed.loc%5Bmask%2C%20%22area_unit%22%5D%20%3D%20%22sqm%22%60%20%7C%0A%20%20%20%20%7C%20The%20result%20is%20right%2C%20but%20the%20check%20is%20red%20%7C%20The%20table%20given%20was%20changed%3A%20%60fixed%20%3D%20plots%60%20is%20not%20a%20copy.%20%7C%20%60plots.copy()%60%2C%20or%20%60assign%60%20%7C%0A%20%20%20%20%7C%20The%20plot%20of%20exactly%20100%20ha%20changed%20%7C%20%60%3E%3D%60%20where%20the%20rule%20says%20over%20100.%20%7C%20%60%3E%20100%60%20%7C%0A%20%20%20%20%7C%20The%20%60area%60%20column%20changed%20instead%20%7C%20The%20column%20name%20in%20%60.loc%60%20is%20%60%22area%22%60.%20%7C%20%60.loc%5Bmask%2C%20%22area_unit%22%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%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20you%20can%20%7C%20with%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20line%20values%20up%20by%20label%2C%20and%20move%20labels%20in%20and%20out%20of%20the%20index%20%7C%20alignment%2C%20%60set_index%60%2C%20%60reset_index%60%20%7C%0A%20%20%20%20%7C%20read%20the%20survey's%20files%20with%20the%20right%20types%20%7C%20%60dtype%3D%60%2C%20%60thousands%3D%60%2C%20%60format%3D%60%2C%20%60sheet_name%3D%60%2C%20%60header%3D%60%2C%20%60.str.zfill%60%20%7C%0A%20%20%20%20%7C%20write%20CSV%2C%20Excel%20and%20Parquet%2C%20and%20know%20which%20types%20survive%20%7C%20%60to_csv%60%2C%20%60to_excel%60%2C%20%60to_parquet%60%20%7C%0A%20%20%20%20%7C%20pick%20rows%20and%20columns%2C%20and%20order%20them%20%7C%20masks%20with%20%60%26%60%2C%20%60isin%60%2C%20%60between%60%2C%20%60query%60%2C%20%60.loc%60%2C%20%60.iloc%60%2C%20%60sort_values%60%20%7C%0A%20%20%20%20%7C%20add%20and%20change%20columns%2C%20in%20one%20step%20%7C%20%60where%60%2C%20%60assign%60%2C%20%60.loc%5Bmask%2C%20column%5D%20%3D%20value%60%20%7C%0A%0A%20%20%20%20Next%2C%20the%20debugging%20drill%20%5BD1%2C%20A%20table%20that%20lies%5D(%2F%3Ffile%3D25_debugging_drill.py)%20hides%20six%20of%20this%20module's%20mistakes%20in%20one%20notebook%20for%20you%20to%20find.%20Then%20module%204%2C%20%5BSummarize%20and%20combine%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%2C%20groups%20these%20tables%2C%20weights%20them%20and%20joins%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%20course.quizzes_at(%223.4%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
59315de5d0b1b17895ff843977d2800d