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%22Cleaning%20and%20time%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20lists%20every%20other%20cell%20uses.%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%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20NOT_STATED%20%3D%20%7B%22head_sex%22%3A%209%2C%20%22head_age%22%3A%20999%7D%20%20%23%20the%20codebook's%20%22not%20stated%22%20code%20for%20each%20column%0A%20%20%20%20NORTH_TO_SOUTH%20%3D%20%5B%22Ilocos%20Norte%22%2C%20%22Isabela%22%2C%20%22Nueva%20Ecija%22%2C%20%22Camarines%20Sur%22%2C%20%22Iloilo%22%2C%20%22Bukidnon%22%2C%20%22Cotabato%22%5D%0A%20%20%20%20SQM_PER_HA%20%3D%2010_000%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%20Cleaning%20and%20time%0A%0A%20%20%20%20**Where%20you%20are%3A**%20in%20modules%203%20and%204%20you%20read%20the%20farm%20survey%20with%20the%20right%20types%2C%20then%20grouped%2C%20weighted%20and%20joined%20it.%20All%20along%2C%20a%20few%20things%20in%20the%20survey%20were%20quietly%20wrong%3A%20codes%20that%20mean%20%22not%20stated%22%20counted%20as%20real%20ages%2C%20plots%20measured%20in%20square%20meters%20added%20to%20plots%20in%20hectares%2C%20households%20entered%20twice.%20This%20module%20finds%20each%20one%2C%20fixes%20it%20with%20code%20you%20can%20run%20again%2C%20and%20then%20turns%20to%20**time**%3A%20the%20monthly%20farmgate%20prices%2C%20and%20a%20month%20that%20never%20arrived.%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%20Missing%20values%20%7C%20tell%20NaN%20from%20NA%2C%20turn%20%22not%20stated%22%20codes%20into%20missing%20values%2C%20and%20decide%20when%20not%20to%20fill%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Types%20%7C%20give%20each%20column%20the%20type%20its%20values%20need%3A%20nullable%20integers%2C%20categories%2C%20text%2C%20one%20unit%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Dates%20%7C%20read%20day-first%20dates%20without%20swapping%20day%20and%20month%3B%20count%20by%20month%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20Time%20series%20%7C%20put%20the%20prices%20on%20a%20date%20index%2C%20make%20the%20missing%20month%20visible%2C%20then%20resample%2C%20smooth%20and%20compute%20growth%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20Duplicates%20and%20checks%20%7C%20drop%20rows%20entered%20twice%2C%20check%20the%20keys%2C%20and%20write%20a%20check%20you%20can%20rerun%20next%20time%20%7C%0A%0A%20%20%20%20You%20start%20from%20the%20tables%20as%20module%203%20read%20them%3A%20%60course.households(clean%3DFalse)%60%20and%20%60course.plots(clean%3DFalse)%60%2C%20the%20right%20types%20and%20every%20problem%20still%20in%20them.%20By%20the%20end%2C%20your%20cleaning%20gives%20exactly%20what%20%60course.households()%60%20and%20%60course.plots()%60%20give%2C%20and%20the%20later%20modules%20start%20from%20those.%0A%0A%20%20%20%20Exercises%20work%20as%20before%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20last%20line%20first.%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(%225.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20survey%20as%20module%203%20read%20it%3A%20the%20right%20types%2C%20every%20problem%20still%20in%20it%0A%20%20%20%20raw_households%20%3D%20course.households(clean%3DFalse)%0A%20%20%20%20raw_plots%20%3D%20course.plots(clean%3DFalse)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60raw_households%60%3A%20**%7Blen(raw_households)%3A%2C%7D%20rows**.%20%60raw_plots%60%3A%20**%7Blen(raw_plots)%3A%2C%7D%20rows**.%20The%20first%20households%3A%22)%2C%0A%20%20%20%20%20%20%20%20raw_households.head(5)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20raw_households%2C%20raw_plots%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%20Missing%20values%3A%20NaN%2C%20NA%2C%20not-stated%20codes%0A%0A%20%20%20%20**The%20problem.**%20What%20is%20the%20average%20age%20of%20a%20household%20head%3F%20Ask%20pandas%20directly%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20raw_households%5B%22head_age%22%5D.mean()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_households)%3A%0A%20%20%20%20_ages%20%3D%20raw_households%5B%22head_age%22%5D%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20About%20%7B_ages.mean()%3A.0f%7D%20years%2C%20which%20no%20one%20would%20believe%20of%20the%20heads%20of%20farm%20households.%20The%20cause%20is%20in%20the%20codebook%3A%20**999%20means%20%22not%20stated%22**.%20%7Bint(_ages.eq(999).sum())%7D%20rows%20say%20999%2C%20and%20%60mean()%60%20treats%20each%20as%20a%20head%20aged%20999.%20Without%20them%20the%20average%20is%20%7B_ages%5B_ages.ne(999)%5D.mean()%3A.1f%7D.%0A%0A%20%20%20%20%60head_sex%60%20has%20the%20same%20kind%20of%20code%3A%20**9%20%3D%20not%20stated**%2C%20in%20%7Bint(raw_households%5B%22head_sex%22%5D.eq(9).sum())%7D%20rows.%20A%20code%20like%20this%20is%20information%20(the%20enumerator%20asked%20and%20got%20no%20answer)%2C%20but%20it%20is%20not%20a%20number%20to%20compute%20with.%20pandas%20has%20a%20way%20to%20say%20%22no%20value%20here%22%3A%20a%20**missing%20value**.%20This%20section%20turns%20the%20codes%20into%20missing%20values%2C%20and%20then%20asks%20the%20harder%20question%3A%20what%20to%20do%20with%20values%20that%20are%20missing.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Three%20ways%20to%20say%20%22missing%22%0A%0A%20%20%20%20pandas%20marks%20a%20missing%20value%20differently%20depending%20on%20the%20column's%20type.%20All%20three%20are%20missing%20to%20pandas%2C%20and%20%60isna()%60%20finds%20all%20three%3A%0A%0A%20%20%20%20%7C%20marker%20%7C%20where%20you%20meet%20it%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60NaN%60%20%7C%20decimal%20(%60float64%60)%20columns%2C%20and%20text%20(%60str%60)%20columns%20%7C%20%22not%20a%20number%22%3A%20a%20special%20decimal%20value%20from%20NumPy%20(%5Bmodule%202%5D(%2F%3Ffile%3D10_arrays.py)%2C%20section%204)%20%7C%0A%20%20%20%20%7C%20%60%3CNA%3E%60%20%7C%20**nullable**%20columns%3A%20%60Int64%60%20(capital%20I)%2C%20%60boolean%60%2C%20%60Float64%60%20%7C%20pandas'%20own%20missing%20value%2C%20which%20a%20whole-number%20column%20can%20hold%20%7C%0A%20%20%20%20%7C%20%60NaT%60%20%7C%20date%20columns%20%7C%20%22not%20a%20time%22%20%7C%0A%0A%20%20%20%20Two%20rules%20follow%2C%20and%20both%20catch%20people%20out%3A%0A%0A%20%20%20%20-%20**Never%20compare%20with%20%60%3D%3D%20np.nan%60.**%20NaN%20is%20not%20equal%20to%20anything%2C%20not%20even%20itself%2C%20so%20%60column%20%3D%3D%20np.nan%60%20is%20%60False%60%20everywhere.%20Ask%20%60column.isna()%60%20instead%20(and%20%60notna()%60%20for%20the%20opposite).%0A%20%20%20%20-%20**%60pd.NA%60%20is%20not%20%60True%60%20or%20%60False%60.**%20%60pd.NA%20%3D%3D%201%60%20gives%20%60%3CNA%3E%60%20(%22don't%20know%22)%2C%20and%20%60if%20pd.NA%3A%60%20raises%20*TypeError%3A%20boolean%20value%20of%20NA%20is%20ambiguous*.%20Use%20%60isna()%60%20here%20too.%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_ages%20%3D%20pd.Series(%5B45%2C%20None%2C%2061%5D%2C%20dtype%3D%22Int64%22)%0A%20%20%20%20_decimals%20%3D%20pd.Series(%5B2.5%2C%20np.nan%2C%201.0%5D)%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22an%20Int64%20column%22%3A%20_ages.astype(str)%2C%0A%20%20%20%20%20%20%20%20%22.isna()%22%3A%20_ages.isna()%2C%0A%20%20%20%20%20%20%20%20%22a%20float%20column%22%3A%20_decimals.astype(str)%2C%0A%20%20%20%20%20%20%20%20%22%3D%3D%20np.nan%22%3A%20_decimals%20%3D%3D%20np.nan%2C%0A%20%20%20%20%20%20%20%20%22.isna()%20%22%3A%20_decimals.isna()%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Finding%20the%20missing%20values%0A%0A%20%20%20%20%60table.isna()%60%20gives%20a%20table%20of%20%60True%60%20and%20%60False%60%2C%20one%20per%20value%3B%20%60.sum()%60%20then%20counts%20the%20%60True%60s%20in%20each%20column.%20Here%20are%20the%20plots%3A%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%20raw_plots.isna().sum()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_plots)%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20Two%20columns%20have%20gaps%2C%20both%20described%20in%20the%20codebook%20as%20%22blank%20%3D%20not%20recorded%22%3A%20**%60crop%60**%20(%7Bint(raw_plots%5B%22crop%22%5D.isna().sum())%7D%20plots)%20and%20**%60fertilizer_kg%60**%20(%7Bint(raw_plots%5B%22fertilizer_kg%22%5D.isna().sum())%7D%20plots).%20The%20households'%20table%20shows%20no%20gaps%20at%20all%2C%20because%20its%20%22not%20stated%22%20answers%20are%20written%20as%209%20and%20999%2C%20which%20pandas%20reads%20as%20ordinary%20numbers.%20Those%20are%20the%20first%20thing%20to%20fix.%0A%0A%20%20%20%20%23%23%23%20Codes%20into%20missing%20values%0A%0A%20%20%20%20%60column.mask(condition)%60%20keeps%20each%20value%20where%20the%20condition%20is%20%60False%60%20and%20makes%20it%20missing%20where%20it%20is%20%60True%60.%20On%20a%20whole-number%20column%2C%20it%20needs%20one%20step%20first%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_age%20%3D%20pd.Series(%5B58%2C%20999%2C%2040%5D%2C%20name%3D%22head_age%22)%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22head_age%22%3A%20_age%2C%0A%20%20%20%20%20%20%20%20%22.mask(age%20%3D%3D%20999)%22%3A%20_age.mask(_age%20%3D%3D%20999)%2C%0A%20%20%20%20%20%20%20%20'.astype(%22Int64%22).mask(age%20%3D%3D%20999)'%3A%20_age.astype(%22Int64%22).mask(_age%20%3D%3D%20999)%2C%0A%20%20%20%20%7D).astype(str).assign(%0A%20%20%20%20%20%20%20%20**%7B%22type%20of%20the%20middle%20column%22%3A%20str(_age.mask(_age%20%3D%3D%20999).dtype)%2C%20%22type%20of%20the%20last%20column%22%3A%20str(_age.astype(%22Int64%22).mask(_age%20%3D%3D%20999).dtype)%7D%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%20A%20plain%20whole-number%20column%20(%60int64%60)%20cannot%20hold%20a%20missing%20value.%20Asked%20to%2C%20pandas%20turns%20the%20whole%20column%20into%20decimals%20so%20it%20can%20use%20NaN%3A%2058%20becomes%2058.0.%20%60astype(%22Int64%22)%60%20first%20makes%20it%20a%20**nullable%20integer**%20column%2C%20which%20holds%20%60%3CNA%3E%60%20and%20keeps%20every%20other%20value%20a%20whole%20number.%20Section%202%20says%20more%20about%20why%20that%20matters.%0A%0A%20%20%20%20**Why%20not%20fill%20them%20in%3F**%20You%20could%20replace%20each%20999%20with%20the%20average%20age.%20Then%2031%20households%20would%20have%20an%20age%20nobody%20gave%2C%20the%20spread%20of%20ages%20would%20look%20narrower%20than%20it%20is%2C%20and%20no%20one%20reading%20the%20table%20could%20tell.%20A%20missing%20value%20says%20exactly%20what%20happened%3A%20the%20head%20did%20not%20say.%20%60mean()%60%2C%20%60sum()%60%20and%20the%20other%20summaries%20skip%20missing%20values%20by%20themselves%2C%20so%20you%20lose%20nothing%20by%20leaving%20them%20missing%2C%20and%20you%20can%20still%20count%20them%20(%60isna().sum()%60)%20to%20report%20how%20many%20did%20not%20answer.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.1.%20Not%20stated%20is%20missing%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20average%2C%20share%20or%20chart%20of%20the%20head's%20age%20or%20sex%20is%20wrong%20while%20999%20and%209%20are%20counted%20as%20answers.%20This%20function%20fixes%20both%20columns%20at%20once%2C%20and%20section%205%20builds%20it%20into%20the%20cleaning%20you%20can%20rerun.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20households%20table%20%E2%86%92%20the%20same%20table%2C%20with%20%60head_sex%60%209%20and%20%60head_age%60%20999%20turned%20into%20%60%3CNA%3E%60%2C%20and%20both%20columns%20of%20type%20%60Int64%60.%20Every%20other%20column%20stays%20as%20it%20was.%0A%0A%20%20%20%20%7C%20head_sex%20%7C%20head_age%20%7C%20%E2%86%92%20%7C%20head_sex%20%7C%20head_age%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%2058%20%7C%20%7C%201%20%7C%2058%20%7C%0A%20%20%20%20%7C%209%20%7C%20999%20%7C%20%7C%20%60%3CNA%3E%60%20%7C%20%60%3CNA%3E%60%20%7C%0A%20%20%20%20%7C%202%20%7C%2099%20%7C%20%7C%202%20%7C%2099%20%7C%0A%0A%20%20%20%20A%20head%20aged%2099%20is%20rare%20but%20possible%3A%20only%20the%20code%20999%20means%20%22not%20stated%22.%20The%20codes%20are%20in%20the%20setup%20cell%20as%20%60NOT_STATED%20%3D%20%7B%22head_sex%22%3A%209%2C%20%22head_age%22%3A%20999%7D%60.%0A%0A%20%20%20%20Tools%3A%20%60.astype(%22Int64%22)%60%20and%20%60.mask(condition)%60%2C%20above%3B%20%60table.assign(column%3D...)%60%20returns%20a%20new%20table%20with%20that%20column%20replaced%20(%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%2C%20section%204).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20mark_not_stated(households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20with%20the%20not-stated%20codes%20(head_sex%209%2C%20head_age%20999)%20as%20%3CNA%3E%2C%20both%20columns%20nullable%20Int64.%22%22%22%0A%20%20%20%20%23%20Exercise%205.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20start%20from%20the%20table%3B%20for%20each%20column%20and%20code%20in%20NOT_STATED.items()%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20make%20the%20column%20Int64%2C%20then%20mask%20the%20values%20equal%20to%20the%20code%0A%20%20%20%20%23%20Step%202%3A%20return%20the%20new%20table%20(assign%20returns%20a%20new%20one%2C%20so%20the%20input%20is%20never%20changed)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.1%22)%0A%0A%0A%40app.function%0Adef%20households_fixture(kind%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Small%20households%20tables%20for%20the%20checks%20of%20Exercises%205.1%20and%205.9.%22%22%22%0A%20%20%20%20rows%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%23%20codes%20in%20both%20columns%2C%20alone%20and%20together%2C%20and%20a%20real%20age%20of%2099%0A%20%20%20%20%20%20%20%20%22codes%22%3A%20%5B(%22HH-0001%22%2C%201%2C%2058)%2C%20(%22HH-0002%22%2C%209%2C%20999)%2C%20(%22HH-0003%22%2C%202%2C%2099)%2C%20(%22HH-0004%22%2C%201%2C%20999)%2C%20(%22HH-0005%22%2C%209%2C%2061)%5D%2C%0A%20%20%20%20%20%20%20%20%23%20no%20codes%20at%20all%3A%20the%20columns%20still%20become%20Int64%0A%20%20%20%20%20%20%20%20%22no%20codes%22%3A%20%5B(%22HH-0001%22%2C%202%2C%2041)%2C%20(%22HH-0002%22%2C%201%2C%2035)%5D%2C%0A%20%20%20%20%20%20%20%20%23%20HH-0002%20entered%20twice%20(the%20same%20row)%2C%20HH-0003%20twice%20with%20different%20ages%20(not%20a%20copy)%0A%20%20%20%20%20%20%20%20%22twice%22%3A%20%5B(%22HH-0001%22%2C%201%2C%2058)%2C%20(%22HH-0002%22%2C%209%2C%20999)%2C%20(%22HH-0002%22%2C%209%2C%20999)%2C%20(%22HH-0003%22%2C%202%2C%2047)%2C%20(%22HH-0003%22%2C%202%2C%2048)%2C%20(%22HH-0004%22%2C%201%2C%2030)%5D%2C%0A%20%20%20%20%7D%5Bkind%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22hh_id%22%2C%20%22head_sex%22%2C%20%22head_age%22%5D)%0A%20%20%20%20return%20table.assign(province%3D%22Iloilo%22%2C%20weight%3D250.0)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_households)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.1%22%2C%20mark_not_stated%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22codes%20in%20both%20columns%2C%20and%20a%20head%20aged%2099%22%2C%20(households_fixture(%22codes%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20table%20with%20no%20codes%20at%20all%22%2C%20(households_fixture(%22no%20codes%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%22%2C%20(raw_households%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%3Dcourse.with_dtypes%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20compares%20the%20values%20and%20each%20column's%20type%3A%20both%20columns%20must%20be%20Int64%2C%20with%20%3CNA%3E%20only%20where%20the%20code%20was.%20Use%20the%20Try%20it%20cell%20to%20see%20your%20types.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20function%20on%20the%20survey.%20How%20many%20missing%20values%20now%2C%20and%20what%20type%20is%20each%20column%3F%0A%20%20%20%20def%20_try_not_stated()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20marked%20%3D%20mark_not_stated(raw_households)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%3A%20marked%5B%5B%22head_sex%22%2C%20%22head_age%22%5D%5D.dtypes.astype(str)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22missing%22%3A%20marked%5B%5B%22head_sex%22%2C%20%22head_age%22%5D%5D.isna().sum()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mean%22%3A%20marked%5B%5B%22head_sex%22%2C%20%22head_age%22%5D%5D.mean().round(2)%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_try_not_stated()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20type%20is%20%60float64%60%20%7C%20%60mask%60%20on%20the%20plain%20integer%20column%3A%20pandas%20turned%20it%20into%20decimals%20to%20hold%20NaN%20%7C%20%60astype(%22Int64%22)%60%20before%20%60.mask(...)%60%20%7C%0A%20%20%20%20%7C%20the%20type%20is%20%60object%60%20%7C%20%60replace(999%2C%20pd.NA)%60%20on%20a%20plain%20integer%20column%2C%20which%20gives%20a%20column%20of%20mixed%20Python%20values%20%7C%20%60astype(%22Int64%22).mask(column%20%3D%3D%20999)%60%20%7C%0A%20%20%20%20%7C%200%20or%20an%20average%20where%20the%20code%20was%20%7C%20the%20codes%20were%20filled%20instead%20of%20made%20missing%20%7C%20%60.mask(...)%60%20makes%20missing%3B%20leave%20out%20%60fillna%60%20%7C%0A%20%20%20%20%7C%20the%20head%20aged%2099%20is%20missing%20%7C%20the%20condition%20catches%20more%20than%20the%20code%2C%20such%20as%20%60%3E%3D%2099%60%20%7C%20%60%3D%3D%20code%60%20%7C%0A%20%20%20%20%7C%20wrong%20on%20%22a%20table%20with%20no%20codes%20at%20all%22%20%7C%20the%20type%20is%20changed%20only%20when%20a%20code%20is%20found%20%7C%20change%20the%20type%20for%20every%20table%20%7C%0A%20%20%20%20%7C%20one%20column%20still%20has%20its%20code%20%7C%20only%20one%20column%20is%20handled%20%7C%20loop%20over%20%60NOT_STATED.items()%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_plots)%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20When%20not%20to%20fill%0A%0A%20%20%20%20%60fillna(value)%60%20replaces%20missing%20values%20with%20a%20value%2C%20and%20%60dropna()%60%20drops%20the%20rows%20that%20have%20any.%20Both%20are%20easy%2C%20and%20both%20change%20what%20the%20numbers%20mean.%20%7Bint(raw_plots%5B%22fertilizer_kg%22%5D.isna().sum())%7D%20plots%20have%20no%20fertilizer%20amount%20recorded.%20Choose%20what%20to%20do%20with%20them%2C%20and%20watch%20the%20summary%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20fill_choice%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%5B%22leave%20them%20missing%22%2C%20%22fill%20with%200%22%2C%20%22fill%20with%20the%20average%22%2C%20%22drop%20every%20row%20with%20a%20missing%20value%22%5D%2C%0A%20%20%20%20%20%20%20%20value%3D%22leave%20them%20missing%22%2C%20label%3D%22The%20missing%20fertilizer%20amounts%3A%22%2C%0A%20%20%20%20)%0A%20%20%20%20fill_choice%0A%20%20%20%20return%20(fill_choice%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(fill_choice%2C%20raw_plots)%3A%0A%20%20%20%20_fert%20%3D%20raw_plots%5B%22fertilizer_kg%22%5D%0A%20%20%20%20_versions%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22leave%20them%20missing%22%3A%20raw_plots%2C%0A%20%20%20%20%20%20%20%20%22fill%20with%200%22%3A%20raw_plots.assign(fertilizer_kg%3D_fert.fillna(0))%2C%0A%20%20%20%20%20%20%20%20%22fill%20with%20the%20average%22%3A%20raw_plots.assign(fertilizer_kg%3D_fert.fillna(_fert.mean()))%2C%0A%20%20%20%20%20%20%20%20%22drop%20every%20row%20with%20a%20missing%20value%22%3A%20raw_plots.dropna()%2C%0A%20%20%20%20%7D%0A%20%20%20%20_what%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22leave%20them%20missing%22%3A%20%22pandas%20skips%20missing%20values%20in%20%60mean()%60%2C%20%60sum()%60%20and%20%60std()%60%2C%20so%20the%20summaries%20describe%20the%20plots%20whose%20amount%20is%20known%2C%20and%20%60count()%60%20says%20how%20many%20those%20are.%20Nothing%20is%20invented%20and%20nothing%20is%20lost.%22%2C%0A%20%20%20%20%20%20%20%20%22fill%20with%200%22%3A%20%22A%20missing%20amount%20is%20not%20zero%3A%20the%20enumerator%20did%20not%20record%20it%2C%20which%20is%20not%20the%20same%20as%20the%20farmer%20using%20none.%20Every%20one%20of%20these%20plots%20now%20counts%20as%20unfertilized%2C%20so%20the%20average%20falls%2C%20and%20an%20analysis%20of%20fertilizer%20and%20yield%20would%20be%20wrong%20for%20them.%22%2C%0A%20%20%20%20%20%20%20%20%22fill%20with%20the%20average%22%3A%20%22The%20average%20stays%20the%20same%2C%20but%20the%20total%20grows%20by%20amounts%20no%20one%20recorded%2C%20and%20the%20spread%20(%60std%60)%20shrinks%2C%20because%20the%20invented%20values%20all%20sit%20at%20the%20middle.%20Nothing%20in%20the%20table%20says%20which%20values%20were%20invented.%22%2C%0A%20%20%20%20%20%20%20%20%22drop%20every%20row%20with%20a%20missing%20value%22%3A%20%22%60dropna()%60%20drops%20a%20row%20if%20*any*%20column%20is%20missing%2C%20so%20the%20plots%20with%20no%20crop%20go%20too%2C%20and%20with%20them%20their%20production.%20Every%20total%20of%20production%20is%20now%20short.%20To%20drop%20only%20the%20rows%20missing%20one%20column%2C%20give%20%60dropna(subset%3D%5B'fertilizer_kg'%5D)%60%2C%20and%20only%20where%20the%20question%20needs%20it.%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20_t%20%3D%20_versions%5Bfill_choice.value%5D%0A%20%20%20%20_summary%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22plots%20in%20the%20table%22%3A%20%5Blen(_t)%5D%2C%0A%20%20%20%20%20%20%20%20%22plots%20with%20an%20amount%22%3A%20%5Bint(_t%5B%22fertilizer_kg%22%5D.count())%5D%2C%0A%20%20%20%20%20%20%20%20%22average%20fertilizer%2C%20kg%22%3A%20%5Bround(_t%5B%22fertilizer_kg%22%5D.mean()%2C%201)%5D%2C%0A%20%20%20%20%20%20%20%20%22spread%20(std)%2C%20kg%22%3A%20%5Bround(_t%5B%22fertilizer_kg%22%5D.std()%2C%201)%5D%2C%0A%20%20%20%20%20%20%20%20%22total%20fertilizer%2C%20kg%22%3A%20%5Bround(_t%5B%22fertilizer_kg%22%5D.sum())%5D%2C%0A%20%20%20%20%20%20%20%20%22total%20production%2C%20kg%22%3A%20%5Bint(_t%5B%22production_kg%22%5D.sum())%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20mo.vstack(%5B_summary%2C%20mo.callout(mo.md(_what%5Bfill_choice.value%5D)%2C%20kind%3D%22success%22%20if%20fill_choice.value%20%3D%3D%20%22leave%20them%20missing%22%20else%20%22warn%22)%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%20So%20the%20rule%20for%20an%20**amount**%20(kilograms%2C%20ages%2C%20prices)%20is%3A%20leave%20it%20missing%2C%20and%20report%20how%20many%20are%20missing.%20Fill%20only%20when%20you%20know%20the%20value%3A%20when%20the%20codebook%20says%20a%20blank%20means%20zero%2C%20for%20example.%0A%0A%20%20%20%20A%20**label**%20is%20different.%20A%20plot%20with%20no%20crop%20recorded%20should%20not%20vanish%20from%20a%20table%20of%20crops%3A%20%60groupby%60%20leaves%20out%20missing%20keys%20by%20default%20(%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%2C%20section%201)%2C%20so%20its%20production%20would%20silently%20drop%20out%20of%20the%20totals.%20In%20a%20summary%2C%20give%20the%20missing%20label%20a%20name%20the%20reader%20understands%2C%20with%20%60fillna(%22Not%20recorded%22)%60.%20The%20data%20keeps%20its%20missing%20value%3B%20only%20the%20report%20shows%20the%20name.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.2.%20A%20crop%20summary%20that%20loses%20nothing%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20table%20a%20report%20would%20print.%20Every%20plot%20must%20be%20counted%20once%2C%20every%20kilogram%20of%20production%20must%20be%20in%20a%20total%2C%20and%20the%20average%20fertilizer%20must%20describe%20the%20plots%20whose%20amount%20is%20known.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20%E2%86%92%20one%20row%20per%20crop%2C%20the%20crops%20in%20alphabetical%20order%20(as%20%60groupby%60%20sorts%20them)%2C%20with%20the%20plots%20that%20have%20no%20crop%20as%20a%20row%20named%20%60Not%20recorded%60%2C%20and%20three%20columns%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60plots%60%20%7C%20how%20many%20plots%20(every%20plot%2C%20whether%20or%20not%20its%20fertilizer%20is%20recorded)%20%7C%0A%20%20%20%20%7C%20%60production_kg%60%20%7C%20the%20total%20production%20%7C%0A%20%20%20%20%7C%20%60fertilizer_kg%60%20%7C%20the%20average%20fertilizer%2C%20over%20the%20plots%20whose%20amount%20is%20recorded%20%7C%0A%0A%20%20%20%20%7C%20crop%20%7C%20plots%20%7C%20production_kg%20%7C%20fertilizer_kg%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Banana%20%7C%20137%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20Not%20recorded%20%7C%2014%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20Tools%3A%20%60fillna%60%2C%20above%3B%20%60groupby(...).agg(name%3D(%22column%22%2C%20%22function%22))%60%20with%20%60%22size%22%60%2C%20%60%22sum%22%60%20and%20%60%22mean%22%60%20(%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%2C%20section%201).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20crop_summary(plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20crop%20(missing%20crops%20as%20%22Not%20recorded%22)%3A%20number%20of%20plots%2C%20total%20production%2C%20average%20fertilizer.%22%22%22%0A%20%20%20%20%23%20Exercise%205.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20copy%20of%20the%20table%20whose%20crop%20column%20has%20its%20missing%20values%20filled%20with%20%22Not%20recorded%22%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20(fill%20the%20label%20only%3A%20leave%20fertilizer_kg%20as%20it%20is)%0A%20%20%20%20%23%20Step%202%3A%20group%20by%20crop%20and%20aggregate%3A%20plots%20(size)%2C%20production_kg%20(sum)%2C%20fertilizer_kg%20(mean)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.2%22)%0A%0A%0A%40app.function%0Adef%20plots_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20small%20plots%20table%3A%20a%20plot%20with%20no%20crop%2C%20plots%20with%20no%20fertilizer%20recorded%2C%20and%20both%20units%20of%20area.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22HH-0001%22%2C%201%2C%20%22Rice%22%2C%201.5%2C%20%22ha%22%2C%20%22Yes%22%2C%206200%2C%20260.0)%2C%0A%20%20%20%20%20%20%20%20(%22HH-0001%22%2C%202%2C%20%22Rice%22%2C%202500.0%2C%20%22sqm%22%2C%20%22No%22%2C%20900%2C%20np.nan)%2C%0A%20%20%20%20%20%20%20%20(%22HH-0002%22%2C%201%2C%20None%2C%200.4%2C%20%22ha%22%2C%20%22No%22%2C%201400%2C%2070.0)%2C%0A%20%20%20%20%20%20%20%20(%22HH-0002%22%2C%202%2C%20%22Corn%22%2C%20600.0%2C%20%22sqm%22%2C%20%22No%22%2C%20250%2C%2011.5)%2C%0A%20%20%20%20%20%20%20%20(%22HH-0003%22%2C%201%2C%20%22Corn%22%2C%200.8%2C%20%22ha%22%2C%20%22No%22%2C%203100%2C%20np.nan)%2C%0A%20%20%20%20%20%20%20%20(%22HH-0003%22%2C%202%2C%20%22Banana%22%2C%201.2%2C%20%22ha%22%2C%20%22No%22%2C%2025000%2C%20150.0)%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22crop%22%2C%20%22area%22%2C%20%22area_unit%22%2C%20%22irrigated%22%2C%20%22production_kg%22%2C%20%22fertilizer_kg%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_plots)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.2%22%2C%20crop_summary%2C%0A%20%20%20%20%20%20%20%20%5B(%22six%20plots%3A%20one%20with%20no%20crop%2C%20two%20with%20no%20fertilizer%22%2C%20(plots_fixture()%2C))%2C%20(%22the%20survey's%20plots%22%2C%20(raw_plots%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Every%20plot%20counts%20in%20%60plots%60%20and%20in%20%60production_kg%60%3B%20only%20%60fertilizer_kg%60%20leaves%20out%20the%20plots%20with%20no%20amount.%20Use%20the%20Try%20it%20cell%20to%20compare%20your%20plot%20counts%20with%20%60len(raw_plots)%60.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20summary%20of%20the%20survey's%20plots.%20Do%20the%20plots%20add%20up%20to%20every%20plot%20in%20the%20table%3F%0A%20%20%20%20def%20_try_crops()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20summary%20%3D%20crop_summary(raw_plots)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20counted%20%3D%20int(summary%5B%22plots%22%5D.sum())%20if%20%22plots%22%20in%20summary.columns%20else%200%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bsummary%2C%20mo.md(f%22Plots%20counted%3A%20**%7Bcounted%3A%2C%7D**%20of%20**%7Blen(raw_plots)%3A%2C%7D**%20in%20the%20table.%22)%5D)%0A%0A%20%20%20%20_try_crops()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20no%20%60Not%20recorded%60%20row%20%7C%20%60groupby%60%20leaves%20out%20missing%20keys%20%7C%20%60fillna(%22Not%20recorded%22)%60%20on%20%60crop%60%20first%20%7C%0A%20%20%20%20%7C%20a%20row%20named%20%60NaN%60%20or%20%600%60%20%7C%20%60groupby(dropna%3DFalse)%60%20keeps%20the%20group%20but%20not%20the%20name%3B%20%60fillna(0)%60%20on%20the%20whole%20table%20%7C%20fill%20%60crop%60%20alone%2C%20with%20%60%22Not%20recorded%22%60%20%7C%0A%20%20%20%20%7C%20fertilizer%20averages%20too%20low%20%7C%20missing%20amounts%20filled%20with%200%20%7C%20leave%20%60fertilizer_kg%60%20missing%20%7C%0A%20%20%20%20%7C%20fewer%20plots%20than%20in%20the%20table%20%7C%20%60dropna()%60%20before%20grouping%2C%20or%20%60%22count%22%60%20on%20a%20column%20with%20gaps%20%7C%20no%20%60dropna()%60%3B%20%60(%22plot_no%22%2C%20%22size%22)%60%20%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20a%20column%20%7C%20a%20typo%20in%20a%20column%20name%2C%20or%20%60agg%60%20given%20a%20name%20that%20is%20not%20a%20column%20%7C%20the%20column%20names%20in%20the%20task%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**If%20you%20know%20pandas%202.**%20A%20text%20column%20read%20from%20a%20file%20used%20to%20be%20%60object%60%2C%20and%20its%20missing%20values%20could%20be%20%60None%60%20or%20%60NaN%60.%20In%20pandas%203%20a%20text%20column%20is%20%60str%60%2C%20and%20its%20missing%20values%20are%20always%20%60NaN%60%3A%20%60raw_plots%5B%22crop%22%5D.unique()%60%20shows%20%60nan%60%20among%20the%20crops.%20%60isna()%60%2C%20%60fillna()%60%20and%20%60dropna()%60%20work%20the%20same%20on%20both.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20Types%3A%20nullable%20integers%2C%20category%2C%20text%20with%20.str%2C%20one%20unit%0A%0A%20%20%20%20**The%20problem.**%20A%20column's%20type%20decides%20what%20pandas%20lets%20you%20do%20with%20it%20and%20what%20it%20gives%20back.%20Most%20of%20the%20survey's%20columns%20got%20the%20right%20type%20when%20module%203%20read%20them%2C%20but%20a%20type%20that%20is%20merely%20*possible*%20is%20not%20always%20the%20one%20the%20values%20need%3A%20a%20sex%20code%20stored%20as%20decimals%2C%20a%20province%20spelled%20out%201%2C196%20times%2C%20a%20code%20whose%20first%20digits%20are%20a%20province%2C%20an%20area%20whose%20unit%20changes%20from%20row%20to%20row.%20This%20section%20gives%20four%20columns%20the%20type%2C%20or%20the%20unit%2C%20their%20values%20need.%0A%0A%20%20%20%20%23%23%23%20Nullable%20integers%0A%0A%20%20%20%20Section%201%20made%20%60head_sex%60%20and%20%60head_age%60%20%60Int64%60%20rather%20than%20letting%20them%20become%20decimals.%20Here%20is%20what%20the%20choice%20changes%3A%0A%0A%20%20%20%20%7C%20%7C%20%60float64%60%20with%20NaN%20%7C%20%60Int64%60%20with%20%60%3CNA%3E%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20how%20a%20value%20prints%20%7C%20%601.0%60%2C%20%6058.0%60%20%7C%20%601%60%2C%20%6058%60%20%7C%0A%20%20%20%20%7C%20written%20to%20a%20CSV%20file%20%7C%20%601.0%60%2C%20so%20the%20code%20no%20longer%20matches%20the%20codebook's%20%601%60%20%7C%20%601%60%20%7C%0A%20%20%20%20%7C%20compared%20with%20%60%3D%3D%201%60%20%7C%20works%20%7C%20works%3B%20a%20missing%20value%20gives%20%60%3CNA%3E%60%2C%20not%20%60False%60%20%7C%0A%20%20%20%20%7C%20%60mean()%60%2C%20%60sum()%60%20%7C%20skip%20NaN%20%7C%20skip%20%60%3CNA%3E%60%20%7C%0A%0A%20%20%20%20The%20values%20are%20whole%20numbers%20and%20codes%3B%20%60Int64%60%20keeps%20them%20that%20way%20and%20still%20holds%20%22not%20stated%22.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20same%20two%20codes%2C%20written%20to%20a%20CSV%20file%20from%20each%20type%0A%20%20%20%20_as_float%20%3D%20pd.DataFrame(%7B%22head_sex%22%3A%20%5B1.0%2C%20np.nan%5D%7D).to_csv(index%3DFalse)%0A%20%20%20%20_as_int64%20%3D%20pd.DataFrame(%7B%22head_sex%22%3A%20pd.array(%5B1%2C%20pd.NA%5D%2C%20dtype%3D%22Int64%22)%7D).to_csv(index%3DFalse)%0A%20%20%20%20mo.hstack(%5Bmo.md(f%22**From%20%60float64%60%3A**%5Cn%60%60%60%5Cn%7B_as_float%7D%60%60%60%22)%2C%20mo.md(f%22**From%20%60Int64%60%3A**%5Cn%60%60%60%5Cn%7B_as_int64%7D%60%60%60%22)%5D%2C%20justify%3D%22start%22%2C%20gap%3D3)%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%20Category%3A%20a%20fixed%20set%20of%20values%0A%0A%20%20%20%20%60province%60%20holds%20seven%20different%20values%2C%20repeated%20over%201%2C196%20rows.%20A%20**category**%20column%20stores%20each%20different%20value%20once%2C%20and%20each%20row%20as%20a%20small%20number%20pointing%20to%20it.%20You%20make%20one%20with%20%60astype(%22category%22)%60%2C%20or%20with%20%60pd.Categorical(values%2C%20categories%3D%5B...%5D%2C%20ordered%3DTrue)%60%20when%20the%20values%20have%20an%20order%20of%20their%20own.%20What%20it%20changes%3A%0A%0A%20%20%20%20%7C%20%7C%20text%20(%60str%60)%20%7C%20category%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20memory%20%7C%20every%20row%20holds%20its%20text%20%7C%20each%20value%20once%2C%20plus%20one%20small%20number%20a%20row%20%7C%0A%20%20%20%20%7C%20values%20allowed%20%7C%20any%20text%20%7C%20only%20the%20categories%3A%20a%20new%20one%20raises%20*TypeError%3A%20Cannot%20setitem%20on%20a%20Categorical%20with%20a%20new%20category*%20%7C%0A%20%20%20%20%7C%20sorting%20%7C%20alphabetical%20%7C%20in%20the%20categories'%20order%2C%20when%20%60ordered%3DTrue%60%20%7C%0A%20%20%20%20%7C%20%60value_counts()%60%20%7C%20the%20values%20that%20occur%20%7C%20every%20category%2C%20with%200%20for%20those%20that%20don't%20occur%20%7C%0A%0A%20%20%20%20The%20order%20is%20the%20useful%20part%20here.%20Reports%20on%20these%20seven%20provinces%20list%20them%20north%20to%20south%2C%20not%20alphabetically.%20Switch%20the%20type%20and%20watch%20the%20sort%20and%20the%20memory%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20as_category%20%3D%20mo.ui.switch(label%3D%22province%20as%20an%20ordered%20category%2C%20north%20to%20south%22%2C%20value%3DFalse)%0A%20%20%20%20as_category%0A%20%20%20%20return%20(as_category%2C)%0A%0A%0A%40app.cell%0Adef%20_(as_category%2C%20raw_households)%3A%0A%20%20%20%20_table%20%3D%20raw_households%5B%5B%22province%22%2C%20%22hh_id%22%5D%5D%0A%20%20%20%20if%20as_category.value%3A%0A%20%20%20%20%20%20%20%20_table%20%3D%20_table.assign(province%3Dpd.Categorical(_table%5B%22province%22%5D%2C%20categories%3DNORTH_TO_SOUTH%2C%20ordered%3DTrue))%0A%20%20%20%20_sorted%20%3D%20_table.sort_values(%22province%22).drop_duplicates(%22province%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22Type%3A%20%60%7B_table%5B'province'%5D.dtype%7D%60%20%C2%B7%20memory%20for%20the%20column%3A%20**%7B_table%5B'province'%5D.memory_usage(deep%3DTrue)%3A%2C%7D%20bytes**%20%C2%B7%20%60sort_values('province')%60%20puts%20the%20provinces%20in%20this%20order%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22%20%E2%86%92%20%22.join(_sorted%5B%22province%22%5D.astype(str)))%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%20Make%20a%20column%20a%20category%20for%20the%20analysis%20that%20needs%20it%20(a%20sorted%20report%2C%20a%20chart's%20order%2C%20a%20large%20table%20in%20memory)%2C%20not%20as%20a%20habit%3A%20joining%20two%20tables%20on%20category%20columns%20whose%20categories%20differ%2C%20or%20adding%20a%20value%20the%20categories%20don't%20have%2C%20both%20need%20care.%20The%20cleaned%20tables%20the%20later%20modules%20use%20keep%20%60province%60%20and%20%60crop%60%20as%20text.%0A%0A%20%20%20%20%23%23%23%20Text%20with%20.str%0A%0A%20%20%20%20A%20text%20column%20has%20a%20set%20of%20text%20methods%20behind%20%60.str%60%2C%20which%20work%20on%20every%20value%20at%20once%2C%20as%20Python's%20own%20text%20methods%20work%20on%20one%20(Python%20Essentials%2C%20module%205).%20Each%20gives%20a%20column%20back%2C%20so%20they%20chain%3A%0A%0A%20%20%20%20%7C%20you%20write%20%7C%20gives%2C%20for%20%60%220102820000%22%60%20or%20%60%22HH-0001%22%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60psgc.str.len()%60%20%7C%20%6010%60%20%7C%0A%20%20%20%20%7C%20%60psgc.str.startswith(%220%22)%60%20%7C%20%60True%60%20%7C%0A%20%20%20%20%7C%20%60psgc.str%5B%3A5%5D%60%20%7C%20%60%2201028%22%60%3A%20the%20first%20five%20characters%2C%20as%20slicing%20a%20text%20%7C%0A%20%20%20%20%7C%20%60psgc.str%5B%3A5%5D%20%2B%20%2200000%22%60%20%7C%20%60%220102800000%22%60%3A%20adding%20text%20to%20a%20text%20column%20adds%20it%20to%20every%20value%20%7C%0A%20%20%20%20%7C%20%60hh_id.str.removeprefix(%22HH-%22)%60%20%7C%20%60%220001%22%60%20%7C%0A%20%20%20%20%7C%20%60codes.str.zfill(10)%60%20%7C%20pads%20with%20zeros%20on%20the%20left%20to%2010%20characters%20(how%20module%203%20restored%20the%20codes%20Excel%20lost)%20%7C%0A%0A%20%20%20%20A%20missing%20value%20stays%20missing%20through%20the%20methods%20that%20give%20text%20or%20numbers%3B%20the%20ones%20that%20answer%20yes%20or%20no%2C%20such%20as%20%60startswith%60%2C%20give%20%60False%60%20for%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20_psgc%20%3D%20raw_households%5B%22psgc%22%5D%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22check%22%3A%20%5B%22every%20code%20has%2010%20characters%22%2C%20%22codes%20that%20start%20with%200%22%2C%20%22the%20first%20IDs%2C%20without%20'HH-'%22%5D%2C%0A%20%20%20%20%20%20%20%20%22result%22%3A%20%5Bstr(_psgc.str.len().eq(10).all())%2C%20f%22%7Bint(_psgc.str.startswith('0').sum())%3A%2C%7D%22%2C%20%22%2C%20%22.join(raw_households%5B%22hh_id%22%5D.str.removeprefix(%22HH-%22).head(3))%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.3.%20The%20province%20in%20the%20code%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20PSGC%20code%20is%20built%20from%20parts.%20Its%20first%20two%20digits%20are%20the%20region%20and%20the%20next%20three%20the%20province%2C%20so%20the%20province's%20own%20code%20is%20those%20five%20digits%20followed%20by%20five%20zeros.%20That%20is%20the%20key%20that%20joins%20a%20household%20to%20the%20province%20rows%20of%20the%20PSA%20code%20list%20(%60course.code_list()%5B%22province_code%22%5D%60)%2C%20and%20it%20only%20works%20while%20the%20code%20is%20text%3A%20as%20a%20number%2C%20the%20leading%20zero%20of%20%600102800000%60%20is%20gone.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20column%20of%2010-digit%20PSGC%20codes%2C%20as%20text%20%E2%86%92%20a%20column%20of%20province%20codes%2C%20as%20text%2C%20in%20the%20same%20order.%0A%0A%20%20%20%20%7C%20psgc%20%7C%20%E2%86%92%20province%20code%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%220102820000%22%60%20%7C%20%60%220102800000%22%60%20%7C%0A%20%20%20%20%7C%20%60%221204712000%22%60%20%7C%20%60%221204700000%22%60%20%7C%0A%0A%20%20%20%20Tools%3A%20%60.str%5B%3A5%5D%60%20and%20adding%20a%20text%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20province_code(psgc%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20province's%20PSGC%20code%20for%20each%2010-digit%20code%3A%20its%20first%20five%20digits%20followed%20by%20five%20zeros%2C%20as%20text.%22%22%22%0A%20%20%20%20%23%20Exercise%205.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20first%20five%20characters%20of%20each%20code%20(.str%5B%3A5%5D)%0A%20%20%20%20%23%20Step%202%3A%20add%20%2200000%22%20to%20each%2C%20and%20return%20the%20column%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_households)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.3%22%2C%20province_code%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%20codes%2C%20two%20starting%20with%200%22%2C%20(pd.Series(%5B%220102820000%22%2C%20%221204712000%22%2C%20%220603019000%22%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20codes%22%2C%20(raw_households%5B%22psgc%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%22columns%20of%20codes%22%2C%20summarize%3Dlambda%20codes%3A%20list(codes)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20five%20characters%20(region%20and%20province)%2C%20then%20add%20five%20zeros%3B%20the%20result%20is%20text%20of%2010%20characters.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20province%20codes%20of%20the%20survey's%20households%2C%20joined%20to%20the%20PSA%20code%20list.%20Does%20every%20household%20find%20its%20province%3F%0A%20%20%20%20def%20_try_province()%3A%0A%20%20%20%20%20%20%20%20codes%20%3D%20attempt(province_code%2C%20raw_households%5B%22psgc%22%5D)%0A%20%20%20%20%20%20%20%20if%20codes%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20provinces%20%3D%20course.code_list().drop_duplicates(%22province_code%22).set_index(%22province_code%22)%5B%22province%22%5D%0A%20%20%20%20%20%20%20%20found%20%3D%20pd.Series(list(codes)%2C%20index%3Draw_households.index).map(provinces)%0A%20%20%20%20%20%20%20%20same%20%3D%20int((found%20%3D%3D%20raw_households%5B%22province%22%5D).sum())%0A%20%20%20%20%20%20%20%20return%20mo.md(f%22First%20codes%3A%20%60%7Blist(codes)%5B%3A3%5D%7D%60.%20**%7Bsame%3A%2C%7D%20of%20%7Blen(raw_households)%3A%2C%7D**%20households%20find%20their%20own%20province%20in%20the%20code%20list.%22)%0A%0A%20%20%20%20_try_province()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.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%20numbers%20such%20as%20%60102800000%60%20%7C%20the%20code%20was%20made%20a%20number%20first%2C%20which%20drops%20the%20leading%20zero%20%7C%20keep%20the%20code%20as%20text%3A%20%60.str%5B%3A5%5D%60%20%7C%0A%20%20%20%20%7C%20%60%220102000000%22%60%20for%20%60%220102820000%22%60%20%7C%20four%20characters%20kept%20instead%20of%20five%20%7C%20%60.str%5B%3A5%5D%60%20%7C%0A%20%20%20%20%7C%20%60%2201028%22%60%20%7C%20the%20five%20zeros%20were%20not%20added%20%7C%20%60%2B%20%2200000%22%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20Can%20only%20use%20.str%20accessor%20with%20string%20values*%20%7C%20the%20column%20is%20not%20text%20(read%20without%20%60dtype%3D%7B%22psgc%22%3A%20str%7D%60)%20%7C%20%60raw_households%5B%22psgc%22%5D.dtype%60%20should%20be%20%60str%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_plots)%3A%0A%20%20%20%20_sqm%20%3D%20raw_plots%5Braw_plots%5B%22area_unit%22%5D%20%3D%3D%20%22sqm%22%5D%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20One%20unit%20for%20a%20measure%0A%0A%20%20%20%20A%20column%20should%20hold%20one%20measure%20in%20**one%20unit**.%20%60area%60%20doesn't%3A%20the%20codebook%20says%20its%20unit%20is%20in%20%60area_unit%60%2C%20and%20%7Blen(_sqm)%3A%2C%7D%20plots%20are%20in%20square%20meters%20(from%20%7B_sqm%5B%22area%22%5D.min()%3A%2C.0f%7D%20to%20%7B_sqm%5B%22area%22%5D.max()%3A%2C.0f%7D)%20among%20%7Bint(raw_plots%5B%22area_unit%22%5D.eq(%22ha%22).sum())%3A%2C%7D%20in%20hectares.%20Any%20sum%2C%20mean%20or%20yield%20that%20uses%20%60area%60%20as%20it%20is%20mixes%20the%20two.%20Here%20is%20the%20average%20rice%20yield%2C%20tonnes%20per%20hectare%2C%20before%20and%20after%20converting%20(one%20hectare%20is%2010%2C000%20square%20meters)%3A%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_rice%20%3D%20raw_plots%5Braw_plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20_ha%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%20SQM_PER_HA)%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22land%20in%20rice%2C%20ha%22%3A%20%5Bround(_rice%5B%22area%22%5D.sum())%2C%20round(_ha.sum())%5D%2C%0A%20%20%20%20%20%20%20%20%22average%20rice%20yield%2C%20t%2Fha%22%3A%20%5Bround((_rice%5B%22production_kg%22%5D%20%2F%20_rice%5B%22area%22%5D%20%2F%201000).mean()%2C%202)%2C%20round((_rice%5B%22production_kg%22%5D%20%2F%20_ha%20%2F%201000).mean()%2C%202)%5D%2C%0A%20%20%20%20%7D%2C%20index%3D%5B%22area%20as%20written%20(two%20units%20mixed)%22%2C%20%22every%20area%20in%20hectares%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Read%20as%20hectares%2C%20each%20square-meter%20plot%20is%2010%2C000%20times%20bigger%20than%20it%20is%3A%20the%20land%20in%20rice%20comes%20out%20hundreds%20of%20times%20too%20large%2C%20and%20those%20plots'%20yields%20come%20out%20nearly%20zero%2C%20pulling%20the%20average%20down.%0A%0A%20%20%20%20%60column.where(condition%2C%20other)%60%20is%20the%20tool%3A%20it%20keeps%20each%20value%20where%20the%20condition%20is%20%60True%60%2C%20and%20takes%20the%20value%20from%20%60other%60%20where%20it%20is%20%60False%60%20(the%20opposite%20of%20%60mask%60).%20Keep%20the%20unit%20column%20too%3A%20it%20records%20what%20was%20written%2C%20which%20is%20how%20anyone%20can%20check%20the%20conversion%20later.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.4.%20An%20area_ha%20column%20in%20the%20plots%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20module%202%20converted%20the%20areas%20as%20a%20NumPy%20array%20(Exercise%202.5)%3B%20here%20the%20conversion%20becomes%20a%20column%20of%20the%20table%2C%20so%20it%20travels%20with%20each%20plot%20through%20filters%2C%20groups%20and%20joins.%20Yields%2C%20totals%20of%20land%20and%20every%20per-hectare%20figure%20in%20the%20later%20modules%20use%20this%20column.%20It%20is%20also%20the%20second%20half%20of%20the%20course's%20cleaning%20of%20the%20plots%2C%20so%20section%205%20reuses%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20%E2%86%92%20the%20same%20table%20with%20one%20new%20column%20at%20the%20end%2C%20%60area_ha%60%3A%20the%20area%20in%20hectares%2C%20whatever%20unit%20it%20was%20written%20in.%20%60area%60%20and%20%60area_unit%60%20stay%20as%20they%20are.%0A%0A%20%20%20%20%7C%20area%20%7C%20area_unit%20%7C%20%E2%86%92%20area_ha%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201.5%20%7C%20ha%20%7C%201.5%20%7C%0A%20%20%20%20%7C%202500%20%7C%20sqm%20%7C%200.25%20%7C%0A%0A%20%20%20%20Tools%3A%20%60.where(condition%2C%20other)%60%2C%20above%3B%20%60assign%60%3B%20%60SQM_PER_HA%60%20is%20in%20the%20setup%20cell.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20add_area_ha(plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20with%20a%20new%20last%20column%2C%20area_ha%3A%20every%20area%20in%20hectares%20(square%20meters%20divided%20by%2010%2C000).%22%22%22%0A%20%20%20%20%23%20Exercise%205.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20area%20where%20area_unit%20is%20%22ha%22%3B%20the%20area%20divided%20by%20SQM_PER_HA%20where%20it%20is%20not%20(.where)%0A%20%20%20%20%23%20Step%202%3A%20return%20the%20table%20with%20that%20column%20added%20as%20area_ha%20(assign%20adds%20it%20at%20the%20end)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.4%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_plots)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.4%22%2C%20add_area_ha%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%20two%20in%20square%20meters%22%2C%20(plots_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22plots%20all%20in%20hectares%22%2C%20(plots_fixture()%5Blambda%20t%3A%20t%5B%22area_unit%22%5D%20%3D%3D%20%22ha%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots%22%2C%20(raw_plots%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%0A%20%20%20%20%20%20%20%20hint%3D%22Hectares%20stay%20as%20they%20are%3B%20square%20meters%20are%20divided%20by%2010%2C000%3B%20the%20new%20column%20is%20the%20last%20one%2C%20and%20the%20others%20don't%20change.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20plots%20in%20square%20meters%2C%20with%20your%20area%20in%20hectares%20beside%20them%0A%20%20%20%20def%20_try_area()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20with_ha%20%3D%20add_area_ha(raw_plots)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20if%20%22area_ha%22%20not%20in%20with_ha.columns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22No%20%60area_ha%60%20column.%20The%20columns%20are%3A%20%7B'%2C%20'.join(with_ha.columns)%7D.%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20with_ha%5Bwith_ha%5B%22area_unit%22%5D%20%3D%3D%20%22sqm%22%5D%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22area%22%2C%20%22area_unit%22%2C%20%22area_ha%22%5D%5D.head(5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22Largest%20area%20in%20hectares%3A%20**%7Bwith_ha%5B'area_ha'%5D.max()%3A.2f%7D**.%20Total%20land%3A%20**%7Bwith_ha%5B'area_ha'%5D.sum()%3A%2C.1f%7D%20ha**.%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_try_area()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.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%20areas%20of%20thousands%20of%20hectares%20%7C%20the%20square%20meters%20were%20left%20as%20they%20were%2C%20or%20multiplied%20%7C%20divide%20by%20%60SQM_PER_HA%60%20%7C%0A%20%20%20%20%7C%20the%20hectares%20are%20tiny%20too%20%7C%20every%20area%20was%20divided%2C%20not%20only%20the%20square%20meters%20%7C%20%60.where(area_unit%20%3D%3D%20%22ha%22%2C%20...)%60%20keeps%20the%20hectares%20%7C%0A%20%20%20%20%7C%202%2C500%20sqm%20gives%202.5%20or%2025%20%7C%20divided%20by%201%2C000%20or%20100%20%7C%2010%2C000%20square%20meters%20in%20a%20hectare%20%7C%0A%20%20%20%20%7C%20wrong%20only%20on%20the%20survey%20%7C%20%60area%60%20itself%20was%20changed%2C%20or%20the%20column%20is%20not%20last%20%7C%20add%20a%20new%20column%20with%20%60assign(area_ha%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**If%20you%20know%20pandas%202.**%20Text%20columns%20are%20%60str%60%20now%2C%20not%20%60object%60%3A%20an%20%60object%60%20column%20could%20hold%20a%20number%20or%20a%20list%20among%20its%20texts%20without%20complaint%2C%20and%20%60.str%60%20then%20gave%20%60NaN%60%20for%20those%20values.%20A%20%60str%60%20column%20holds%20only%20text%20and%20missing%20values.%20The%20categories%20of%20a%20category%20column%20made%20from%20text%20are%20%60str%60%20too.%20And%20%60groupby%60%20on%20a%20category%20column%20now%20gives%20only%20the%20categories%20that%20occur%20(%60observed%3DTrue%60)%3B%20pandas%202%20listed%20every%20category%2C%20with%20zeros%2C%20and%20warned%20that%20the%20default%20would%20change.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Dates%3A%20parsing%20day-first%2C%20periods%0A%0A%20%20%20%20**The%20problem.**%20The%20interview%20dates%20are%20written%20as%20%6019%2F12%2F2025%60%3A%20day%2C%20month%2C%20year%2C%20as%20most%20of%20the%20world%20writes%20them.%20A%20computer%20reading%20%6009%2F11%2F2025%60%20cannot%20know%20whether%20that%20is%20the%209th%20of%20November%20or%20the%2011th%20of%20September.%20Read%20the%20wrong%20way%2C%20a%20date%20either%20fails%20to%20read%20(there%20is%20no%2019th%20month)%20or%2C%20worse%2C%20reads%20without%20complaint%20as%20a%20different%20date.%20%60course.households()%60%20reads%20them%20correctly%3B%20to%20see%20the%20problem%2C%20read%20the%20column%20as%20it%20is%20in%20the%20file%2C%20as%20text%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20date_text%20%3D%20pd.read_csv(course.PATHS.survey%20%2F%20%22households.csv%22%2C%20dtype%3Dstr)%5B%22interview_date%22%5D%0A%20%20%20%20date_text.head(4).tolist()%0A%20%20%20%20return%20(date_text%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.to_datetime(text)%60%20turns%20text%20into%20dates.%20Without%20being%20told%20the%20format%2C%20it%20**guesses%20from%20the%20first%20value**%20and%20applies%20that%20guess%20to%20all%20of%20them.%20Here%20the%20first%20value%20is%20%6019%2F12%2F2025%60%2C%20which%20can%20only%20be%20day-first%2C%20so%20the%20guess%20happens%20to%20be%20right.%20Start%20from%20the%20fourth%20value%20instead%2C%20%6009%2F11%2F2025%60%2C%20and%20pandas%20guesses%20month-first%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(date_text)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20pd.to_datetime(date_text.iloc%5B3%3A%5D)%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.md(%22It%20read%20without%20an%20error.%22)%0A%20%20%20%20except%20ValueError%20as%20error%3A%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.callout(mo.md(f%22**ValueError**%3A%20%60%7Bstr(error).splitlines()%5B0%5D%7D%60%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20_shown%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Read%20the%20last%20line%20of%20the%20error%3A%20*time%20data%20%2220%2F12%2F2025%22%20doesn't%20match%20format%20%22%25m%2F%25d%2F%25Y%22*.%20pandas%20guessed%20month%2Fday%2Fyear%20from%20%6009%2F11%2F2025%60%2C%20then%20met%20a%20value%20with%20no%20month%2020.%20An%20error%20is%20the%20good%20outcome%3A%20it%20stops%20you.%20The%20bad%20outcome%20is%20a%20column%20where%20every%20day%20is%2012%20or%20less%2C%20or%20%60format%3D%22mixed%22%60%2C%20which%20guesses%20again%20for%20each%20value.%20Then%20nothing%20fails%2C%20and%20some%20dates%20are%20simply%20wrong.%20The%20chart%20below%20counts%20the%20interviews%20by%20month%2C%20read%20correctly%20and%20read%20with%20%60format%3D%22mixed%22%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(date_text)%3A%0A%20%20%20%20_right%20%3D%20pd.to_datetime(date_text%2C%20format%3D%22%25d%2F%25m%2F%25Y%22).dt.to_period(%22M%22).value_counts()%0A%20%20%20%20_mixed%20%3D%20pd.to_datetime(date_text%2C%20format%3D%22mixed%22).dt.to_period(%22M%22).value_counts()%0A%20%20%20%20_counts%20%3D%20pd.concat(%5B%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22month%22%3A%20_right.index.astype(str)%2C%20%22interviews%22%3A%20_right.to_numpy()%2C%20%22read%20as%22%3A%20'format%3D%22%25d%2F%25m%2F%25Y%22%20(day%20first)'%7D)%2C%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22month%22%3A%20_mixed.index.astype(str)%2C%20%22interviews%22%3A%20_mixed.to_numpy()%2C%20%22read%20as%22%3A%20'format%3D%22mixed%22%20(a%20guess%20per%20value)'%7D)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20alt.Chart(_counts%2C%20title%3D%22Interviews%20by%20month%2C%20as%20each%20reading%20gives%20them%22).mark_bar().encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22month%3AN%22%2C%20title%3D%22month%22%2C%20sort%3D%22ascending%22)%2C%0A%20%20%20%20%20%20%20%20xOffset%3D%22read%20as%3AN%22%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22interviews%3AQ%22%2C%20title%3D%22interviews%22)%2C%0A%20%20%20%20%20%20%20%20color%3Dalt.Color(%22read%20as%3AN%22%2C%20title%3DNone%2C%20legend%3Dalt.Legend(orient%3D%22top%22))%2C%0A%20%20%20%20%20%20%20%20tooltip%3D%5B%22read%20as%22%2C%20%22month%22%2C%20%22interviews%22%5D%2C%0A%20%20%20%20).properties(width%3D560%2C%20height%3D240)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(date_text)%3A%0A%20%20%20%20_mixed%20%3D%20pd.to_datetime(date_text%2C%20format%3D%22mixed%22)%0A%20%20%20%20_right%20%3D%20pd.to_datetime(date_text%2C%20format%3D%22%25d%2F%25m%2F%25Y%22)%0A%20%20%20%20_swapped%20%3D%20int((_mixed%20!%3D%20_right).sum())%0A%20%20%20%20_moved%20%3D%20int((_mixed%20%3C%20pd.Timestamp(%222025-10-01%22)).sum())%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20The%20survey%20ran%20from%20October%20to%20December%202025%20(the%20workbook's%20About%20sheet%20says%20so).%20Read%20with%20a%20guess%20per%20value%2C%20**%7B_swapped%3A%2C%7D%20interviews**%20get%20a%20different%20date%2C%20and%20**%7B_moved%3A%2C%7D**%20of%20them%20land%20in%20January%20to%20September%3A%20every%20date%20whose%20day%20is%2012%20or%20less%20had%20its%20day%20and%20month%20swapped%2C%20with%20no%20error%20and%20no%20warning.%20Checking%20a%20parsed%20date%20column%20against%20what%20you%20know%20(the%20fieldwork%20months)%20is%20how%20you%20catch%20it.%0A%0A%20%20%20%20The%20fix%20is%20to%20**say%20the%20format**%3A%20%60pd.to_datetime(text%2C%20format%3D%22%25d%2F%25m%2F%25Y%22)%60.%20Each%20%60%25%60%20code%20is%20one%20part%20of%20the%20date%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(%60%25y%60%20is%20two%20digits)%20%7C%20%602025%60%20%7C%0A%0A%20%20%20%20With%20the%20format%20given%2C%20pandas%20never%20guesses%2C%20and%20a%20value%20that%20doesn't%20fit%20raises%20an%20error%20naming%20it.%20(%60dayfirst%3DTrue%60%20also%20reads%20these%20dates%20correctly%2C%20but%20it%20is%20a%20preference%2C%20not%20a%20rule%3A%20the%20format%20is.)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.5.%20Read%20the%20dates%20day-first%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20count%20by%20month%2C%20every%20time%20between%20two%20dates%20and%20every%20check%20against%20the%20fieldwork%20period%20depends%20on%20the%20dates%20being%20the%20right%20ones.%20A%20wrong%20guess%20that%20doesn't%20fail%20is%20the%20one%20nobody%20notices.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20column%20of%20dates%20written%20as%20text%2C%20day%2Fmonth%2Fyear%20%E2%86%92%20a%20column%20of%20dates%20(%60datetime64%60)%2C%20each%20the%20date%20that%20was%20written.%0A%0A%20%20%20%20%7C%20text%20%7C%20%E2%86%92%20date%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%2203%2F11%2F2025%22%60%20%7C%203%20November%202025%20%7C%0A%20%20%20%20%7C%20%60%2225%2F12%2F2025%22%60%20%7C%2025%20December%202025%20%7C%0A%0A%20%20%20%20Tools%3A%20%60pd.to_datetime(text%2C%20format%3D...)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20parse_day_first(text%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20dates%20written%20as%20day%2Fmonth%2Fyear%20text%2C%20as%20dates.%22%22%22%0A%20%20%20%20%23%20Exercise%205.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20pd.to_datetime%20with%20the%20format%20that%20says%20day%2C%20then%20month%2C%20then%20a%20four-digit%20year%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.5%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(date_text)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.5%22%2C%20parse_day_first%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22dates%20whose%20first%20value%20could%20be%20either%20way%2C%20then%20a%20day%20over%2012%22%2C%20(pd.Series(%5B%2203%2F11%2F2025%22%2C%20%2225%2F12%2F2025%22%2C%20%2212%2F10%2F2025%22%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22dates%20whose%20days%20are%20all%2012%20or%20less%22%2C%20(pd.Series(%5B%2210%2F10%2F2025%22%2C%20%2201%2F12%2F2025%22%2C%20%2205%2F11%2F2025%22%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20interview%20dates%22%2C%20(date_text%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22columns%20of%20dates%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Give%20the%20format%3A%20day%2C%20month%2C%20then%20a%20four-digit%20year.%20A%20guess%20(no%20format%2C%20or%20format%3D%5C%22mixed%5C%22)%20reads%20days%20of%2012%20or%20less%20as%20months.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(date_text)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20dates.%20Do%20they%20all%20fall%20in%20October%20to%20December%202025%2C%20when%20the%20survey%20ran%3F%0A%20%20%20%20def%20_try_dates()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20dates%20%3D%20parse_day_first(date_text)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20except%20ValueError%20as%20error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22**ValueError**%3A%20%60%7Bstr(error).splitlines()%5B0%5D%7D%60.%20Read%20the%20format%20in%20the%20message%3A%20is%20it%20day%20first%3F%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20%20%20%20%20return%20mo.md(f%22From%20**%7Bdates.min()%3A%25d%20%25B%20%25Y%7D**%20to%20**%7Bdates.max()%3A%25d%20%25B%20%25Y%7D**%3B%20type%20%60%7Bdates.dtype%7D%60.%22)%0A%0A%20%20%20%20_try_dates()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.5%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*ValueError%3A%20time%20data%20%2225%2F12%2F2025%22%20doesn't%20match%20format%20%22%25m%2F%25d%2F%25Y%22*%20%7C%20no%20format%20given%2C%20and%20pandas%20guessed%20month-first%20from%20the%20first%20value%20%7C%20%60format%3D%22%25d%2F%25m%2F%25Y%22%60%20%7C%0A%20%20%20%20%7C%20wrong%20on%20%22days%20all%2012%20or%20less%22%2C%20with%20no%20error%20%7C%20a%20guess%20(no%20format%2C%20or%20%60format%3D%22mixed%22%60)%20read%20them%20month-first%20%7C%20%60format%3D%22%25d%2F%25m%2F%25Y%22%60%20%7C%0A%20%20%20%20%7C%20*ValueError%20%E2%80%A6%20doesn't%20match%20format%20%22%25d%2F%25m%2F%25y%22*%20%7C%20%60%25y%60%20is%20a%20two-digit%20year%20%7C%20%60%25Y%60%2C%20capital%20%7C%0A%20%20%20%20%7C%20*ValueError%20%E2%80%A6%20doesn't%20match%20format%20%22%25m%2F%25d%2F%25Y%22*%20%7C%20day%20and%20month%20swapped%20in%20the%20format%20%7C%20%60%25d%60%20first%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%20Parts%20of%20a%20date%2C%20and%20periods%0A%0A%20%20%20%20A%20date%20column%20has%20its%20own%20methods%20behind%20%60.dt%60%2C%20as%20text%20has%20%60.str%60%3A%20%60dates.dt.month%60%20(a%20number%2C%201%20to%2012)%2C%20%60dates.dt.day_name()%60%2C%20%60dates.dt.year%60.%20Subtracting%20two%20dates%20gives%20a%20**timedelta**%2C%20a%20length%20of%20time%3A%20%60dates.max()%20-%20dates.min()%60.%0A%0A%20%20%20%20Often%20the%20question%20is%20about%20a%20whole%20month%2C%20not%20a%20day%3A%20how%20many%20interviews%20were%20done%20in%20November%202025%3F%20%60dates.dt.month%60%20gives%2011%20for%20November%20of%20*any*%20year%2C%20so%20a%20survey%20that%20runs%20from%20December%20into%20January%20would%20add%20the%20two%20Januaries%20together.%20A%20**period**%20is%20a%20span%20of%20time%20with%20its%20year%3A%20%60dates.dt.to_period(%22M%22)%60%20turns%20each%20date%20into%20its%20month%2C%20such%20as%20%602025-11%60%2C%20and%20counts%20by%20period%20never%20mix%20years.%20(%60%22Q%22%60%20gives%20quarters%2C%20%60%22Y%22%60%20years.)%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_dates%20%3D%20pd.to_datetime(pd.Series(%5B%2230%2F12%2F2025%22%2C%20%2202%2F01%2F2026%22%2C%20%2215%2F01%2F2025%22%5D)%2C%20format%3D%22%25d%2F%25m%2F%25Y%22)%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22date%22%3A%20_dates.dt.strftime(%22%25d%20%25b%20%25Y%22)%2C%0A%20%20%20%20%20%20%20%20%22.dt.month%22%3A%20_dates.dt.month%2C%0A%20%20%20%20%20%20%20%20'.dt.to_period(%22M%22)'%3A%20_dates.dt.to_period(%22M%22).astype(str)%2C%0A%20%20%20%20%20%20%20%20%22days%20since%20the%20first%22%3A%20(_dates%20-%20_dates.min()).dt.days%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.6.%20Interviews%20by%20month%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20fieldwork%20report%20starts%20with%20how%20many%20interviews%20were%20done%20each%20month.%20Counting%20by%20period%20keeps%20each%20month%20with%20its%20year%2C%20and%20a%20count%20in%20the%20wrong%20month%20is%20the%20first%20sign%20of%20a%20date%20read%20the%20wrong%20way.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20column%20of%20dates%20%E2%86%92%20the%20number%20of%20interviews%20in%20each%20month%2C%20as%20a%20column%20indexed%20by%20month%20periods%20(%602025-10%60%2C%20%602025-11%60%2C%20%E2%80%A6)%2C%20in%20order%20of%20month.%0A%0A%20%20%20%20%7C%20month%20%7C%20interviews%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%202025-10%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%202025-11%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%202025-12%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20Tools%3A%20%60.dt.to_period(%22M%22)%60%2C%20above%3B%20%60value_counts()%60%20and%20%60sort_index()%60%20(%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%2C%20section%203)%2C%20or%20%60groupby(...).size()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20interviews_by_month(dates%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20number%20of%20dates%20in%20each%20month%3A%20a%20column%20indexed%20by%20monthly%20periods%2C%20in%20order.%22%22%22%0A%20%20%20%20%23%20Exercise%205.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20turn%20each%20date%20into%20its%20month%2C%20as%20a%20period%20(.dt.to_period)%0A%20%20%20%20%23%20Step%202%3A%20count%20each%20month%2C%20and%20put%20the%20months%20in%20order%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.6%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_households)%3A%0A%20%20%20%20_across_years%20%3D%20pd.to_datetime(pd.Series(%5B%2230%2F12%2F2025%22%2C%20%2202%2F01%2F2026%22%2C%20%2215%2F01%2F2025%22%2C%20%2201%2F12%2F2025%22%5D)%2C%20format%3D%22%25d%2F%25m%2F%25Y%22)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.6%22%2C%20interviews_by_month%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%20dates%20across%20two%20years'%20Decembers%20and%20Januaries%22%2C%20(_across_years%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20interview%20dates%22%2C%20(raw_households%5B%22interview_date%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%22columns%20of%20dates%22%2C%20summarize%3Dlambda%20counts%3A%20list(counts.items())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20by%20month%20periods%20(to_period)%2C%20not%20month%20numbers%2C%20so%20each%20month%20keeps%20its%20year%3B%20then%20put%20them%20in%20order%20of%20month.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20%23%20Try%20it%3A%20interviews%20by%20month%20in%20the%20survey%0A%20%20%20%20try_it(interviews_by_month%2C%20raw_households%5B%22interview_date%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.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%20months%20as%20%6010%60%2C%20%6011%60%2C%20%6012%60%20%7C%20%60.dt.month%60%20gives%20the%20month's%20number%20without%20its%20year%20%7C%20%60.dt.to_period(%22M%22)%60%20%7C%0A%20%20%20%20%7C%20months%20as%20text%20such%20as%20%60%222025-10%22%60%20%7C%20%60.dt.strftime(...)%60%20makes%20text%2C%20not%20periods%20%7C%20%60.dt.to_period(%22M%22)%60%20%7C%0A%20%20%20%20%7C%20the%20right%20counts%2C%20in%20the%20wrong%20order%20%7C%20%60value_counts()%60%20puts%20the%20most%20frequent%20first%20%7C%20%60.sort_index()%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20Can%20only%20use%20.dt%20accessor%20with%20datetimelike%20values*%20%7C%20the%20dates%20are%20still%20text%20%7C%20parse%20them%20first%20(Exercise%205.5)%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**If%20you%20know%20pandas%202.**%20Dates%20read%20from%20text%20are%20now%20stored%20to%20the%20microsecond%2C%20%60datetime64%5Bus%5D%60%2C%20not%20the%20nanosecond%2C%20%60datetime64%5Bns%5D%60%3A%20the%20interview%20dates%20show%20%60datetime64%5Bus%5D%60.%20The%20dates%20themselves%20are%20the%20same.%20It%20matters%20only%20if%20your%20code%20checks%20for%20the%20exact%20type%20%60datetime64%5Bns%5D%60.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204%20%C2%B7%20An%20introduction%20to%20time%20series%3A%20farmgate%20prices%20by%20month%2C%20a%20date%20index%2C%20resample%2C%20rolling%2C%20shift%20and%20growth%0A%0A%20%20%20%20**The%20problem.**%20A%20**time%20series**%20is%20one%20value%20measured%20at%20regular%20times%3A%20here%2C%20the%20farmgate%20price%20of%20a%20commodity%20each%20month%2C%20from%20January%202022%20to%20December%202025.%20The%20questions%20are%20about%20change%3A%20is%20palay%20dearer%20than%20a%20year%20ago%3F%20Is%20the%20rise%20steady%2C%20or%20a%20seasonal%20swing%3F%20Each%20answer%20compares%20a%20month%20with%20another%20month%2C%20so%20it%20depends%20on%20knowing%20which%20month%20each%20row%20is%2C%20and%20on%20every%20month%20being%20there.%20The%20prices%20come%20as%20one%20file%20a%20month%2C%20and%20the%20file%20for%20**September%202023%20never%20arrived**.%20%60course.prices()%60%20reads%20every%20file%20into%20one%20long%20table%2C%20and%20that%20month%20simply%20has%20no%20rows%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%20prices%20%3D%20course.prices()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60prices%60%3A%20**%7Blen(prices)%3A%2C%7D%20rows**%3A%20%7Bprices%5B'commodity'%5D.nunique()%7D%20commodities%20%C3%97%20%7Bprices%5B'province'%5D.nunique()%7D%20provinces%20%C3%97%20**%7Bprices%5B'month'%5D.nunique()%7D%20months**.%20The%20column%20%60month%60%20is%20of%20type%20%60%7Bprices%5B'month'%5D.dtype%7D%60%2C%20a%20monthly%20period.%22)%2C%0A%20%20%20%20%20%20%20%20prices.head(4)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(prices%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%20From%20January%202022%20to%20December%202025%20is%2048%20months%3B%20the%20table%20has%2047.%20Nothing%20in%20it%20says%20which%20one%20is%20missing%2C%20and%20the%20tools%20below%20would%20happily%20compare%20August%20with%20October%20as%20if%20they%20were%20neighbors.%20First%2C%20a%20**date%20index**%3A%20one%20row%20per%20month%2C%20the%20month%20as%20the%20index.%0A%0A%20%20%20%20%23%23%23%20A%20date%20index%3A%20periods%20or%20timestamps%0A%0A%20%20%20%20A%20monthly%20series%20can%20be%20labeled%20two%20ways%2C%20and%20pandas%20supports%20both%3A%0A%0A%20%20%20%20%7C%20%7C%20a%20%60PeriodIndex%60%3A%20%602023-09%60%20%7C%20a%20%60DatetimeIndex%60%3A%20%602023-09-01%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20a%20label%20is%20%7C%20a%20whole%20month%20%7C%20one%20moment%3A%20midnight%20on%20the%201st%20%7C%0A%20%20%20%20%7C%20from%20%60course.prices()%60%20%7C%20as%20it%20comes%20(%60month%60%20is%20a%20period)%20%7C%20%60.to_timestamp()%60%20first%20%7C%0A%20%20%20%20%7C%20every%20month%2C%20the%20missing%20as%20NaN%20%7C%20%60.reindex(pd.period_range(first%2C%20last%2C%20freq%3D%22M%22))%60%20%7C%20%60.asfreq(%22MS%22)%60%20(month%20start)%20%7C%0A%20%20%20%20%7C%20quarters%20%7C%20%60.resample(%22Q%22)%60%20%7C%20%60.resample(%22QE%22)%60%20(quarter%20end)%20or%20%60%22QS%22%60%20%7C%0A%20%20%20%20%7C%20a%20matplotlib%20chart%20%7C%20%60.to_timestamp()%60%20first%3A%20matplotlib%20can't%20draw%20Periods%20%7C%20as%20it%20is%20%7C%0A%0A%20%20%20%20This%20module%20uses%20**periods**.%20A%20farmgate%20price%20is%20a%20price%20*for%20the%20month*%2C%20not%20at%20midnight%20on%20its%20first%20day%2C%20and%20a%20period%20says%20so%3A%20%602023-09%60%20prints%20as%20what%20it%20means%2C%20and%20every%20tool%20here%20works%20on%20it.%20The%20one%20cost%3A%20on%20a%20period%20index%20%60asfreq(%22M%22)%60%20does%20not%20add%20missing%20months%20(it%20only%20re-labels%20each%20period)%2C%20so%20the%20missing%20months%20are%20made%20with%20%60reindex%60.%20Charts%20convert%20to%20timestamps%20at%20the%20last%20step.%0A%0A%20%20%20%20One%20series%2C%20palay's%20average%20price%20across%20the%20seven%20provinces%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(prices)%3A%0A%20%20%20%20palay%20%3D%20prices%5Bprices%5B%22commodity%22%5D%20%3D%3D%20%22Palay%22%5D.groupby(%22month%22)%5B%22price%22%5D.mean()%0A%20%20%20%20every_month%20%3D%20pd.period_range(palay.index.min()%2C%20palay.index.max()%2C%20freq%3D%22M%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60palay%60%20has%20**%7Blen(palay)%7D%20rows**%3B%20from%20its%20first%20to%20its%20last%20month%20there%20are%20**%7Blen(every_month)%7D**%20(%60every_month%60).%20Around%20the%20gap%2C%20before%20and%20after%20%60reindex(every_month)%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.hstack(%5Bpalay.loc%5B%222023-07%22%3A%222023-11%22%5D.round(2)%2C%20palay.reindex(every_month).loc%5B%222023-07%22%3A%222023-11%22%5D.round(2)%5D%2C%20justify%3D%22start%22%2C%20gap%3D3)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20every_month%2C%20palay%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.period_range(first%2C%20last%2C%20freq%3D%22M%22)%60%20lists%20every%20month%20from%20the%20first%20to%20the%20last.%20%60series.reindex(those%20months)%60%20puts%20the%20series%20on%20that%20list%3A%20months%20it%20has%20keep%20their%20value%2C%20the%20month%20it%20lacks%20gets%20NaN.%20The%20gap%20is%20now%20a%20row%20you%20can%20see%2C%20count%20(%60isna().sum()%60)%20and%20report%2C%20and%20every%20month%20sits%20exactly%20one%20row%20after%20the%20month%20before%20it.%20That%20last%20property%20is%20what%20the%20rest%20of%20this%20section%20depends%20on.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.7.%20One%20price%20series%2C%20every%20month%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20later%20step%20(quarters%2C%20a%20rolling%20mean%2C%20growth%20on%20the%20month%20and%20on%20the%20year)%20assumes%20one%20row%20per%20month.%20This%20function%20makes%20that%20true%20for%20any%20commodity%2C%20with%20the%20missing%20month%20visible%20instead%20of%20skipped.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20prices%20table%20and%20a%20commodity%20%E2%86%92%20that%20commodity's%20average%20price%20across%20the%20provinces%20in%20each%20month%2C%20as%20a%20column%20indexed%20by%20monthly%20periods%2C%20with%20**every**%20month%20from%20the%20first%20to%20the%20last%20and%20NaN%20where%20a%20month%20has%20no%20prices.%0A%0A%20%20%20%20%7C%20month%20%7C%20price%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%202023-08%20%7C%2022.24%20%7C%0A%20%20%20%20%7C%202023-09%20%7C%20NaN%20%7C%0A%20%20%20%20%7C%202023-10%20%7C%2021.27%20%7C%0A%0A%20%20%20%20Don't%20fill%20the%20gap%3A%20no%20price%20was%20collected%20that%20month%20(section%201's%20rule%20for%20amounts).%0A%0A%20%20%20%20Tools%3A%20a%20filter%20(%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%2C%20section%203)%2C%20%60groupby%60%20and%20%60mean%60%2C%20%60pd.period_range%60%20and%20%60reindex%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20monthly_series(prices%3A%20pd.DataFrame%2C%20commodity%3A%20str)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22A%20commodity's%20average%20price%20across%20provinces%20for%20every%20month%20from%20the%20first%20to%20the%20last%20(NaN%20where%20none).%22%22%22%0A%20%20%20%20%23%20Exercise%205.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20rows%20of%20this%20commodity%3B%20average%20the%20price%20by%20month%20(groupby)%0A%20%20%20%20%23%20Step%202%3A%20every%20month%20from%20the%20first%20to%20the%20last%20(pd.period_range%2C%20freq%3D%22M%22)%0A%20%20%20%20%23%20Step%203%3A%20reindex%20the%20averages%20to%20every%20month%2C%20and%20return%20the%20result%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.7%22)%0A%0A%0A%40app.function%0Adef%20prices_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20small%20prices%20table%3A%20two%20commodities%2C%20two%20provinces%2C%20January%20to%20April%202024%20with%20March%20missing.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20month%2C%20palay%2C%20tomato%20in%20%5B(%222024-01%22%2C%20(20.0%2C%2021.0)%2C%20(30.0%2C%2034.0))%2C%20(%222024-02%22%2C%20(21.0%2C%2022.0)%2C%20(28.0%2C%2031.0))%2C%20(%222024-04%22%2C%20(22.0%2C%2024.0)%2C%20(40.0%2C%2044.0))%5D%3A%0A%20%20%20%20%20%20%20%20for%20province%2C%20p%2C%20t%20in%20zip(%5B%22Iloilo%22%2C%20%22Isabela%22%5D%2C%20palay%2C%20tomato)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(pd.Period(month%2C%20freq%3D%22M%22)%2C%20province%2C%20%22Palay%22%2C%20p)%2C%20(pd.Period(month%2C%20freq%3D%22M%22)%2C%20province%2C%20%22Tomato%22%2C%20t)%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22month%22%2C%20%22province%22%2C%20%22commodity%22%2C%20%22price%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(prices)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.7%22%2C%20monthly_series%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22palay%20in%20a%20small%20table%20with%20March%20missing%22%2C%20(prices_fixture()%2C%20%22Palay%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22palay%20in%20the%20course's%20prices%22%2C%20(prices%2C%20%22Palay%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22tomato%20in%20the%20course's%20prices%22%2C%20(prices%2C%20%22Tomato%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22series%22%2C%20summarize%3Dlambda%20series%3A%20list(series.items())%2C%0A%20%20%20%20%20%20%20%20hint%3D%22One%20row%20for%20every%20month%20from%20the%20first%20to%20the%20last%2C%20labeled%20by%20monthly%20periods%3B%20the%20missing%20month%20is%20there%2C%20as%20NaN%2C%20not%20filled.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(prices)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20series%20for%20palay.%20How%20many%20months%2C%20and%20how%20many%20are%20missing%3F%0A%20%20%20%20def%20_try_series()%3A%0A%20%20%20%20%20%20%20%20series%20%3D%20attempt(monthly_series%2C%20prices%2C%20%22Palay%22)%0A%20%20%20%20%20%20%20%20if%20series%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%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(series)%7D%20months**%2C%20**%7Bint(series.isna().sum())%7D**%20of%20them%20missing%3B%20index%20type%20%60%7Bseries.index.dtype%7D%60.%20Around%20September%202023%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20series.loc%5B%222023-07%22%3A%222023-11%22%5D%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_try_series()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.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%2047%20months%20instead%20of%2048%20(3%20instead%20of%204%20in%20the%20small%20table)%20%7C%20the%20missing%20month%20was%20not%20added%20%7C%20%60reindex(pd.period_range(...))%60%20%7C%0A%20%20%20%20%7C%20still%2047%20after%20%60asfreq(%22M%22)%60%20%7C%20on%20a%20period%20index%2C%20%60asfreq%60%20re-labels%3B%20it%20adds%20no%20months%20%7C%20%60reindex%60%20%7C%0A%20%20%20%20%7C%20the%20missing%20month%20has%20a%20price%20%7C%20filled%20with%20%60ffill%60%2C%20%60interpolate%60%20or%20%60fillna%60%20%7C%20leave%20it%20NaN%20%7C%0A%20%20%20%20%7C%20months%20shown%20as%20%602023-09-01%60%20%7C%20the%20index%20was%20turned%20into%20timestamps%20%7C%20keep%20the%20periods%20for%20this%20function%20%7C%0A%20%20%20%20%7C%20prices%20too%20high%20or%20too%20low%20%7C%20the%20commodity%20was%20not%20filtered%2C%20or%20%60sum%60%2F%60median%60%20used%20%7C%20%60prices%5B%22commodity%22%5D%20%3D%3D%20commodity%60%2C%20then%20%60mean()%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%20resample%3A%20from%20months%20to%20quarters%0A%0A%20%20%20%20%60series.resample(%22Q%22)%60%20groups%20a%20time%20series%20into%20quarters%2C%20as%20%60groupby%60%20groups%20by%20a%20column%2C%20and%20%60.mean()%60%20(or%20%60.sum()%60%2C%20%60.count()%60)%20summarizes%20each.%20Ask%20for%20%60count%60%20beside%20the%20%60mean%60%2C%20and%20the%20gap%20shows%20itself%20again%3A%20the%20third%20quarter%20of%202023%20rests%20on%20two%20months%2C%20not%20three.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(every_month%2C%20palay)%3A%0A%20%20%20%20palay.reindex(every_month).resample(%22Q%22).agg(%5B%22mean%22%2C%20%22count%22%5D).loc%5B%222023Q1%22%3A%222024Q2%22%5D.round(2)%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%20rolling%3A%20a%20moving%20average%0A%0A%20%20%20%20Monthly%20prices%20jump%20about.%20A%20**rolling%20mean**%20averages%20each%20month%20with%20the%20months%20just%20before%20it%3A%20%60series.rolling(3).mean()%60%20is%2C%20for%20each%20month%2C%20the%20mean%20of%20that%20month%20and%20the%20two%20before.%20It%20smooths%20the%20jumps%20and%20keeps%20the%20trend.%20A%20window%20that%20includes%20the%20missing%20month%20gives%20NaN%2C%20so%20the%20smooth%20line%20stops%20at%20the%20gap%20rather%20than%20averaging%20two%20months%20and%20calling%20it%20three.%20Choose%20a%20commodity%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(prices)%3A%0A%20%20%20%20commodity_choice%20%3D%20mo.ui.dropdown(options%3Dsorted(prices%5B%22commodity%22%5D.unique())%2C%20value%3D%22Palay%22%2C%20label%3D%22Commodity%3A%22)%0A%20%20%20%20commodity_choice%0A%20%20%20%20return%20(commodity_choice%2C)%0A%0A%0A%40app.cell%0Adef%20_(commodity_choice%2C%20prices)%3A%0A%20%20%20%20_series%20%3D%20prices%5Bprices%5B%22commodity%22%5D%20%3D%3D%20commodity_choice.value%5D.groupby(%22month%22)%5B%22price%22%5D.mean()%0A%20%20%20%20_series%20%3D%20_series.reindex(pd.period_range(_series.index.min()%2C%20_series.index.max()%2C%20freq%3D%22M%22))%0A%20%20%20%20_when%20%3D%20_series.index.to_timestamp()%20%20%23%20matplotlib%20draws%20timestamps%2C%20not%20periods%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(8.5%2C%203.6))%0A%20%20%20%20_ax.plot(_when%2C%20_series.to_numpy()%2C%20marker%3D%22o%22%2C%20markersize%3D3%2C%20linewidth%3D1%2C%20color%3D%22%238a8f98%22%2C%20label%3D%22monthly%20average%22)%0A%20%20%20%20_ax.plot(_when%2C%20_series.rolling(3).mean().to_numpy()%2C%20linewidth%3D2.2%2C%20color%3D%22%23d1661b%22%2C%20label%3D%22rolling%203-month%20mean%22)%0A%20%20%20%20_gap%20%3D%20pd.Period(%222023-09%22%2C%20freq%3D%22M%22).to_timestamp()%0A%20%20%20%20_ax.axvspan(_gap%20-%20pd.Timedelta(days%3D15)%2C%20_gap%20%2B%20pd.Timedelta(days%3D15)%2C%20color%3D%22%23d64545%22%2C%20alpha%3D0.12%2C%20label%3D%22no%20file%3A%20September%202023%22)%0A%20%20%20%20_ax.set_title(f%22%7Bcommodity_choice.value%7D%3A%20farmgate%20price%2C%20average%20of%20seven%20provinces%22)%0A%20%20%20%20_ax.set_ylabel(%22pesos%20per%20kilogram%22)%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20fontsize%3D8%2C%20loc%3D%22upper%20left%22)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20line%20breaks%20at%20September%202023%3A%20matplotlib%20leaves%20a%20gap%20where%20a%20value%20is%20NaN%2C%20and%20the%20rolling%20mean%20stays%20missing%20until%20three%20real%20months%20follow.%20Had%20the%20month%20been%20skipped%20instead%20of%20made%20visible%2C%20both%20lines%20would%20run%20straight%20through%20it.%0A%0A%20%20%20%20%23%23%23%20shift%20and%20growth%3A%20compare%20with%20the%20right%20month%0A%0A%20%20%20%20%60series.shift(1)%60%20moves%20every%20value%20down%20**one%20row**%3A%20each%20row%20then%20holds%20the%20previous%20row's%20value%20beside%20its%20own.%20%60series.pct_change()%60%20uses%20it%20to%20compute%20the%20change%20from%20the%20previous%20row%2C%20as%20a%20fraction%3A%20%60(this%20%E2%88%92%20previous)%20%2F%20previous%60%3B%20times%20100%2C%20the%20percent%20change%20**month%20on%20month**.%20%60pct_change(12)%60%20compares%20with%2012%20rows%20earlier%3A%20**year%20on%20year**%2C%20which%20removes%20the%20seasonal%20swing%2C%20because%20it%20compares%20a%20month%20with%20the%20same%20month%20a%20year%20before.%0A%0A%20%20%20%20The%20catch%20is%20in%20the%20word%20*row*.%20Both%20count%20rows%2C%20not%20months.%20Here%20is%20year-on-year%20growth%20for%20palay%2C%20computed%20on%20the%20series%20as%20the%20files%20give%20it%2C%20and%20on%20the%20series%20with%20every%20month%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(every_month%2C%20palay)%3A%0A%20%20%20%20_skipped%20%3D%20palay.pct_change(12)%20*%20100%0A%20%20%20%20_complete%20%3D%20palay.reindex(every_month).pct_change(12)%20*%20100%0A%20%20%20%20_compare%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22compared%20with%20(12%20rows%20back)%2C%20files%20as%20given%22%3A%20%5Bstr(palay.index%5Bpalay.index.get_loc(m)%20-%2012%5D)%20if%20m%20in%20palay.index%20else%20%22(no%20row)%22%20for%20m%20in%20every_month%5D%2C%0A%20%20%20%20%20%20%20%20%22yoy%20%25%2C%20files%20as%20given%22%3A%20_skipped.reindex(every_month).round(1)%2C%0A%20%20%20%20%20%20%20%20%22yoy%20%25%2C%20every%20month%22%3A%20_complete.round(1)%2C%0A%20%20%20%20%7D%2C%20index%3Devery_month)%0A%20%20%20%20_compare.loc%5B%222024-07%22%3A%222024-11%22%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%20With%20the%20month%20skipped%2C%20%2212%20rows%20back%22%20from%20August%202024%20is%20July%202023%3A%20thirteen%20months.%20Every%20year-on-year%20figure%20from%20October%202023%20to%20September%202024%20compares%20across%20the%20gap%20and%20is%20wrong%2C%20with%20no%20error%20and%20nothing%20missing%20to%20warn%20you%3A%20August%202024%20shows%2011.0%25%20where%20the%20true%20figure%20is%206.6%25.%20On%20the%20series%20with%20every%20month%2C%2012%20rows%20back%20is%20always%2012%20months%20back%2C%20and%20the%20months%20whose%20comparison%20would%20need%20September%202023%20say%20NaN%2C%20which%20is%20the%20truth.%0A%0A%20%20%20%20So%20the%20order%20is%20always%3A%20**every%20month%20first%2C%20then%20shift.**%20(A%20period%20index%20can%20also%20shift%20by%20the%20calendar%2C%20%60pct_change(freq%3D%22M%22)%60%2C%20which%20matches%20labels%20instead%20of%20counting%20rows%3B%20reindexing%20first%20keeps%20the%20gap%20visible%20in%20the%20result%20as%20well.)%0A%0A%20%20%20%20%23%23%23%20Exercise%205.8.%20Growth%20on%20the%20month%20and%20on%20the%20year%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22palay%20is%20up%206.6%25%20on%20a%20year%20ago%22%20is%20the%20sentence%20a%20price%20bulletin%20leads%20with.%20It%20must%20compare%20August%20with%20August%2C%20even%20when%20a%20file%20is%20missing%20in%20between.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20monthly%20price%20series%20indexed%20by%20monthly%20periods%2C%20which%20may%20skip%20months%20%E2%86%92%20a%20table%20with%20one%20row%20for%20**every**%20month%20from%20the%20first%20to%20the%20last%20and%20three%20columns%3A%20%60price%60%20(NaN%20in%20the%20months%20skipped)%2C%20%60mom_pct%60%20(percent%20change%20from%20the%20month%20before)%20and%20%60yoy_pct%60%20(percent%20change%20from%20the%20same%20month%20a%20year%20before).%20A%20change%20that%20would%20need%20a%20missing%20month%20is%20NaN.%0A%0A%20%20%20%20%7C%20month%20%7C%20price%20%7C%20mom_pct%20%7C%20yoy_pct%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%202024-08%20%7C%2023.71%20%7C%20%E2%80%A6%20%7C%206.6%20%7C%0A%20%20%20%20%7C%202024-09%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20NaN%20%7C%0A%0A%20%20%20%20Tools%3A%20%60pd.period_range%60%20and%20%60reindex%60%2C%20above%3B%20%60pct_change(1)%60%20and%20%60pct_change(12)%60%2C%20times%20100%3B%20%60pd.DataFrame(%7B%22price%22%3A%20...%2C%20...%7D)%60%20to%20build%20the%20table.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20growth(monthly%3A%20pd.Series)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20month%20from%20the%20first%20to%20the%20last%3A%20the%20price%2C%20its%20percent%20change%20on%20the%20month%20and%20on%20the%20year.%22%22%22%0A%20%20%20%20%23%20Exercise%205.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20put%20the%20series%20on%20every%20month%20from%20its%20first%20to%20its%20last%20(period_range%20and%20reindex)%0A%20%20%20%20%23%20Step%202%3A%20month%20on%20month%3A%20pct_change()%20times%20100%3B%20year%20on%20year%3A%20pct_change(12)%20times%20100%0A%20%20%20%20%23%20Step%203%3A%20return%20a%20table%20with%20the%20columns%20price%2C%20mom_pct%20and%20yoy_pct%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.8%22)%0A%0A%0A%40app.function%0Adef%20monthly_fixture(skip%3A%20str%20%7C%20None)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22A%20price%20series%20for%20January%202023%20to%20April%202024%20(16%20months)%20rising%201%20peso%20a%20month%2C%20with%20one%20month%20left%20out.%22%22%22%0A%20%20%20%20months%20%3D%20pd.period_range(%222023-01%22%2C%20%222024-04%22%2C%20freq%3D%22M%22)%0A%20%20%20%20series%20%3D%20pd.Series(np.arange(20.0%2C%2036.0)%2C%20index%3Dmonths%2C%20name%3D%22price%22)%0A%20%20%20%20return%20series.drop(pd.Period(skip%2C%20freq%3D%22M%22))%20if%20skip%20else%20series%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(palay)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.8%22%2C%20growth%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%2216%20months%20with%20June%202023%20left%20out%22%2C%20(monthly_fixture(%222023-06%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%2216%20months%2C%20none%20left%20out%22%2C%20(monthly_fixture(None)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22palay's%20averages%20as%20the%20files%20give%20them%20(September%202023%20skipped)%22%2C%20(palay%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22series%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Every%20month%20first%20(reindex)%2C%20then%20pct_change%3A%20rows%20must%20be%20months.%20Changes%20are%20in%20percent%20(times%20100).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(palay)%3A%0A%20%20%20%20%23%20Try%20it%3A%20growth%20for%20palay%2C%20around%20the%20missing%20month%20and%20a%20year%20after%20it%0A%20%20%20%20def%20_try_growth()%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20attempt(growth%2C%20palay)%0A%20%20%20%20%20%20%20%20if%20table%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%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(table)%7D%20rows**%2C%20columns%20%7B'%2C%20'.join(map(str%2C%20table.columns))%7D.%20August%20to%20October%20in%202023%20and%202024%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20table.loc%5B%5Bp%20for%20p%20in%20table.index%20if%20str(p)%20in%20(%222023-08%22%2C%20%222023-09%22%2C%20%222023-10%22%2C%20%222024-08%22%2C%20%222024-09%22%2C%20%222024-10%22)%5D%5D.round(2)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_try_growth()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.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%20right%20on%20%22none%20left%20out%22%2C%20wrong%20on%20the%20others%20%7C%20%60pct_change%60%20ran%20on%20the%20series%20that%20skips%20a%20month%2C%20so%20it%20compared%20across%20the%20gap%20%7C%20%60reindex%60%20to%20every%20month%20first%20%7C%0A%20%20%20%20%7C%20changes%20such%20as%20%600.066%60%20instead%20of%20%606.6%60%20%7C%20fractions%2C%20not%20percent%20%7C%20%60*%20100%60%20%7C%0A%20%20%20%20%7C%20%60yoy_pct%60%20equals%20%60mom_pct%60%20%7C%20%60pct_change()%60%20used%20for%20both%20%7C%20%60pct_change(12)%60%20for%20the%20year%20%7C%0A%20%20%20%20%7C%20numbers%20where%20the%20gap%20should%20give%20NaN%20%7C%20the%20gap%20was%20filled%20(%60ffill%60%2C%20%60interpolate%60)%20before%20the%20changes%20%7C%20leave%20the%20missing%20month%20NaN%20%7C%0A%20%20%20%20%7C%20changes%20in%20pesos%2C%20not%20percent%20%7C%20%60diff()%60%20gives%20the%20difference%2C%20not%20the%20change%20in%20percent%20%7C%20%60pct_change%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**If%20you%20know%20pandas%202.**%20Two%20changes%20meet%20in%20this%20section.%20%60pct_change%60%20used%20to%20fill%20a%20missing%20value%20with%20the%20one%20before%20it%20(%60fill_method%3D%22pad%22%60%2C%20with%20a%20warning%20from%20pandas%202.1)%3A%20the%20missing%20month%20got%20a%20change%20of%200%2C%20and%20the%20next%20month's%20two-month%20change%20was%20labeled%20as%20one.%20In%20pandas%203%20it%20never%20fills%2C%20so%20a%20gap%20gives%20NaN.%20And%20quarters%20on%20a%20timestamp%20index%20are%20%60%22QE%22%60%20(quarter%20end)%20or%20%60%22QS%22%60%20(quarter%20start)%3A%20plain%20%60%22Q%22%60%20raises%20an%20error%20there%20now.%20On%20a%20period%20index%2C%20as%20here%2C%20%60%22Q%22%60%20is%20still%20the%20quarter.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205%20%C2%B7%20Duplicates%2C%20and%20checks%20you%20can%20rerun%0A%0A%20%20%20%20**The%20problem.**%20A%20household%20interviewed%20once%20should%20be%20one%20row.%20When%20the%20data%20are%20entered%20by%20hand%20or%20merged%20from%20several%20tablets%2C%20the%20same%20form%20can%20be%20entered%20twice.%20Each%20copy%20counts%20again%20in%20every%20total%2C%20and%20its%20weight%20doubles%20the%20farm%20households%20it%20stands%20for.%20%60table.duplicated()%60%20gives%20%60True%60%20for%20each%20row%20that%20is%20an%20exact%20copy%20of%20an%20earlier%20one%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households%2C%20raw_plots)%3A%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22rows%22%3A%20%5Blen(raw_households)%2C%20len(raw_plots)%5D%2C%0A%20%20%20%20%20%20%20%20%22exact%20copies%20of%20an%20earlier%20row%22%3A%20%5Bint(raw_households.duplicated().sum())%2C%20int(raw_plots.duplicated().sum())%5D%2C%0A%20%20%20%20%20%20%20%20%22copies%20of%20an%20earlier%20row's%20key%22%3A%20%5Bint(raw_households.duplicated(%22hh_id%22).sum())%2C%20int(raw_plots.duplicated(%5B%22hh_id%22%2C%20%22plot_no%22%5D).sum())%5D%2C%0A%20%20%20%20%7D%2C%20index%3D%5B%22households%20(key%3A%20hh_id)%22%2C%20%22plots%20(key%3A%20hh_id%20and%20plot_no)%22%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%60duplicated()%60%20marks%20the%20second%20copy%20and%20leaves%20the%20first%20unmarked%20(%60keep%3D%22first%22%60)%3B%20%60keep%3DFalse%60%20marks%20every%20copy%2C%20which%20is%20how%20to%20look%20at%20them%20side%20by%20side.%20%60duplicated(subset)%60%20compares%20only%20some%20columns%2C%20such%20as%20the%20**key**%3A%20the%20column%2C%20or%20columns%2C%20that%20should%20name%20each%20row%20once.%20A%20household's%20key%20is%20%60hh_id%60%3B%20a%20plot's%20is%20%60hh_id%60%20with%20%60plot_no%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20raw_households%5Braw_households.duplicated(keep%3DFalse)%5D.head(6)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Here%20the%20two%20counts%20agree%3A%20every%20repeated%20key%20is%20an%20exact%20copy%2C%20entered%20twice.%20Two%20cases%20need%20telling%20apart%2C%20because%20they%20need%20different%20actions%3A%0A%0A%20%20%20%20%7C%20what%20you%20find%20%7C%20what%20it%20is%20%7C%20what%20to%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20two%20rows%20identical%20in%20every%20column%20%7C%20the%20same%20form%20entered%20twice%20%7C%20drop%20the%20copy%3A%20%60table.drop_duplicates()%60%20%7C%0A%20%20%20%20%7C%20two%20rows%20with%20the%20same%20key%20and%20some%20values%20different%20%7C%20two%20answers%20for%20one%20household%3A%20a%20conflict%20%7C%20**don't**%20drop%20either%3A%20find%20out%20which%20is%20right%3B%20until%20then%2C%20report%20it%20%7C%0A%0A%20%20%20%20%60drop_duplicates()%60%20with%20no%20arguments%20drops%20only%20rows%20identical%20in%20every%20column%2C%20so%20it%20can%20never%20throw%20away%20a%20conflict.%20%60drop_duplicates(%22hh_id%22)%60%20would%20keep%20whichever%20row%20came%20first%20and%20silently%20discard%20the%20other%20answer.%0A%0A%20%20%20%20After%20dropping%20rows%2C%20%60reset_index(drop%3DTrue)%60%20numbers%20the%20rows%20again%20from%200%2C%20so%20the%20index%20has%20no%20holes%20where%20the%20copies%20were.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.9.%20The%20households%2C%20cleaned%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20cleaning%20the%20rest%20of%20the%20course%20relies%20on%2C%20in%20one%20function%20you%20can%20run%20again%20on%20next%20year's%20file.%20It%20brings%20together%20section%201%20and%20this%20section.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20raw%20households%20table%20%E2%86%92%20the%20table%20with%20rows%20entered%20twice%20dropped%20(only%20rows%20identical%20in%20every%20column)%2C%20the%20index%20numbered%20again%20from%200%2C%20and%20the%20not-stated%20codes%20made%20missing%20(your%20%60mark_not_stated%60%20from%20Exercise%205.1).%20A%20household%20with%20two%20different%20answers%20keeps%20both%20rows%3A%20that%20is%20for%20the%20checks.%0A%0A%20%20%20%20Tools%3A%20%60drop_duplicates()%60%20and%20%60reset_index(drop%3DTrue)%60%2C%20above%3B%20%60mark_not_stated%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20clean_households(raw%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20cleaned%3A%20rows%20entered%20twice%20dropped%2C%20rows%20numbered%20from%200%2C%20not-stated%20codes%20as%20%3CNA%3E.%22%22%22%0A%20%20%20%20%23%20Exercise%205.9%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20drop%20the%20rows%20that%20are%20identical%20to%20an%20earlier%20row%20in%20every%20column%0A%20%20%20%20%23%20Step%202%3A%20number%20the%20rows%20again%20from%200%20(reset_index%2C%20without%20keeping%20the%20old%20index)%0A%20%20%20%20%23%20Step%203%3A%20mark%20the%20not-stated%20codes%20as%20missing%20with%20your%20function%20from%20Exercise%205.1%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.9%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_households)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.9%22%2C%20clean_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(%22a%20household%20entered%20twice%2C%20and%20one%20with%20two%20different%20ages%22%2C%20(households_fixture(%22twice%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%22%2C%20(raw_households%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%3Dcourse.with_dtypes%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Drop%20only%20exact%20copies%20(drop_duplicates%20with%20no%20columns%20named)%2C%20reset_index(drop%3DTrue)%2C%20then%20mark_not_stated.%20The%20check%20compares%20values%2C%20row%20numbers%20and%20types.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20cleaning%20of%20the%20households%2C%20against%20the%20course's%20own%3A%20course.households()%0A%20%20%20%20def%20_try_clean()%3A%0A%20%20%20%20%20%20%20%20mine%20%3D%20attempt(clean_households%2C%20raw_households)%0A%20%20%20%20%20%20%20%20if%20mine%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20theirs%20%3D%20course.households()%0A%20%20%20%20%20%20%20%20if%20mine.equals(theirs)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22**Your%20cleaning%20matches%20the%20course's.**%20%7Blen(mine)%3A%2C%7D%20households%2C%20the%20same%20values%20and%20the%20same%20types%20as%20%60course.households()%60%2C%20which%20the%20later%20modules%20start%20from.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22Not%20the%20same%20as%20%60course.households()%60%20yet%3A%20yours%20has%20%7Blen(mine)%3A%2C%7D%20rows%2C%20the%20course's%20%7Blen(theirs)%3A%2C%7D.%20Types%3A%20yours%20%60%7Bdict(mine.dtypes.astype(str))%7D%60.%22)%2C%20kind%3D%22warn%22)%0A%0A%20%20%20%20_try_clean()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.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%201%2C196%20rows%20%7C%20the%20copies%20were%20not%20dropped%20%7C%20%60drop_duplicates()%60%20%7C%0A%20%20%20%20%7C%20the%20household%20with%20two%20ages%20lost%20a%20row%20%7C%20%60drop_duplicates(%22hh_id%22)%60%20keeps%20the%20first%20row%20of%20each%20key%20%7C%20%60drop_duplicates()%60%2C%20no%20columns%20named%20%7C%0A%20%20%20%20%7C%20the%20right%20rows%2C%20but%20the%20check%20is%20red%20%7C%20the%20old%20row%20numbers%20were%20kept%20(the%20index%20jumps%20where%20copies%20were)%20%7C%20%60reset_index(drop%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20a%20column%20named%20%60index%60%20%7C%20%60reset_index()%60%20without%20%60drop%3DTrue%60%20%7C%20%60drop%3DTrue%60%20%7C%0A%20%20%20%20%7C%20999%20and%209%20still%20there%2C%20or%20%60float64%60%20%7C%20%60mark_not_stated%60%20not%20applied%2C%20or%20Exercise%205.1%20not%20right%20yet%20%7C%20Exercise%205.1's%20check%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20plots%20are%20cleaned%20the%20same%20way%3A%20drop%20the%20rows%20entered%20twice%2C%20number%20them%20again%2C%20add%20the%20area%20in%20hectares%20(your%20Exercise%205.4).%20Nothing%20new%20to%20write%3B%20this%20function%20puts%20your%20pieces%20together%2C%20and%20the%20cell%20after%20it%20compares%20both%20tables%20with%20the%20course's%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20clean_plots(raw%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20cleaned%3A%20rows%20entered%20twice%20dropped%2C%20rows%20numbered%20from%200%2C%20every%20area%20also%20in%20hectares%20(area_ha).%22%22%22%0A%20%20%20%20return%20add_area_ha(raw.drop_duplicates().reset_index(drop%3DTrue))%0A%0A%0A%40app.cell%0Adef%20_(raw_households%2C%20raw_plots)%3A%0A%20%20%20%20_mine%20%3D%20%7B%22households%22%3A%20attempt(clean_households%2C%20raw_households)%2C%20%22plots%22%3A%20attempt(clean_plots%2C%20raw_plots)%7D%0A%20%20%20%20_theirs%20%3D%20%7B%22households%22%3A%20course.households()%2C%20%22plots%22%3A%20course.plots()%7D%0A%20%20%20%20mo.stop(any(t%20is%20NOT_YET%20for%20t%20in%20_mine.values())%2C%20waiting_for(%22Exercises%205.4%20and%205.9%22))%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22your%20rows%22%3A%20%5Blen(_mine%5Bk%5D)%20for%20k%20in%20_mine%5D%2C%0A%20%20%20%20%20%20%20%20%22course's%20rows%22%3A%20%5Blen(_theirs%5Bk%5D)%20for%20k%20in%20_mine%5D%2C%0A%20%20%20%20%20%20%20%20%22the%20same%20values%20and%20types%22%3A%20%5B%22yes%22%20if%20_mine%5Bk%5D.equals(_theirs%5Bk%5D)%20else%20%22**no**%22%20for%20k%20in%20_mine%5D%2C%0A%20%20%20%20%7D%2C%20index%3Dlist(_mine))%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%20Checks%20you%20can%20rerun%0A%0A%20%20%20%20Next%20year's%20file%20will%20have%20its%20own%20copies%2C%20codes%20and%20units.%20A%20**check**%20is%20code%20that%20says%20whether%20a%20table%20is%20as%20it%20should%20be%2C%20written%20once%20and%20run%20on%20every%20new%20file.%20The%20smallest%20check%20is%20%60assert%60%3A%20%60assert%20condition%2C%20%22message%22%60%20does%20nothing%20when%20the%20condition%20is%20%60True%60%2C%20and%20stops%20with%20an%20%60AssertionError%60%20carrying%20the%20message%20when%20it%20is%20%60False%60.%20%60series.is_unique%60%20is%20%60True%60%20when%20no%20value%20repeats%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households)%3A%0A%20%20%20%20%23%20In%20your%20own%20code%20you%20write%20the%20assert%20on%20its%20own%20line%3B%20here%20it%20is%20inside%20try%2C%20so%20the%20notebook%20keeps%20running%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20assert%20raw_households%5B%22hh_id%22%5D.is_unique%2C%20%22hh_id%20repeats%3A%20some%20households%20are%20in%20the%20table%20more%20than%20once%22%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.md(%22No%20error%3A%20every%20%60hh_id%60%20is%20unique.%22)%0A%20%20%20%20except%20AssertionError%20as%20error%3A%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.callout(mo.md(f%22**AssertionError**%3A%20%7Berror%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20_shown%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Read%20the%20last%20line%2C%20as%20with%20any%20error%3A%20the%20message%20you%20wrote%20says%20what%20is%20wrong.%20An%20%60assert%60%20stops%20at%20the%20first%20problem%2C%20though%2C%20and%20a%20file%20usually%20has%20several.%20A%20check%20that%20**collects**%20every%20problem%20first%20is%20more%20useful%3A%20a%20function%20that%20runs%20each%20rule%20and%20returns%20the%20list%20of%20rules%20that%20fail%2C%20empty%20when%20the%20tables%20are%20clean.%20You%20read%20the%20whole%20list%20once%2C%20fix%20what%20it%20names%20and%20run%20it%20again.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.10.%20A%20check%20for%20the%20survey%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20function%20is%20what%20you%20run%20first%20on%20the%20next%20survey%20file.%20It%20tells%20you%2C%20in%20one%20list%2C%20everything%20this%20module%20found%20by%20hand.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20households%20table%20and%20a%20plots%20table%20%E2%86%92%20the%20list%20of%20the%20names%20of%20the%20rules%20they%20break%2C%20in%20the%20order%20below%3B%20an%20empty%20list%20when%20they%20break%20none.%0A%0A%20%20%20%20%7C%20rule%20%7C%20it%20holds%20when%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60hh_id%20is%20unique%60%20%7C%20no%20%60hh_id%60%20appears%20twice%20in%20the%20households%20%7C%0A%20%20%20%20%7C%20%60each%20plot%20is%20listed%20once%60%20%7C%20no%20two%20plots%20share%20%60hh_id%60%20and%20%60plot_no%60%20%7C%0A%20%20%20%20%7C%20%60every%20plot%20has%20a%20household%60%20%7C%20every%20plot's%20%60hh_id%60%20is%20in%20the%20households%20%7C%0A%20%20%20%20%7C%20%60head_sex%20is%201%2C%202%20or%20missing%60%20%7C%20every%20%60head_sex%60%20is%201%2C%202%20or%20missing%20(so%20the%20code%209%20breaks%20it)%20%7C%0A%20%20%20%20%7C%20%60head_age%20is%2015%20to%20100%20or%20missing%60%20%7C%20every%20%60head_age%60%20is%20from%2015%20to%20100%2C%20or%20missing%20%7C%0A%20%20%20%20%7C%20%60every%20area%20is%20in%20hectares%60%20%7C%20the%20plots%20have%20an%20%60area_ha%60%20column%2C%20and%20every%20value%20in%20it%20is%20above%200%20%7C%0A%0A%20%20%20%20The%20skeleton%20has%20the%20names%20and%20two%20of%20the%20rules%20written%3B%20you%20write%20the%20other%20four.%20Each%20rule%20is%20one%20%60True%60%20or%20%60False%60%20for%20the%20whole%20table%3A%20a%20condition%20per%20row%2C%20then%20%60.all()%60%20(%22every%20row%22)%20or%20%60.any()%60%20(%22at%20least%20one%20row%22).%0A%0A%20%20%20%20Tools%3A%20%60is_unique%60%2C%20%60duplicated(subset)%60%2C%20above%3B%20%60isin%60%20(%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%2C%20section%203)%2C%20%60between%60%2C%20%60isna()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20survey_problems(households%3A%20pd.DataFrame%2C%20plots%3A%20pd.DataFrame)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20names%20of%20the%20rules%20the%20survey's%20tables%20break%2C%20in%20a%20fixed%20order%3B%20an%20empty%20list%20when%20they%20break%20none.%22%22%22%0A%20%20%20%20%23%20Exercise%205.10%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20The%20skeleton%20is%20commented%20out.%20Select%20all%20of%20it%20and%20press%20Ctrl%20%2B%20%2F%20(Mac%3A%20Cmd%20%2B%20%2F)%20to%20uncomment%20it%2C%0A%20%20%20%20%23%20then%20replace%20each%20...%20with%20the%20rule%20its%20name%20describes%3A%20one%20True%20or%20False%20for%20the%20whole%20table.%0A%20%20%20%20%23%20rules%20%3D%20%7B%0A%20%20%20%20%23%20%20%20%20%20%22hh_id%20is%20unique%22%3A%20households%5B%22hh_id%22%5D.is_unique%2C%0A%20%20%20%20%23%20%20%20%20%20%22each%20plot%20is%20listed%20once%22%3A%20...%2C%0A%20%20%20%20%23%20%20%20%20%20%22every%20plot%20has%20a%20household%22%3A%20...%2C%0A%20%20%20%20%23%20%20%20%20%20%22head_sex%20is%201%2C%202%20or%20missing%22%3A%20...%2C%0A%20%20%20%20%23%20%20%20%20%20%22head_age%20is%2015%20to%20100%20or%20missing%22%3A%20...%2C%0A%20%20%20%20%23%20%20%20%20%20%22every%20area%20is%20in%20hectares%22%3A%20%22area_ha%22%20in%20plots.columns%20and%20bool(plots%5B%22area_ha%22%5D.gt(0).all())%2C%0A%20%20%20%20%23%20%7D%0A%20%20%20%20%23%20return%20%5Bname%20for%20name%2C%20holds%20in%20rules.items()%20if%20not%20holds%5D%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.10%22)%0A%0A%0A%40app.function%0Adef%20survey_fixture(problem%3A%20str)%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Small%20households%20and%20plots%20tables%2C%20clean%20except%20for%20one%20problem%2C%20for%20the%20check%20of%20Exercise%205.10.%22%22%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-0001%22%2C%20%22HH-0002%22%2C%20%22HH-0003%22%5D%2C%0A%20%20%20%20%20%20%20%20%22head_sex%22%3A%20pd.array(%5B1%2C%202%2C%20pd.NA%5D%2C%20dtype%3D%22Int64%22)%2C%0A%20%20%20%20%20%20%20%20%22head_age%22%3A%20pd.array(%5B45%2C%20pd.NA%2C%20100%5D%2C%20dtype%3D%22Int64%22)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20plots%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0001%22%2C%20%22HH-0001%22%2C%20%22HH-0002%22%5D%2C%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%5D%2C%20%22area_ha%22%3A%20%5B0.8%2C%200.25%2C%201.2%5D%7D)%0A%20%20%20%20if%20problem%20%3D%3D%20%22a%20plot%20with%20no%20household%22%3A%0A%20%20%20%20%20%20%20%20plots%20%3D%20pd.concat(%5Bplots%2C%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0009%22%5D%2C%20%22plot_no%22%3A%20%5B1%5D%2C%20%22area_ha%22%3A%20%5B0.5%5D%7D)%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20elif%20problem%20%3D%3D%20%22a%20head%20aged%2014%22%3A%0A%20%20%20%20%20%20%20%20households%20%3D%20households.assign(head_age%3Dpd.array(%5B45%2C%2014%2C%20100%5D%2C%20dtype%3D%22Int64%22))%0A%20%20%20%20elif%20problem%20%3D%3D%20%22one%20household%20twice%2C%20with%20two%20answers%22%3A%0A%20%20%20%20%20%20%20%20households%20%3D%20pd.concat(%5Bhouseholds%2C%20households.iloc%5B%5B0%5D%5D.assign(head_age%3Dpd.array(%5B46%5D%2C%20dtype%3D%22Int64%22))%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20return%20households%2C%20plots%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(raw_households%2C%20raw_plots)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.10%22%2C%20survey_problems%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22small%20clean%20tables%2C%20with%20missing%20sex%20and%20age%22%2C%20survey_fixture(%22none%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20plot%20whose%20household%20is%20not%20in%20the%20table%22%2C%20survey_fixture(%22a%20plot%20with%20no%20household%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20head%20aged%2014%22%2C%20survey_fixture(%22a%20head%20aged%2014%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22one%20household%20twice%2C%20with%20two%20answers%22%2C%20survey_fixture(%22one%20household%20twice%2C%20with%20two%20answers%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%20as%20read%22%2C%20(raw_households%2C%20raw_plots))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%20as%20the%20course%20cleans%20it%22%2C%20(course.households()%2C%20course.plots()))%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%22Each%20rule%20is%20one%20True%20or%20False%20for%20the%20whole%20table%20(.all()%20or%20.any())%3B%20missing%20sex%20and%20age%20are%20allowed%20(%7C%20isna()).%20Use%20the%20Try%20it%20cell%20to%20run%20your%20check%20on%20the%20survey%20as%20read%20and%20as%20cleaned.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(raw_households%2C%20raw_plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20check%20on%20the%20survey%20as%20read%2C%20and%20on%20your%20cleaned%20tables%0A%20%20%20%20def%20_try_check()%3A%0A%20%20%20%20%20%20%20%20found%20%3D%20attempt(survey_problems%2C%20raw_households%2C%20raw_plots)%0A%20%20%20%20%20%20%20%20if%20found%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20function%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20lines%20%3D%20%5Bf%22**The%20survey%20as%20read**%20breaks%20%7Blen(found)%7D%20rule(s)%3A%20%22%20%2B%20(%22%3B%20%22.join(f%22%60%7Bname%7D%60%22%20for%20name%20in%20found)%20or%20%22none%22)%20%2B%20%22.%22%5D%0A%20%20%20%20%20%20%20%20cleaned%20%3D%20(attempt(clean_households%2C%20raw_households)%2C%20attempt(clean_plots%2C%20raw_plots))%0A%20%20%20%20%20%20%20%20if%20not%20any(table%20is%20NOT_YET%20for%20table%20in%20cleaned)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20after%20%3D%20survey_problems(*cleaned)%0A%20%20%20%20%20%20%20%20%20%20%20%20lines.append(f%22**Your%20cleaned%20tables**%20break%20%7Blen(after)%7D%3A%20%22%20%2B%20(%22%3B%20%22.join(f%22%60%7Bname%7D%60%22%20for%20name%20in%20after)%20or%20%22none%22)%20%2B%20%22.%22)%0A%20%20%20%20%20%20%20%20return%20mo.md(%22%5Cn%5Cn%22.join(lines))%0A%0A%20%20%20%20_try_check()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%205.10%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20cleaned%20survey%20breaks%20%60head_sex%20is%201%2C%202%20or%20missing%60%20%7C%20%60isin(%5B1%2C%202%5D)%60%20is%20%60False%60%20for%20a%20missing%20value%20%7C%20add%20%60%7C%20households%5B%22head_sex%22%5D.isna()%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous*%20%7C%20a%20rule%20is%20a%20column%20of%20%60True%60%2F%60False%60%2C%20not%20one%20value%20%7C%20end%20the%20rule%20with%20%60.all()%60%20or%20%60.any()%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'area_ha'*%20%7C%20the%20area%20rule%20reads%20the%20column%20before%20checking%20it%20exists%20%7C%20keep%20%60%22area_ha%22%20in%20plots.columns%20and%20...%60%20as%20given%20%7C%0A%20%20%20%20%7C%20the%20names%20of%20the%20rules%20that%20hold%2C%20not%20the%20ones%20that%20fail%20%7C%20%60if%20holds%60%20instead%20of%20%60if%20not%20holds%60%20%7C%20the%20last%20line%20of%20the%20skeleton%20%7C%0A%20%20%20%20%7C%20the%20plot%20rule%20fails%20on%20clean%20tables%20%7C%20%60duplicated(%22hh_id%22)%60%3A%20a%20household%20may%20have%20several%20plots%20%7C%20the%20key%20is%20%60%5B%22hh_id%22%2C%20%22plot_no%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%23%20What%20this%20module%20leaves%20behind%0A%0A%20%20%20%20Every%20fix%20in%20this%20module%20is%20a%20function%3A%20%60mark_not_stated%60%2C%20%60add_area_ha%60%2C%20%60parse_day_first%60%2C%20%60clean_households%60%2C%20%60clean_plots%60%2C%20%60monthly_series%60%2C%20%60growth%60%20and%20%60survey_problems%60.%20Run%20on%20the%20survey%20as%20read%2C%20they%20give%20the%20tables%20the%20rest%20of%20the%20course%20starts%20from%2C%20and%20%60survey_problems%60%20says%2C%20in%20one%20list%2C%20whether%20a%20new%20file%20needs%20the%20same%20care.%20The%20later%20modules%20start%20from%20%60course.households()%60%20and%20%60course.plots()%60%2C%20which%20give%20those%20cleaned%20tables%20directly.%0A%0A%20%20%20%20The%20cleaning%20left%20some%20missing%20values%20in%20place%20on%20purpose%3A%20the%20not-stated%20sex%20and%20age%2C%20the%20fertilizer%20and%20the%20crop%20not%20recorded%2C%20the%20month%20with%20no%20prices.%20Each%20is%20reported%2C%20never%20filled.%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(%225.5%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
4fe980442a0c0bac8a2689d5ff46975c