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%22Stretch%3A%20polars%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20every%20other%20cell%20uses.%0A%20%20%20%20import%20datetime%0A%20%20%20%20import%20os%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey's%20loaders%20and%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20PATHS%2C%20check_cases%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%20Stretch%3A%20polars%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20%5Bmodule%206%5D(%2F%3Ffile%3D50_polars.py)%20and%20want%20more.%20Each%20exercise%20goes%20further%20than%20the%20module's%2C%20with%20less%20help%3A%20a%20task%2C%20an%20example%2C%20a%20check%2C%20and%20the%20common%20causes%20when%20the%20check%20stays%20red.%20Do%20them%20in%20any%20order%3B%20none%20of%20them%20is%20needed%20later%20in%20the%20course.%0A%0A%20%20%20%20%7C%20exercise%20%7C%20what%20it%20adds%20to%20module%206%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20S2.1%20%C2%B7%20Every%20price%20file%2C%20in%20one%20lazy%20query%20%7C%20scanning%20many%20files%20of%20two%20layouts%2C%20a%20column%20from%20the%20file%20name%2C%20%60pl.concat%60%20%7C%0A%20%20%20%20%7C%20S2.2%20%C2%B7%20The%20months%20with%20no%20file%20%7C%20a%20calendar%20of%20months%20with%20%60pl.date_range%60%2C%20and%20an%20anti-join%20%7C%0A%20%20%20%20%7C%20S2.3%20%C2%B7%20Each%20crop's%20share%20of%20its%20province's%20farm%20area%20%7C%20a%20window%20expression%3A%20%60.over()%60%20%7C%0A%20%20%20%20%7C%20S2.4%20%C2%B7%20Each%20province's%20best%20rice%20yields%20%7C%20ranks%20within%20groups%2C%20and%20what%20a%20tie%20does%20%7C%0A%20%20%20%20%7C%20S2.5%20%C2%B7%20Codes%20that%20match%20nothing%20%7C%20an%20anti-join%20before%20a%20join%2C%20to%20see%20what%20it%20would%20lose%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20These%20exercises%20start%20from%20polars%20tables.%20This%20cell%20makes%20them%20from%20the%20course's%20loaders%2C%20so%20you%20can%20start%20here%20without%20the%20module's%20exercises%3A%20the%20clean%20plots%20and%20households%2C%20and%20the%20code%20list.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20plots%20%3D%20pl.from_pandas(course.plots())%0A%20%20%20%20households%20%3D%20pl.from_pandas(course.households())%0A%20%20%20%20code_list%20%3D%20pl.read_csv(PATHS.code_list%2C%20schema_overrides%3D%7B%22code%22%3A%20pl.String%2C%20%22province_code%22%3A%20pl.String%2C%20%22region_code%22%3A%20pl.String%7D)%0A%20%20%20%20return%20code_list%2C%20households%2C%20plots%0A%0A%0A%40app.function%0Adef%20in_order(table%2C%20by%3A%20list%5Bstr%5D)%3A%0A%20%20%20%20%22%22%22A%20polars%20result%20as%20the%20checks%20compare%20it%3A%20its%20rows%20sorted%20by%20%60by%60%2C%20so%20the%20order%20polars%20happened%20to%20give%20them%20in%0A%20%20%20%20does%20not%20count.%22%22%22%0A%20%20%20%20if%20not%20isinstance(table%2C%20pl.DataFrame)%3A%0A%20%20%20%20%20%20%20%20return%20f%22not%20a%20polars%20DataFrame%20but%20a%20%7Btype(table).__name__%7D%22%0A%20%20%20%20return%20table.sort(by%2C%20nulls_last%3DTrue)%0A%0A%0A%40app.function%0Adef%20show_polars(function%2C%20*args)%3A%0A%20%20%20%20%22%22%22A%20Try%20it%20cell's%20display%3A%20call%20the%20exercise%20function%20and%20show%20what%20it%20gives%20back%2C%20or%20what%20its%20error%20means.%22%22%22%0A%20%20%20%20meanings%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22ColumnNotFoundError%22%3A%20%22a%20column%20name%20that%20is%20not%20in%20the%20table.%20polars%20lists%20the%20valid%20names%2C%20and%20often%20suggests%20the%20one%20you%20meant.%22%2C%0A%20%20%20%20%20%20%20%20%22InvalidOperationError%22%3A%20%22an%20operation%20the%20column's%20type%20does%20not%20allow%3A%20%60.str%60%20on%20numbers%2C%20a%20sum%20of%20text%2C%20or%20a%20cast%20that%20fails%20for%20some%20values.%22%2C%0A%20%20%20%20%20%20%20%20%22ComputeError%22%3A%20%22polars%20could%20not%20compute%20the%20result.%20With%20%60validate%3D%60%2C%20a%20join%20key%20that%20should%20be%20unique%20is%20not.%22%2C%0A%20%20%20%20%20%20%20%20%22SchemaError%22%3A%20%22the%20files%20or%20tables%20do%20not%20have%20the%20columns%20or%20types%20the%20query%20expects%3A%20two%20file%20layouts%20in%20one%20scan%2C%20or%20join%20keys%20of%20different%20types.%22%2C%0A%20%20%20%20%20%20%20%20%22ShapeError%22%3A%20%22columns%20of%20different%20lengths%20were%20put%20side%20by%20side.%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20function(*args)%0A%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60%7Bfunction.__name__%7D%60%3A%20**not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%20NotImplementedError%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20except%20Exception%20as%20error%3A%0A%20%20%20%20%20%20%20%20name%2C%20message%20%3D%20type(error).__name__%2C%20str(error).strip()%0A%20%20%20%20%20%20%20%20if%20%22truth%20value%20of%20an%20Expr%20is%20ambiguous%22%20in%20message%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20meaning%20%3D%20%22%60and%60%2C%20%60or%60%20or%20%60not%60%20between%20expressions%2C%20or%20a%20comparison%20next%20to%20%60%26%60%20without%20brackets%3A%20use%20%60%26%60%2C%20%60%7C%60%2C%20%60~%60%2C%20with%20brackets%20around%20each%20comparison.%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20meaning%20%3D%20meanings.get(name)%20or%20course.ERROR_MEANINGS.get(name%2C%20%22read%20the%20message%3A%20it%20usually%20names%20the%20value%20or%20line%20that%20went%20wrong.%22)%0A%20%20%20%20%20%20%20%20short%20%3D%20message%20if%20len(message)%20%3C%3D%20600%20else%20message%5B%3A599%5D%20%2B%20%22%E2%80%A6%22%0A%20%20%20%20%20%20%20%20short%20%3D%20short.replace(PATHS.data.as_posix()%20%2B%20%22%2F%22%2C%20%22%22).replace(str(PATHS.data)%20%2B%20os.sep%2C%20%22%22)%0A%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60%7Bfunction.__name__%7D%60%20raised%20**%7Bname%7D**%3A%5Cn%5Cn%60%60%60text%5Cn%7Bshort%7D%5Cn%60%60%60%5Cn%5Cn**What%20it%20usually%20means%3A**%20%7Bmeaning%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20if%20isinstance(result%2C%20pl.LazyFrame)%3A%0A%20%20%20%20%20%20%20%20collected%20%3D%20result.collect()%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20**a%20LazyFrame**%3A%20a%20query%2C%20not%20run%20yet.%20Run%20with%20%60.collect()%60%2C%20it%20gives%20%7Bcollected.height%3A%2C%7D%20rows%3B%20the%20first%20ones%3A%22)%2C%20collected.head(8)%5D)%0A%20%20%20%20if%20isinstance(result%2C%20pl.DataFrame)%3A%0A%20%20%20%20%20%20%20%20types%20%3D%20%22%2C%20%22.join(f%22%60%7Bname%7D%60%20%7Bdtype%7D%22%20for%20name%2C%20dtype%20in%20result.schema.items())%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20**a%20polars%20DataFrame%20of%20%7Bresult.height%3A%2C%7D%20rows**.%20Its%20columns%20and%20types%3A%20%7Btypes%7D.%20The%20first%20rows%3A%22)%2C%20result.head(8)%5D)%0A%20%20%20%20return%20mo.md(f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20%60%7Bresult!r%3A.200%7D%60%20(type%20%60%7Btype(result).__name__%7D%60).%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S2.1%20%C2%B7%20Every%20price%20file%2C%20in%20one%20lazy%20query%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%60course.prices()%60%20has%20read%20the%20farmgate%20prices%20for%20you%20all%20along.%20Here%20you%20write%20that%20reader%20yourself%2C%20lazily%2C%20so%20it%20would%20cope%20with%20ten%20years%20of%20files%20as%20easily%20as%20four.%20It%20is%20the%20core%20of%20the%20capstone's%20third%20problem.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20prices%20folder%20and%20the%20code%20list%20(polars)%20%E2%86%92%20a%20**LazyFrame**%20that%20gives%20every%20price%20in%20one%20table%20of%20four%20columns%2C%20sorted%20by%20%60month%60%2C%20%60province%60%2C%20%60commodity%60%3A%0A%0A%20%20%20%20%7C%20month%20%7C%20province%20%7C%20commodity%20%7C%20price%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%202022-01-01%20%7C%20Bukidnon%20%7C%20Eggplant%20%7C%2029.0%20%7C%0A%0A%20%20%20%20%60month%60%20is%20a%20%60Date%60%2C%20the%20first%20day%20of%20the%20month.%20Two%20things%20make%20it%20more%20than%20one%20%60scan_csv%60%3A%0A%0A%20%20%20%20-%20**two%20layouts.**%20Files%20up%20to%20%60farmgate_2024-12.csv%60%20have%20%60province%60%2C%20%60commodity%60%2C%20%60price_php_per_kg%60%2C%20and%20no%20month%3A%20the%20month%20is%20only%20in%20the%20file's%20name.%20From%20%60farmgate_2025-01.csv%60%20on%2C%20the%20files%20have%20%60Month%60%2C%20%60Commodity%60%2C%20%60Province%20Code%60%20(10-digit%20text%2C%20to%20be%20turned%20into%20the%20province's%20name%20with%20the%20code%20list)%20and%20%60Price%20(PHP%2Fkg)%60.%20One%20glob%20across%20both%20fails%20with%20a%20*SchemaError*%2C%20as%20module%206%20showed.%0A%20%20%20%20-%20**the%20code%20list%20has%20one%20row%20per%20municipality%2C**%20so%20each%20province%20code%20is%20in%20it%20many%20times.%20Joining%20it%20as%20it%20is%20would%20copy%20every%20price%20once%20per%20municipality.%0A%0A%20%20%20%20**Tools%3A**%20list%20the%20files%20with%20%60sorted(folder.glob(%22farmgate_*.csv%22))%60%20and%20split%20them%20by%20name%20(%60path.stem%20%3C%20%22farmgate_2025-01%22%60)%3B%20%60pl.scan_csv%60%20takes%20a%20list%20of%20paths.%20%60include_file_paths%3D%22path%22%60%20adds%20each%20row's%20file%20as%20a%20column%3B%20%60pl.col(%22path%22).str.extract(r%22farmgate_(%5Cd%7B4%7D-%5Cd%7B2%7D)%22%2C%201)%60%20takes%20the%20month%20out%20of%20it%2C%20and%20%60.str.to_date(%22%25Y-%25m%22)%60%20makes%20it%20a%20date.%20%60select(new%3Dpl.col(%22old%22))%60%20renames%20as%20it%20selects.%20%60pl.concat(%5Blazy_a%2C%20lazy_b%5D)%60%20stacks%20two%20queries%20with%20the%20same%20columns.%20The%20result%20has%20the%20same%201%2C316%20rows%20as%20%60course.prices()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20all_prices(folder%3A%20Path%2C%20code_list%3A%20pl.DataFrame)%20-%3E%20pl.LazyFrame%3A%0A%20%20%20%20%22%22%22Every%20monthly%20price%20file%2C%20both%20layouts%2C%20as%20one%20lazy%20query%3A%20month%20(a%20date)%2C%20province%2C%20commodity%2C%20price.%22%22%22%0A%20%20%20%20%23%20Step%201%3A%20list%20the%20files%2C%20and%20split%20them%20into%20the%20older%20layout%20and%20the%20newer%0A%20%20%20%20%23%20Step%202%3A%20the%20older%20files%3A%20scan%20with%20the%20file%20path%2C%20month%20from%20the%20path%2C%20the%20three%20columns%20renamed%0A%20%20%20%20%23%20Step%203%3A%20the%20newer%20files%3A%20scan%20with%20the%20code%20as%20text%2C%20join%20the%20province%20names%20(each%20code%20once)%2C%20month%20as%20a%20date%0A%20%20%20%20%23%20Step%204%3A%20concat%20the%20two%20queries%20and%20sort%3B%20return%20the%20LazyFrame%2C%20without%20.collect()%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S2.1%22)%0A%0A%0A%40app.function%0Adef%20prices_folder_fixture()%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22A%20small%20prices%20folder%2C%20written%20afresh%20to%20the%20same%20temporary%20folder%20at%20each%20run%2C%20for%20the%20check%20of%20S2.1%3A%20two%20months%0A%20%20%20%20of%20the%20older%20layout%20and%20two%20of%20the%20newer%2C%20around%20the%20change.%22%22%22%0A%20%20%20%20folder%20%3D%20Path(tempfile.gettempdir())%20%2F%20%22adf-stretch-polars-prices%22%0A%20%20%20%20folder.mkdir(exist_ok%3DTrue)%0A%20%20%20%20for%20old%20in%20folder.glob(%22farmgate_*.csv%22)%3A%20%20%23%20only%20the%20files%20below%2C%20whatever%20an%20earlier%20run%20left%0A%20%20%20%20%20%20%20%20old.unlink()%0A%20%20%20%20files%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22farmgate_2024-11.csv%22%3A%20%22province%2Ccommodity%2Cprice_php_per_kg%5CnIloilo%2CPalay%2C20.5%5CnIsabela%2CPalay%2C19.0%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%22farmgate_2024-12.csv%22%3A%20%22province%2Ccommodity%2Cprice_php_per_kg%5CnIloilo%2CPalay%2C21.0%5CnIsabela%2CTomato%2C30.25%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%22farmgate_2025-01.csv%22%3A%20%22Month%2CCommodity%2CProvince%20Code%2CPrice%20(PHP%2Fkg)%5Cn2025-01%2CPalay%2C0603000000%2C21.4%5Cn2025-01%2CPalay%2C0102800000%2C22.5%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%22farmgate_2025-02.csv%22%3A%20%22Month%2CCommodity%2CProvince%20Code%2CPrice%20(PHP%2Fkg)%5Cn2025-02%2CTomato%2C0203100000%2C28.75%5Cn%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20for%20name%2C%20text%20in%20files.items()%3A%0A%20%20%20%20%20%20%20%20(folder%20%2F%20name).write_text(text%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20return%20folder%0A%0A%0A%40app.function%0Adef%20scanned_result(query)%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20S2.1%20compares%3A%20whether%20the%20answer%20is%20a%20query%20that%20scans%20the%20files%2C%20and%20what%20it%20gives.%22%22%22%0A%20%20%20%20if%20not%20isinstance(query%2C%20pl.LazyFrame)%3A%0A%20%20%20%20%20%20%20%20return%20f%22not%20a%20LazyFrame%20but%20a%20%7Btype(query).__name__%7D%3A%20start%20from%20pl.scan_csv%2C%20and%20leave%20out%20.collect()%22%0A%20%20%20%20return%20%7B%22scans%20the%20files%22%3A%20%22Csv%20SCAN%22%20in%20query.explain()%2C%20%22result%22%3A%20in_order(query.collect()%2C%20%5B%22month%22%2C%20%22province%22%2C%20%22commodity%22%5D)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.1%22%2C%20all_prices%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%20files%20around%20the%20change%20of%20layout%20(Nov%202024%20to%20Feb%202025)%22%2C%20(prices_folder_fixture()%2C%20code_list))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20course's%2047%20price%20files%22%2C%20(PATHS.prices%2C%20code_list))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22folders%22%2C%20summarize%3Dscanned_result%2C%0A%20%20%20%20%20%20%20%20hint%3D%22A%20LazyFrame%20that%20scans%20the%20files%3B%20month%20a%20Date%20from%20the%20file%20name%20(older%20layout)%20or%20the%20Month%20column%20(newer)%3B%20province%20names%20for%20the%20codes%2C%20one%20row%20per%20province%20code%3B%201%2C316%20rows%20for%20the%20course's%20files.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(code_list)%3A%0A%20%20%20%20%23%20Try%20it%3A%20every%20price%20file%20of%20the%20course%2C%20in%20one%20query.%0A%20%20%20%20show_polars(all_prices%2C%20PATHS.prices%2C%20code_list)%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%20Stretch%20S2.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*SchemaError%3A%20extra%20column%20in%20file%20outside%20of%20expected%20schema%3A%20Month*%20%7C%20One%20scan%20across%20both%20layouts.%20%7C%20split%20the%20files%20by%20name%2C%20one%20scan%20per%20layout%20%7C%0A%20%20%20%20%7C%20*SchemaError%3A%20datatypes%20of%20join%20keys%20don't%20match*%20%7C%20%60Province%20Code%60%20read%20as%20a%20number.%20%7C%20%60schema_overrides%3D%7B%22Province%20Code%22%3A%20pl.String%7D%60%20%7C%0A%20%20%20%20%7C%20*ComputeError%3A%20join%20keys%20did%20not%20fulfill%20m%3A1%20validation*%2C%20or%20far%20too%20many%20rows%20%7C%20The%20code%20list%20joined%20as%20it%20is%3A%20a%20row%20per%20municipality.%20%7C%20%60.select(%22province_code%22%2C%20%22province%22).unique()%60%20first%20%7C%0A%20%20%20%20%7C%20*SchemaError*%20or%20*ShapeError*%20at%20%60pl.concat%60%20%7C%20The%20two%20queries'%20columns%20differ%20in%20name%2C%20order%20or%20type.%20%7C%20the%20same%20%60select(month%3D...%2C%20province%3D...%2C%20commodity%3D...%2C%20price%3D...)%60%20in%20both%20%7C%0A%20%20%20%20%7C%20%60month%60%20is%20%60String%60%20%7C%20The%20month%20text%20was%20not%20made%20a%20date.%20%7C%20%60.str.to_date(%22%25Y-%25m%22)%60%20%7C%0A%20%20%20%20%7C%20%60scans%20the%20files%60%20is%20%60False%60%2C%20or%20not%20a%20LazyFrame%20%7C%20%60pl.read_csv%60%2C%20or%20%60.collect()%60%20at%20the%20end.%20%7C%20%60pl.scan_csv%60%2C%20and%20return%20the%20query%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%20S2.2%20%C2%B7%20The%20months%20with%20no%20file%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20month%20whose%20file%20never%20came%20has%20no%20rows%20at%20all%2C%20so%20nothing%20in%20the%20table%20says%20it%20is%20missing.%20A%20chart%20quietly%20draws%20a%20straight%20line%20across%20the%20gap%2C%20and%20a%20growth%20rate%20from%20the%20month%20before%20compares%20two%20months%20apart.%20You%20find%20a%20missing%20month%20only%20by%20comparing%20the%20table%20with%20the%20calendar.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20prices%20table%20in%20polars%20(with%20%60month%60%20as%20a%20%60Date%60%2C%20the%20first%20day%20of%20each%20month)%20%E2%86%92%20a%20polars%20DataFrame%20with%20one%20column%2C%20%60month%60%3A%20every%20month%20between%20the%20first%20and%20the%20last%20month%20of%20the%20table%20that%20has%20**no%20row**%2C%20in%20order.%20No%20missing%20month%20gives%20a%20table%20with%20no%20rows.%0A%0A%20%20%20%20For%20the%20course's%20prices%2C%20the%20answer%20is%20one%20month%3A%0A%0A%20%20%20%20%7C%20month%20%7C%0A%20%20%20%20%7C%3A--%7C%0A%20%20%20%20%7C%202023-09-01%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60pl.date_range(start%2C%20end%2C%20interval%3D%221mo%22%2C%20eager%3DTrue)%60%20makes%20the%20calendar%20(%60start%60%20and%20%60end%60%20from%20%60prices%5B%22month%22%5D.min()%60%20and%20%60.max()%60)%3B%20%60.alias(%22month%22).to_frame()%60%20makes%20it%20a%20table%3B%20%60join(...%2C%20on%3D%22month%22%2C%20how%3D%22anti%22)%60%20keeps%20the%20rows%20of%20the%20left%20table%20that%20have%20**no**%20match%20in%20the%20right%20one.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20missing_months(prices%3A%20pl.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20months%20between%20the%20first%20and%20the%20last%20of%20the%20prices%20that%20have%20no%20row.%22%22%22%0A%20%20%20%20%23%20Step%201%3A%20the%20calendar%3A%20every%20month%20from%20the%20first%20month%20to%20the%20last%2C%20as%20a%20table%20with%20a%20month%20column%0A%20%20%20%20%23%20Step%202%3A%20keep%20the%20calendar's%20months%20that%20are%20not%20in%20the%20prices%20(an%20anti-join)%2C%20sorted%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S2.2%22)%0A%0A%0A%40app.function%0Adef%20prices_fixture(gaps%3A%20bool)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22A%20few%20prices%20from%20November%202023%20to%20April%202024%2C%20for%20the%20check%20of%20S2.2%3A%20with%20two%20months%20missing%20(December%20and%0A%20%20%20%20February%2C%20across%20the%20year's%20end)%2C%20or%20with%20none.%22%22%22%0A%20%20%20%20months%20%3D%20%5B%222023-11%22%2C%20%222023-11%22%2C%20%222024-01%22%2C%20%222024-03%22%2C%20%222024-04%22%5D%20if%20gaps%20else%20%5B%222023-11%22%2C%20%222023-12%22%2C%20%222024-01%22%2C%20%222024-02%22%5D%0A%20%20%20%20return%20pl.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22month%22%3A%20%5Bdatetime.date(int(m%5B%3A4%5D)%2C%20int(m%5B5%3A%5D)%2C%201)%20for%20m%20in%20months%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Iloilo%22%5D%20*%20len(months)%2C%0A%20%20%20%20%20%20%20%20%22commodity%22%3A%20%5B%22Palay%22%5D%20*%20len(months)%2C%0A%20%20%20%20%20%20%20%20%22price%22%3A%20%5B20.0%20%2B%20i%20for%20i%20in%20range(len(months))%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20course_prices_polars()%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22course.prices()%20in%20polars%2C%20with%20month%20as%20the%20date%20of%20its%20first%20day%20(module%206%2C%20Exercise%206.6).%22%22%22%0A%20%20%20%20prices%20%3D%20course.prices()%0A%20%20%20%20return%20pl.from_pandas(prices.assign(month%3Dprices%5B%22month%22%5D.dt.to_timestamp())).with_columns(pl.col(%22month%22).cast(pl.Date))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.2%22%2C%20missing_months%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22prices%20from%20Nov%202023%20to%20Apr%202024%2C%20December%20and%20February%20missing%22%2C%20(prices_fixture(gaps%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22prices%20from%20Nov%202023%20to%20Feb%202024%2C%20nothing%20missing%22%2C%20(prices_fixture(gaps%3DFalse)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20price%20of%20the%20course%22%2C%20(course_prices_polars()%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%3Dlambda%20table%3A%20in_order(table%2C%20%5B%22month%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22A%20table%20with%20one%20column%2C%20month%2C%20of%20Date%20type%3B%20empty%20(but%20with%20its%20column)%20when%20nothing%20is%20missing.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20months%20of%20the%20course's%20prices%20that%20have%20no%20file.%0A%20%20%20%20show_polars(missing_months%2C%20course_prices_polars())%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%20Stretch%20S2.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20months%20that%20**are**%20there%2C%20not%20those%20missing%20%7C%20A%20semi%20or%20inner%20join%20instead%20of%20an%20anti-join%2C%20or%20the%20tables%20the%20wrong%20way%20round.%20%7C%20%60calendar.join(prices%2C%20on%3D%22month%22%2C%20how%3D%22anti%22)%60%20%7C%0A%20%20%20%20%7C%20*ColumnNotFoundError*%20for%20%60month%60%20in%20the%20calendar%20%7C%20The%20calendar's%20column%20has%20another%20name%20(%60date%60%2C%20or%20none).%20%7C%20%60.alias(%22month%22)%60%20%7C%0A%20%20%20%20%7C%20*TypeError*%20or%20*InvalidOperationError*%20in%20%60date_range%60%20%7C%20%60interval%60%20written%20another%20way%2C%20or%20the%20start%20and%20end%20are%20not%20dates.%20%7C%20%60interval%3D%221mo%22%60%2C%20and%20%60prices%5B%22month%22%5D.min()%60%20%7C%0A%20%20%20%20%7C%20columns%20other%20than%20%60month%60%20%7C%20The%20prices'%20columns%20came%20along.%20%7C%20an%20anti-join%20keeps%20only%20the%20left%20table's%20columns%3A%20the%20calendar%20on%20the%20left%20%7C%0A%20%20%20%20%7C%20%60month%60%20of%20type%20%60Datetime%60%20%7C%20The%20calendar%20was%20made%20with%20%60pl.datetime_range%60.%20%7C%20%60pl.date_range%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%20S2.3%20%C2%B7%20Each%20crop's%20share%20of%20its%20province's%20farm%20area%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22rice%20is%2063%25%20of%20the%20farmed%20area%20of%20Camarines%20Sur%22%20is%20a%20share%20within%20a%20group%3A%20each%20crop's%20area%20over%20its%20own%20province's%20total.%20In%20%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%20you%20wrote%20it%20with%20%60groupby(...).transform(%22sum%22)%60.%20polars%20writes%20it%20as%20a%20**window%20expression**%3A%20%60pl.col(%22x%22).sum().over(%22province%22)%60%20is%20the%20sum%20of%20%60x%60%20over%20the%20rows%20of%20the%20same%20province%2C%20set%20beside%20every%20one%20of%20them%2C%20without%20collapsing%20the%20table.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20clean%20plots%20and%20households%20(polars)%20%E2%86%92%20one%20row%20per%20province%20and%20crop%2C%20sorted%20by%20%60province%60%20then%20%60crop%60%2C%20with%20four%20columns%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20crop%20%7C%20area_ha%20%7C%20share%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%20Banana%20%7C%2055%2C783.9%E2%80%A6%20%7C%200.264%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%20Corn%20%7C%20102%2C079.3%E2%80%A6%20%7C%200.483%E2%80%A6%20%7C%0A%0A%20%20%20%20%60area_ha%60%20is%20the%20**weighted**%20area%3A%20the%20sum%20of%20each%20plot's%20%60area_ha%60%20times%20its%20household's%20%60weight%60%2C%20the%20hectares%20the%20province's%20farms%20would%20show.%20%60share%60%20is%20that%20area%20over%20the%20province's%20total%20over%20all%20its%20crops%2C%20so%20a%20province's%20shares%20add%20up%20to%201.%20Plots%20with%20no%20crop%20recorded%20are%20left%20out.%0A%0A%20%20%20%20**Tools%3A**%20a%20join%20for%20%60province%60%20and%20%60weight%60%20(%60validate%3D%22m%3A1%22%60)%2C%20%60drop_nulls(%22crop%22)%60%20or%20a%20%60filter%60%2C%20%60group_by(%22province%22%2C%20%22crop%22).agg(...)%60%2C%20then%20%60with_columns(share%3Dpl.col(%22area_ha%22)%20%2F%20pl.col(%22area_ha%22).sum().over(%22province%22))%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20crop_shares(plots%3A%20pl.DataFrame%2C%20households%3A%20pl.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20crop's%20weighted%20area%2C%20and%20its%20share%20of%20its%20province's%20weighted%20farm%20area.%22%22%22%0A%20%20%20%20%23%20Step%201%3A%20join%20each%20plot's%20province%20and%20weight%3B%20leave%20out%20the%20plots%20with%20no%20crop%0A%20%20%20%20%23%20Step%202%3A%20the%20weighted%20area%20of%20each%20province%20and%20crop%0A%20%20%20%20%23%20Step%203%3A%20each%20crop's%20share%20of%20its%20province's%20total%2C%20with%20.over(%22province%22)%3B%20sort%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S2.3%22)%0A%0A%0A%40app.function%0Adef%20small_survey()%20-%3E%20tuple%5Bpl.DataFrame%2C%20pl.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Seven%20plots%20of%20four%20households%20in%20two%20provinces%2C%20with%20very%20different%20weights%20and%20a%20plot%20with%20no%20crop%2C%20for%20the%0A%20%20%20%20checks%20of%20S2.3%20and%20S2.4.%22%22%22%0A%20%20%20%20plots%20%3D%20pl.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22H1%22%2C%20%22H1%22%2C%20%22H2%22%2C%20%22H2%22%2C%20%22H3%22%2C%20%22H3%22%2C%20%22H4%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%202%2C%201%2C%202%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Corn%22%2C%20%22Rice%22%2C%20None%2C%20%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%5D%2C%0A%20%20%20%20%20%20%20%20%22area_ha%22%3A%20%5B1.0%2C%200.5%2C%202.0%2C%200.3%2C%200.5%2C%201.0%2C%200.8%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B4000%2C%202000%2C%207000%2C%20900%2C%202500%2C%203500%2C%204000%5D%2C%0A%20%20%20%20%20%20%20%20%22irrigated%22%3A%20%5B%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22No%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22Yes%22%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20households%20%3D%20pl.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22H1%22%2C%20%22H2%22%2C%20%22H3%22%2C%20%22H4%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Iloilo%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%2C%20%22Isabela%22%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B100.0%2C%2050.0%2C%20200.0%2C%2010.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20plots%2C%20households%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_plots%2C%20_households%20%3D%20small_survey()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.3%22%2C%20crop_shares%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22seven%20plots%20in%20two%20provinces%2C%20one%20with%20no%20crop%22%2C%20(_plots%2C%20_households))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20clean%20plots%20and%20households%22%2C%20(pl.from_pandas(course.plots())%2C%20pl.from_pandas(course.households())))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20in_order(table%2C%20%5B%22province%22%2C%20%22crop%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20tables%2C%20Iloilo%20has%20200%20weighted%20hectares%20of%20rice%20(1.0%20%C3%97%20100%20%2B%202.0%20%C3%97%2050)%20and%2050%20of%20corn%3A%20shares%200.8%20and%200.2.%20The%20plot%20with%20no%20crop%20is%20left%20out.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(households%2C%20plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20each%20crop's%20share%20of%20each%20province's%20farm%20area.%0A%20%20%20%20show_polars(crop_shares%2C%20plots%2C%20households)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Stretch%20S2.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%20a%20row%20whose%20%60crop%60%20is%20%60null%60%20%7C%20The%20plots%20with%20no%20crop%20are%20still%20there.%20%7C%20%60drop_nulls(%22crop%22)%60%20before%20grouping%20%7C%0A%20%20%20%20%7C%20every%20%60share%60%20is%201.0%20%7C%20%60.over(%22province%22%2C%20%22crop%22)%60%2C%20or%20%60over%60%20on%20the%20wrong%20column%3A%20each%20row%20divided%20by%20itself.%20%7C%20%60.over(%22province%22)%60%20%7C%0A%20%20%20%20%7C%20shares%20add%20up%20to%201%20over%20the%20whole%20survey%2C%20not%20per%20province%20%7C%20%60.sum()%60%20without%20%60.over(...)%60.%20%7C%20%60pl.col(%22area_ha%22).sum().over(%22province%22)%60%20%7C%0A%20%20%20%20%7C%20%60area_ha%60%20equals%20the%20plain%20sum%20of%20areas%20%7C%20The%20weight%20is%20not%20in%20the%20sum.%20%7C%20%60(pl.col(%22area_ha%22)%20*%20pl.col(%22weight%22)).sum()%60%20%7C%0A%20%20%20%20%7C%20the%20table%20has%20one%20row%20per%20plot%20%7C%20%60with_columns%60%20without%20%60group_by%60%20first.%20%7C%20group%20by%20province%20and%20crop%2C%20then%20the%20share%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%20S2.4%20%C2%B7%20Each%20province's%20best%20rice%20yields%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20extension%20office%20wants%20to%20visit%20the%20farms%20with%20the%20best%20rice%20yields%20in%20each%20province%2C%20to%20learn%20what%20they%20do.%20That%20is%20a%20rank%20**within**%20a%20group%2C%20and%20ranks%20raise%20a%20question%20a%20plain%20sort%20does%20not%3A%20what%20happens%20when%20two%20plots%20tie.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20clean%20plots%20and%20households%20(polars)%2C%20and%20a%20number%20%60n%60%20%E2%86%92%20the%20rice%20plots%20whose%20yield%20ranks%20among%20the%20top%20%60n%60%20of%20their%20province%2C%20with%20five%20columns%2C%20sorted%20by%20%60province%60%2C%20%60rank%60%2C%20%60hh_id%60%2C%20%60plot_no%60%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20hh_id%20%7C%20plot_no%20%7C%20yield_t_ha%20%7C%20rank%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%20HH-0881%20%7C%202%20%7C%206.628%E2%80%A6%20%7C%201%20%7C%0A%0A%20%20%20%20%60yield_t_ha%60%20is%20%60production_kg%20%2F%20area_ha%20%2F%201000%60%2C%20as%20in%20module%206.%20%60rank%60%20is%201%20for%20the%20province's%20highest%20yield.%20**Plots%20that%20tie%20share%20a%20rank%2C%20and%20the%20next%20rank%20is%20skipped**%3A%20yields%207.0%2C%205.0%2C%205.0%2C%204.0%20rank%201%2C%202%2C%202%2C%204.%20So%20with%20%60n%20%3D%202%60%20both%20plots%20at%205.0%20are%20kept%2C%20and%20a%20province%20can%20give%20more%20than%20%60n%60%20plots.%0A%0A%20%20%20%20**Tools%3A**%20%60pl.col(%22yield_t_ha%22).rank(%22min%22%2C%20descending%3DTrue).over(%22province%22)%60%3B%20%60%22min%22%60%20gives%20tied%20values%20the%20lowest%20rank%20of%20the%20tie%2C%20which%20is%20the%20rule%20above.%20Then%20%60filter(pl.col(%22rank%22)%20%3C%3D%20n)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20top_rice_plots(plots%3A%20pl.DataFrame%2C%20households%3A%20pl.DataFrame%2C%20n%3A%20int)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20rice%20plots%20whose%20yield%20ranks%20among%20the%20top%20n%20of%20their%20province%20(ties%20share%20a%20rank).%22%22%22%0A%20%20%20%20%23%20Step%201%3A%20the%20rice%20plots%2C%20with%20each%20plot's%20province%20and%20its%20yield%20in%20t%2Fha%0A%20%20%20%20%23%20Step%202%3A%20rank%20the%20yields%20within%20each%20province%2C%20highest%20first%2C%20ties%20sharing%20the%20lowest%20rank%0A%20%20%20%20%23%20Step%203%3A%20keep%20ranks%20up%20to%20n%3B%20the%20five%20columns%3B%20sort%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S2.4%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_plots%2C%20_households%20%3D%20small_survey()%0A%20%20%20%20_survey%20%3D%20(pl.from_pandas(course.plots())%2C%20pl.from_pandas(course.households()))%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.4%22%2C%20top_rice_plots%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22seven%20plots%2C%20the%20top%201%20of%20each%20province%22%2C%20(_plots%2C%20_households%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22seven%20plots%2C%20the%20top%202%3A%20two%20Isabela%20plots%20tie%20at%205.0%20t%2Fha%22%2C%20(_plots%2C%20_households%2C%202))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%2C%20the%20top%203%20of%20each%20province%22%2C%20(*_survey%2C%203))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22cases%22%2C%20summarize%3Dlambda%20table%3A%20in_order(table%2C%20%5B%22province%22%2C%20%22rank%22%2C%20%22hh_id%22%2C%20%22plot_no%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20tables%2C%20Isabela's%20rice%20yields%20are%205.0%2C%203.5%2C%205.0%20t%2Fha%3A%20ranks%201%2C%203%2C%201.%20Ties%20share%20the%20lowest%20rank%20of%20the%20tie%20(rank%20method%20%5C%22min%5C%22).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(households%2C%20plots)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20three%20best%20rice%20yields%20of%20each%20province.%0A%20%20%20%20show_polars(top_rice_plots%2C%20plots%2C%20households%2C%203)%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%20Stretch%20S2.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%20rank%201%20is%20the%20**lowest**%20yield%20%7C%20Ranked%20in%20ascending%20order.%20%7C%20%60descending%3DTrue%60%20%7C%0A%20%20%20%20%7C%20only%20one%20province%20has%20a%20rank%201%20%7C%20Ranked%20across%20the%20whole%20survey.%20%7C%20%60.over(%22province%22)%60%20%7C%0A%20%20%20%20%7C%20ties%20ranked%201%20and%202%2C%20one%20of%20them%20dropped%20%7C%20The%20%60%22ordinal%22%60%20method%2C%20or%20%60head(n)%60%20after%20a%20sort.%20%7C%20%60rank(%22min%22%2C%20...)%60%20and%20%60filter(pl.col(%22rank%22)%20%3C%3D%20n)%60%20%7C%0A%20%20%20%20%7C%20ranks%201%2C%201%2C%202%20instead%20of%201%2C%201%2C%203%20%7C%20The%20%60%22dense%22%60%20method.%20%7C%20%60rank(%22min%22%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20ranks%20like%201.5%20%7C%20The%20default%20method%2C%20%60%22average%22%60%2C%20gives%20ties%20the%20mean%20of%20their%20ranks.%20%7C%20%60rank(%22min%22%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20Corn%20in%20the%20result%20%7C%20The%20rice%20filter%20is%20missing.%20%7C%20%60filter(pl.col(%22crop%22)%20%3D%3D%20%22Rice%22)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S2.5%20%C2%B7%20Codes%20that%20match%20nothing%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20join%20with%20the%20code%20list%20brings%20each%20household%20its%20municipality's%20name%20and%20income%20class.%20A%20household%20whose%20%60psgc%60%20is%20not%20in%20the%20code%20list%20gets%20nothing%3A%20dropped%20by%20an%20inner%20join%2C%20or%20left%20with%20empty%20columns%20by%20a%20left%20join%2C%20and%20nobody%20is%20told.%20Before%20the%20join%2C%20an%20anti-join%20lists%20exactly%20what%20it%20would%20miss.%20On%20the%20clean%20survey%20that%20list%20is%20empty%3B%20on%20households%20read%20carelessly%2C%20with%20%60psgc%60%20as%20a%20number%2C%20it%20is%20most%20of%20the%20survey.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20households%20and%20the%20code%20list%20(polars%2C%20%60psgc%60%20and%20%60code%60%20both%20text)%20%E2%86%92%20one%20row%20per%20household%20%60psgc%60%20that%20is%20**not**%20a%20%60code%60%20of%20the%20code%20list%2C%20with%20%60households%60%2C%20how%20many%20households%20have%20it%2C%20sorted%20by%20%60psgc%60.%20Nothing%20unmatched%20gives%20a%20table%20with%20no%20rows.%0A%0A%20%20%20%20%7C%20psgc%20%7C%20households%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20102802000%20%7C%2010%20%7C%0A%0A%20%20%20%20(That%20row%20is%20from%20a%20careless%20read%3A%20Bacarra%2C%20Ilocos%20Norte%2C%20is%20%600102802000%60.%20Read%20as%20a%20number%20and%20back%20to%20text%2C%20the%20leading%20zero%20is%20gone%20and%20the%20code%20matches%20nothing.)%0A%0A%20%20%20%20**Tools%3A**%20%60join(code_list%2C%20left_on%3D%22psgc%22%2C%20right_on%3D%22code%22%2C%20how%3D%22anti%22)%60%3B%20%60group_by(%22psgc%22).agg(households%3Dpl.len())%60%3B%20%60sort(%22psgc%22)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20unmatched_codes(households%3A%20pl.DataFrame%2C%20code_list%3A%20pl.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20household%20codes%20that%20are%20not%20in%20the%20code%20list%2C%20with%20how%20many%20households%20have%20each.%22%22%22%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20households%20whose%20psgc%20is%20not%20a%20code%20of%20the%20code%20list%20(an%20anti-join)%0A%20%20%20%20%23%20Step%202%3A%20count%20the%20households%20of%20each%20psgc%3B%20sort%20by%20psgc%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S2.5%22)%0A%0A%0A%40app.function%0Adef%20careless_households()%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20read%20without%20saying%20psgc%20is%20text%2C%20then%20made%20text%3A%20the%20leading%20zeros%20are%20gone.%22%22%22%0A%20%20%20%20return%20pl.read_csv(PATHS.survey%20%2F%20%22households.csv%22).with_columns(pl.col(%22psgc%22).cast(pl.String))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(code_list)%3A%0A%20%20%20%20_small%20%3D%20pl.DataFrame(%7B%22hh_id%22%3A%20%5B%22H1%22%2C%20%22H2%22%2C%20%22H3%22%2C%20%22H4%22%5D%2C%20%22psgc%22%3A%20%5B%220603001000%22%2C%20%22603001000%22%2C%20%220603001000%22%2C%20%229999999999%22%5D%7D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.5%22%2C%20unmatched_codes%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20read%20with%20psgc%20as%20text%22%2C%20(pl.from_pandas(course.households())%2C%20code_list))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20households%20read%20carelessly%3A%20psgc%20as%20a%20number%2C%20then%20text%22%2C%20(careless_households()%2C%20code_list))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20households%3A%20a%20good%20code%2C%20the%20same%20code%20without%20its%20zero%2C%20and%20a%20code%20that%20does%20not%20exist%22%2C%20(_small%2C%20code_list))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20table%3A%20in_order(table%2C%20%5B%22psgc%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Two%20columns%2C%20psgc%20and%20households%3B%20an%20empty%20table%20when%20every%20code%20matches.%20The%20careless%20read%20has%20854%20unmatched%20rows.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(code_list)%3A%0A%20%20%20%20%23%20Try%20it%3A%20what%20a%20careless%20read%20would%20lose%20in%20the%20join.%0A%20%20%20%20show_polars(unmatched_codes%2C%20careless_households()%2C%20code_list)%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%20Stretch%20S2.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%20the%20codes%20that%20**do**%20match%20%7C%20A%20semi%20or%20inner%20join%20instead%20of%20%60how%3D%22anti%22%60.%20%7C%20%60how%3D%22anti%22%60%20%7C%0A%20%20%20%20%7C%20*ColumnNotFoundError%3A%20psgc*%20or%20*code*%20%7C%20%60on%3D%22psgc%22%60%2C%20but%20the%20code%20list%20calls%20its%20key%20%60code%60.%20%7C%20%60left_on%3D%22psgc%22%2C%20right_on%3D%22code%22%60%20%7C%0A%20%20%20%20%7C%20*SchemaError%3A%20datatypes%20of%20join%20keys%20don't%20match*%20%7C%20One%20key%20is%20a%20number.%20%7C%20both%20keys%20as%20text%20%7C%0A%20%20%20%20%7C%20one%20row%20per%20household%2C%20not%20per%20code%20%7C%20The%20count%20is%20missing.%20%7C%20%60group_by(%22psgc%22).agg(households%3Dpl.len())%60%20%7C%0A%20%20%20%20%7C%20columns%20of%20the%20code%20list%20in%20the%20result%20%7C%20A%20left%20join%2C%20then%20a%20filter%20on%20missing%20names.%20%7C%20an%20anti-join%20keeps%20only%20the%20households'%20columns%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
42cf6fd55dbd42b35b4c6325c119d187