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%22polars%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20os%0A%20%20%20%20import%20time%0A%0A%20%20%20%20import%20altair%20as%20alt%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%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20waiting_for%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%20polars%0A%0A%20%20%20%20**Where%20you%20are%3A**%20modules%203%20to%205%20taught%20you%20the%20analysis%20in%20pandas%3A%20read%20with%20the%20right%20types%2C%20filter%2C%20add%20columns%2C%20group%2C%20weight%2C%20join.%20This%20module%20does%20**the%20same%20tasks%20in%20polars**%2C%20a%20second%20DataFrame%20library.%20You%20will%20meet%20it%20in%20other%20people's%20code%2C%20and%20in%20work%20with%20files%20too%20big%20for%20pandas%20to%20be%20comfortable.%0A%0A%20%20%20%20In%20this%20module%20you%20will%3A%0A%0A%20%20%20%201.%20write%20the%20analysis%20you%20know%20as%20polars%20**expressions**%2C%20side%20by%20side%20with%20the%20pandas%20you%20already%20have%3B%0A%20%20%20%202.%20see%20what%20changes%20because%20polars%20has%20**no%20index**%3B%0A%20%20%20%203.%20build%20a%20**lazy%20query**%2C%20read%20the%20plan%20polars%20makes%20of%20it%2C%20and%20run%20it%3B%0A%20%20%20%204.%20move%20tables%20between%20pandas%20and%20polars%2C%20and%20decide%20which%20one%20a%20job%20calls%20for.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20modules%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%3A%20polars'%20messages%20are%20long%2C%20but%20their%20first%20sentence%20names%20the%20problem%2C%20and%20often%20the%20fix.%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(%226.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20The%20same%20tasks%20as%20expressions%0A%0A%20%20%20%20A%20colleague%20sends%20you%20a%20notebook%20that%20computes%20the%20rice%20yield%20by%20province.%20You%20know%20what%20the%20analysis%20should%20do%2C%20because%20you%20did%20it%20in%20%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py).%20But%20the%20notebook%20is%20written%20like%20this%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20plots.filter(pl.col(%22crop%22)%20%3D%3D%20%22Rice%22).group_by(%22province%22).agg(pl.col(%22yield_t_ha%22).mean())%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20To%20check%20it%2C%20or%20to%20write%20the%20next%20one%2C%20you%20need%20to%20read%20and%20write%20polars.%20The%20good%20news%3A%20the%20tasks%20are%20the%20ones%20you%20know.%20Only%20the%20words%20change%2C%20and%20one%20idea%20is%20new.%0A%0A%20%20%20%20**polars**%20is%20a%20DataFrame%20library%2C%20like%20pandas%3A%20a%20table%20of%20named%20columns%2C%20each%20of%20one%20type.%20It%20is%20written%20in%20Rust%2C%20a%20fast%20compiled%20language%2C%20keeps%20each%20column%20in%20the%20Apache%20Arrow%20format%2C%20and%20runs%20on%20all%20the%20processor%20cores%20your%20computer%20has.%20It%20is%20imported%20as%20%60pl%60%2C%20as%20pandas%20is%20imported%20as%20%60pd%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20An%20expression%20describes%20a%20column%3B%20a%20context%20runs%20it%0A%0A%20%20%20%20%60pl.col(%22area_ha%22)%60%20is%20not%20a%20column%20of%20numbers.%20It%20is%20a%20**description**%3A%20%22the%20column%20named%20%60area_ha%60%22.%20%60pl.col(%22production_kg%22)%20%2F%20pl.col(%22area_ha%22)%20%2F%201000%60%20describes%20a%20new%20column%20made%20from%20two.%20An%20expression%20does%20nothing%20on%20its%20own%2C%20until%20you%20hand%20it%20to%20a%20**context**%3A%20a%20method%20of%20the%20table%20that%20runs%20it.%0A%0A%20%20%20%20%7C%20context%20%7C%20what%20it%20does%20with%20expressions%20%7C%20the%20pandas%20you%20know%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60table.select(...)%60%20%7C%20keeps%20only%20the%20columns%20the%20expressions%20give%20%7C%20%60table%5B%5B%22a%22%2C%20%22b%22%5D%5D%60%20%7C%0A%20%20%20%20%7C%20%60table.with_columns(...)%60%20%7C%20adds%20or%20replaces%20columns%2C%20and%20keeps%20the%20rest%20%7C%20%60table.assign(...)%60%20%7C%0A%20%20%20%20%7C%20%60table.filter(...)%60%20%7C%20keeps%20the%20rows%20where%20the%20expression%20is%20true%20%7C%20%60table%5Bcondition%5D%60%20%7C%0A%20%20%20%20%7C%20%60table.group_by(...).agg(...)%60%20%7C%20one%20row%20per%20group%3B%20each%20expression%20gives%20one%20value%20per%20group%20%7C%20%60table.groupby(...).agg(...)%60%20%7C%0A%20%20%20%20%7C%20%60table.sort(...)%60%20%7C%20orders%20the%20rows%20%7C%20%60table.sort_values(...)%60%20%7C%0A%0A%20%20%20%20Name%20a%20new%20column%20with%20a%20keyword%2C%20%60with_columns(area_ha%3D...)%60%2C%20or%20with%20%60.alias(%22area_ha%22)%60%20at%20the%20end%20of%20the%20expression.%0A%0A%20%20%20%20Why%20describe%20instead%20of%20compute%3F%20Because%20polars%20sees%20the%20whole%20expression%20before%20it%20runs%20anything%2C%20it%20can%20run%20independent%20expressions%20at%20the%20same%20time%20on%20several%20cores%2C%20and%2C%20in%20section%202%2C%20reorder%20the%20work%20so%20less%20of%20it%20is%20done.%0A%0A%20%20%20%20This%20notebook%20runs%20polars%20%7Bpl.__version__%7D%20and%20pandas%20%7Bpd.__version__%7D.%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%20The%20same%20task%2C%20in%20both%0A%0A%20%20%20%20Pick%20a%20task%3A%20the%20same%20question%2C%20answered%20in%20pandas%20and%20in%20polars%2C%20on%20the%20survey.%20To%20start%20both%20from%20the%20same%20tables%2C%20this%20cell%20turns%20the%20course's%20clean%20pandas%20tables%20into%20polars%20with%20%60pl.from_pandas(...)%60%3B%20section%202%20says%20more%20about%20moving%20between%20the%20two.%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%20survey%2C%20clean%2C%20in%20both%20libraries%20(course.plots()%20and%20course.households()%20are%20module%205's%20clean%20tables)%0A%20%20%20%20plots_pd%20%3D%20course.plots()%0A%20%20%20%20households_pd%20%3D%20course.households()%0A%20%20%20%20plots_pl%20%3D%20pl.from_pandas(plots_pd)%0A%20%20%20%20households_pl%20%3D%20pl.from_pandas(households_pd)%0A%20%20%20%20return%20households_pd%2C%20households_pl%2C%20plots_pd%2C%20plots_pl%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20task%3A%20the%20pandas%20code%2C%20the%20polars%20code%2C%20and%20what%20to%20notice.%20The%20code%20shown%20is%20the%20code%20that%20runs.%0A%20%20%20%20SIDE_BY_SIDE%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Read%20the%20households%2C%20with%20psgc%20as%20text%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'pd.read_csv(PATHS.survey%20%2F%20%22households.csv%22%2C%20dtype%3D%7B%22psgc%22%3A%20str%7D)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'pl.read_csv(PATHS.survey%20%2F%20%22households.csv%22%2C%20schema_overrides%3D%7B%22psgc%22%3A%20pl.String%7D)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%60schema_overrides%60%20is%20polars'%20%60dtype%3D%60%3A%20name%20the%20columns%20whose%20type%20you%20set%2C%20and%20polars%20guesses%20the%20rest.%20Without%20it%2C%20polars%20reads%20%60psgc%60%20as%20a%20whole%20number%20and%20the%20leading%20zero%20is%20lost%2C%20exactly%20as%20in%20pandas.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22Select%20three%20columns%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots%5B%5B%22hh_id%22%2C%20%22crop%22%2C%20%22area_ha%22%5D%5D'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.select(%22hh_id%22%2C%20%22crop%22%2C%20%22area_ha%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22The%20pandas%20table%20keeps%20its%20index%20on%20the%20left%3B%20the%20polars%20table%20has%20none.%20Its%20rows%20are%20only%20positions.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22Filter%3A%20corn%20plots%20that%20are%20not%20irrigated%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots%5B(plots%5B%22crop%22%5D%20%3D%3D%20%22Corn%22)%20%26%20(plots%5B%22irrigated%22%5D%20%3D%3D%20%22No%22)%5D'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.filter(pl.col(%22crop%22)%20%3D%3D%20%22Corn%22%2C%20pl.col(%22irrigated%22)%20%3D%3D%20%22No%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Several%20conditions%20given%20to%20%60filter%60%2C%20separated%20by%20commas%2C%20must%20all%20hold%2C%20so%20you%20need%20no%20%60%26%60%20and%20no%20brackets.%20%60%26%60%20works%20too%2C%20with%20brackets%20around%20each%20comparison%2C%20as%20in%20pandas.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22A%20new%20column%3A%20fertilizer%20per%20hectare%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.assign(fertilizer_kg_ha%3Dplots%5B%22fertilizer_kg%22%5D%20%2F%20plots%5B%22area_ha%22%5D)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.with_columns(fertilizer_kg_ha%3Dpl.col(%22fertilizer_kg%22)%20%2F%20pl.col(%22area_ha%22))'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Both%20return%20a%20new%20table%20and%20leave%20%60plots%60%20as%20it%20was.%20A%20plot%20with%20no%20fertilizer%20recorded%20gives%20a%20missing%20value%20in%20both%3A%20%60NaN%60%20in%20pandas%2C%20%60null%60%20in%20polars.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22Group%3A%20plots%20and%20area%20by%20crop%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.groupby(%22crop%22).agg(plots%3D(%22plot_no%22%2C%20%22size%22)%2C%20area_ha%3D(%22area_ha%22%2C%20%22sum%22))'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.group_by(%22crop%22).agg(plots%3Dpl.len()%2C%20area_ha%3Dpl.col(%22area_ha%22).sum()).sort(%22crop%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22**Count%20the%20rows.**%20polars%20keeps%20the%2014%20plots%20with%20no%20crop%20as%20a%20group%20of%20their%20own%20(%60null%60)%3B%20pandas%20leaves%20them%20out%20unless%20you%20pass%20%60dropna%3DFalse%60.%20And%20polars%20gives%20groups%20in%20no%20fixed%20order%2C%20so%20the%20code%20sorts%20them.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22Weighted%20mean%3A%20household%20size%20by%20province%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'(households.assign(x_w%3Dhouseholds%5B%22hh_size%22%5D%20*%20households%5B%22weight%22%5D)%5Cn%20%20%20%20.groupby(%22province%22)%5B%5B%22x_w%22%2C%20%22weight%22%5D%5D.sum()%5Cn%20%20%20%20.assign(hh_size%3Dlambda%20t%3A%20t%5B%22x_w%22%5D%20%2F%20t%5B%22weight%22%5D)%5B%5B%22hh_size%22%5D%5D)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'households.group_by(%22province%22).agg(%5Cn%20%20%20%20hh_size%3D(pl.col(%22hh_size%22)%20*%20pl.col(%22weight%22)).sum()%20%2F%20pl.col(%22weight%22).sum()%5Cn).sort(%22province%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22In%20polars%20a%20weighted%20mean%20is%20one%20expression%20inside%20%60agg%60%3A%20the%20sum%20of%20value%20times%20weight%2C%20over%20the%20sum%20of%20the%20weights%2C%20worked%20out%20for%20each%20group.%20No%20helper%20column%20is%20needed.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22Join%3A%20each%20plot's%20province%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.merge(households%5B%5B%22hh_id%22%2C%20%22province%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.join(households.select(%22hh_id%22%2C%20%22province%22)%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22%2C%20validate%3D%22m%3A1%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%60validate%3D%60%20exists%20in%20both%3A%20%60%5C%22m%3A1%5C%22%60%20is%20polars'%20short%20form%20of%20%60%5C%22many_to_one%5C%22%60.%20If%20a%20household%20were%20listed%20twice%2C%20polars%20would%20stop%20with%20*ComputeError%3A%20join%20keys%20did%20not%20fulfill%20m%3A1%20validation*.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22Sort%3A%20the%20largest%20plots%20first%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.sort_values(%22area_ha%22%2C%20ascending%3DFalse)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'plots.sort(%22area_ha%22%2C%20descending%3DTrue)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%60descending%3DTrue%60%20where%20pandas%20says%20%60ascending%3DFalse%60.%20The%20pandas%20result%20keeps%20each%20row's%20old%20index%20label%2C%20so%20the%20labels%20are%20now%20out%20of%20order%3B%20polars%20has%20no%20labels%20to%20carry.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%7D%0A%20%20%20%20task%20%3D%20mo.ui.dropdown(options%3Dlist(SIDE_BY_SIDE)%2C%20value%3D%22Group%3A%20plots%20and%20area%20by%20crop%22%2C%20label%3D%22Task%22)%0A%20%20%20%20task%0A%20%20%20%20return%20SIDE_BY_SIDE%2C%20task%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(SIDE_BY_SIDE%2C%20households_pd%2C%20households_pl%2C%20plots_pd%2C%20plots_pl%2C%20task)%3A%0A%20%20%20%20_pandas_code%2C%20_polars_code%2C%20_notice%20%3D%20SIDE_BY_SIDE%5Btask.value%5D%0A%20%20%20%20_pandas%20%3D%20eval(_pandas_code%2C%20%7B%22pd%22%3A%20pd%2C%20%22PATHS%22%3A%20PATHS%2C%20%22plots%22%3A%20plots_pd%2C%20%22households%22%3A%20households_pd%7D)%0A%20%20%20%20_polars%20%3D%20eval(_polars_code%2C%20%7B%22pl%22%3A%20pl%2C%20%22PATHS%22%3A%20PATHS%2C%20%22plots%22%3A%20plots_pl%2C%20%22households%22%3A%20households_pl%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**pandas**%5Cn%5Cn%60%60%60python%5Cn%7B_pandas_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**polars**%5Cn%5Cn%60%60%60python%5Cn%7B_polars_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%20widths%3D%22equal%22%2C%20gap%3D1)%2C%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(f%22**What%20to%20notice.**%20%7B_notice%7D%22)%2C%20kind%3D%22neutral%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**pandas**%20gives%20%7Blen(_pandas)%3A%2C%7D%20rows%3B%20the%20first%20ones%3A%22)%2C%0A%20%20%20%20%20%20%20%20_pandas.head(6)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**polars**%20gives%20%7B_polars.height%3A%2C%7D%20rows%3B%20the%20first%20ones%3A%22)%2C%0A%20%20%20%20%20%20%20%20_polars.head(6)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20polars%20has%20no%20index%0A%0A%20%20%20%20In%20pandas%20every%20row%20has%20a%20label%2C%20the%20**index**%2C%20and%20%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%20showed%20how%20much%20rides%20on%20it%3A%20%60loc%60%2C%20alignment%2C%20%60set_index%60.%20polars%20has%20none.%20A%20row%20is%20only%20its%20position%2C%20and%20every%20key%20is%20an%20ordinary%20column.%20What%20that%20changes%3A%0A%0A%20%20%20%20%7C%20in%20pandas%20%7C%20in%20polars%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60loc%60%20selects%20rows%20by%20their%20label%20%7C%20%60filter%60%20selects%20rows%20by%20a%20condition%3B%20%60table%5B0%5D%60%20or%20%60table.row(0)%60%20by%20position%20%7C%0A%20%20%20%20%7C%20%60set_index(%22hh_id%22)%60%2C%20%60reset_index()%60%20%7C%20nothing%20to%20set%3A%20%60hh_id%60%20stays%20a%20column%2C%20and%20joins%20and%20groups%20use%20columns%20%7C%0A%20%20%20%20%7C%20arithmetic%20between%20two%20Series%20lines%20up%20their%20labels%20first%20(alignment)%2C%20and%20a%20label%20in%20only%20one%20gives%20%60NaN%60%20%7C%20expressions%20combine%20columns%20of%20the%20same%20table%2C%20row%20by%20row%3A%20nothing%20to%20line%20up%2C%20and%20no%20%60NaN%60%20from%20labels%20that%20don't%20match%20%7C%0A%20%20%20%20%7C%20a%20%60groupby%60%20result%20has%20the%20groups%20as%20its%20index%20%7C%20a%20%60group_by%60%20result%20has%20the%20groups%20as%20an%20ordinary%20column%20%7C%0A%20%20%20%20%7C%20%60groupby%60%20sorts%20the%20groups%20%7C%20%60group_by%60%20gives%20the%20groups%20in%20no%20fixed%20order%3A%20%60.sort(...)%60%20after%20it%2C%20or%20%60group_by(...%2C%20maintain_order%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20a%20time%20series%20uses%20a%20date%20index%20(%60resample%60%2C%20in%20%5Bmodule%205%5D(%2F%3Ffile%3D40_cleaning_and_time.py))%20%7C%20the%20date%20stays%20a%20column%3A%20%60group_by_dynamic(%22date%22%2C%20every%3D%221mo%22)%60%20groups%20by%20period%20%7C%0A%0A%20%20%20%20The%20last-but-one%20row%20matters%20for%20your%20own%20work%3A%20run%20the%20same%20%60group_by%60%20twice%20and%20the%20rows%20may%20come%20back%20in%20a%20different%20order.%20Sort%20a%20result%20before%20you%20show%20it%20or%20compare%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Where%20polars%20and%20pandas%20differ%20on%20the%20survey%0A%0A%20%20%20%20The%20survey's%20traps%20from%20modules%203%20and%205%20are%20still%20in%20the%20files.%20polars%20meets%20some%20of%20them%20differently%3A%0A%0A%20%20%20%20%7C%20on%20the%20survey%20%7C%20pandas%20%7C%20polars%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60production_kg%60%20is%20written%20%60%221%2C640%22%60%20%7C%20%60read_csv(...%2C%20thousands%3D%22%2C%22)%60%20%7C%20%60read_csv%60%20has%20no%20%60thousands%3D%60%3A%20read%20the%20column%20as%20text%2C%20then%20%60pl.col(%22production_kg%22).str.replace_all(%22%2C%22%2C%20%22%5C%22).cast(pl.Int64)%60%20%7C%0A%20%20%20%20%7C%20the%20type%20of%20each%20column%20%7C%20guessed%20from%20the%20whole%20column%20%7C%20guessed%20from%20the%20**first%20100%20rows**%20(%60infer_schema_length%3D100%60)%3B%20a%20column%20with%20no%20comma%20in%20its%20first%20100%20rows%20is%20read%20as%20numbers%2C%20and%20%60.str%60%20then%20fails.%20Name%20the%20type%20you%20need%20in%20%60schema_overrides%60%20%7C%0A%20%20%20%20%7C%20a%20plot%20with%20no%20crop%20%7C%20%60NaN%60%2C%20left%20out%20by%20%60groupby%60%20%7C%20%60null%60%2C%20kept%20by%20%60group_by%60%20as%20a%20group%20of%20its%20own%20%7C%0A%20%20%20%20%7C%20missing%20values%20%7C%20%60NaN%60%20or%20%60%3CNA%3E%60%2C%20depending%20on%20the%20type%20%7C%20%60null%60%2C%20in%20every%20type.%20%60NaN%60%20is%20only%20a%20float%20that%20is%20%22not%20a%20number%22%2C%20and%20%60is_null()%60%20does%20not%20count%20it%20%7C%0A%20%20%20%20%7C%20change%20a%20column%20%7C%20%60plots%5B%22x%22%5D%20%3D%20...%60%20%7C%20refused%3A%20*TypeError%3A%20DataFrame%20object%20does%20not%20support%20%60Series%60%20assignment%20by%20index.%20Use%20DataFrame.with_columns*.%20A%20polars%20table%20never%20changes%20in%20place%20%7C%0A%20%20%20%20%7C%20combine%20conditions%20%7C%20%60%26%60%2C%20%60%5C%7C%60%2C%20%60~%60%2C%20with%20brackets%20%7C%20the%20same%3B%20or%20several%20conditions%20in%20%60filter(...)%60%2C%20separated%20by%20commas%20%7C%0A%20%20%20%20%7C%20drop%20repeated%20rows%20%7C%20%60drop_duplicates()%60%20%7C%20%60unique(maintain_order%3DTrue)%60%2C%20which%20keeps%20the%20rows%20in%20their%20order%20%7C%0A%20%20%20%20%7C%20a%20key%20that%20should%20be%20unique%20in%20a%20join%20%7C%20%60validate%3D%22many_to_one%22%60%20%7C%20%60validate%3D%22m%3A1%22%60%20%7C%0A%0A%20%20%20%20Forgetting%20the%20brackets%20fails%20as%20in%20pandas%2C%20with%20a%20different%20message%3A%20%60pl.col(%22crop%22)%20%3D%3D%20%22Rice%22%20%26%20pl.col(%22irrigated%22)%20%3D%3D%20%22Yes%22%60%20raises%20*TypeError%3A%20the%20truth%20value%20of%20an%20Expr%20is%20ambiguous*.%20Python%20works%20out%20%60%22Rice%22%20%26%20pl.col(...)%60%20first%2C%20then%20has%20two%20comparisons%20to%20chain.%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**If%20you%20know%20pandas%202.**%20In%20pandas%202%2C%20changing%20a%20table%20taken%20from%20another%20(%60rice%20%3D%20plots%5B...%5D%60%2C%20then%20%60rice%5B%22x%22%5D%20%3D%201%60)%20could%20give%20a%20*SettingWithCopyWarning*%2C%20because%20whether%20%60plots%60%20changed%20too%20depended%20on%20how%20the%20part%20was%20taken%3A%20a%20slice%20could%20share%20its%20data%20with%20%60plots%60%2C%20a%20filter%20made%20a%20copy.%20pandas%203's%20copy-on-write%20ends%20that%3A%20there%20is%20no%20warning%2C%20and%20changing%20%60rice%60%20never%20changes%20%60plots%60.%20polars%20never%20had%20the%20question%3A%20no%20table%20can%20be%20changed%20in%20place%2C%20so%20every%20step%20returns%20a%20new%20table%20and%20the%20old%20one%20stays%20as%20it%20was.%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%23%20Exercise%206.1.%20Read%20the%20plots%2C%20the%20polars%20way%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20in%20%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%20you%20read%20%60plots.csv%60%20with%20the%20right%20types%20and%20in%20module%205%20you%20cleaned%20it.%20Every%20polars%20analysis%20of%20the%20plots%20starts%20with%20the%20same%20steps%2C%20so%20they%20belong%20in%20one%20function.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20file%20(a%20path%2C%20or%20the%20file's%20contents%20as%20bytes%3A%20%60pl.read_csv%60%20takes%20either)%20%E2%86%92%20a%20polars%20DataFrame%20with%20the%20file's%20eight%20columns%2C%20then%20%60area_ha%60%3A%0A%0A%20%20%20%20-%20%60production_kg%60%20as%20whole%20numbers%20(%60Int64%60)%2C%20its%20thousands%20separators%20removed%3B%0A%20%20%20%20-%20each%20row%20that%20is%20an%20exact%20repeat%20of%20another%20kept%20only%20once%3B%0A%20%20%20%20-%20%60area_ha%60%3A%20%60area%60%20when%20%60area_unit%60%20is%20%60%22ha%22%60%2C%20%60area%20%2F%2010_000%60%20when%20it%20is%20%60%22sqm%22%60.%0A%0A%20%20%20%20%7C%20hh_id%20%7C%20plot_no%20%7C%20crop%20%7C%20area%20%7C%20area_unit%20%7C%20%E2%80%A6%20%7C%20production_kg%20%7C%20fertilizer_kg%20%7C%20area_ha%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20HH-0001%20%7C%201%20%7C%20Rice%20%7C%200.439%20%7C%20ha%20%7C%20%E2%80%A6%20%7C%201640%20%7C%20100.7%20%7C%200.439%20%7C%0A%20%20%20%20%7C%20HH-0001%20%7C%202%20%7C%20Rice%20%7C%201940.0%20%7C%20sqm%20%7C%20%E2%80%A6%20%7C%20700%20%7C%20null%20%7C%200.194%20%7C%0A%0A%20%20%20%20A%20blank%20crop%20stays%20%60null%60%3A%20polars%20reads%20an%20empty%20field%20as%20missing%2C%20and%20the%20plot%20is%20still%20a%20plot.%0A%0A%20%20%20%20**Tools%3A**%20%60pl.read_csv(source%2C%20schema_overrides%3D%7B%22production_kg%22%3A%20pl.String%7D)%60%20so%20the%20column%20is%20text%20whatever%20its%20first%20rows%20hold%3B%20%60.str.replace_all(%22%2C%22%2C%20%22%5C%22).cast(pl.Int64)%60%3B%20%60.unique(maintain_order%3DTrue)%60%3B%20and%20for%20the%20unit%2C%20polars'%20version%20of%20pandas'%20%60where%60%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20pl.when(pl.col(%22area_unit%22)%20%3D%3D%20%22sqm%22).then(pl.col(%22area%22)%20%2F%2010_000).otherwise(pl.col(%22area%22))%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_plots(source)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20file%20in%20polars%3A%20production%20as%20whole%20numbers%2C%20repeated%20rows%20once%2C%20and%20area_ha.%22%22%22%0A%20%20%20%20%23%20Exercise%206.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20read%20the%20source%20with%20pl.read_csv%2C%20with%20production_kg%20as%20text%20(schema_overrides)%0A%20%20%20%20%23%20Step%202%3A%20with_columns%3A%20production_kg%20without%20its%20commas%2C%20cast%20to%20pl.Int64%0A%20%20%20%20%23%20Step%203%3A%20keep%20each%20repeated%20row%20once%2C%20in%20order%0A%20%20%20%20%23%20Step%204%3A%20with_columns%3A%20area_ha%2C%20with%20pl.when(...).then(...).otherwise(...)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.1%22)%0A%0A%0A%40app.function%0Adef%20plots_csv_fixture(commas%3A%20bool)%20-%3E%20bytes%3A%0A%20%20%20%20%22%22%22A%20small%20plots%20file%2C%20as%20bytes%2C%20for%20the%20checks%20of%20Exercises%206.1%20and%206.4%3A%20a%20repeated%20row%2C%20a%20blank%20crop%2C%20a%20plot%20in%0A%20%20%20%20square%20meters%3B%20with%20thousands%20separators%2C%20or%20with%20none%20(so%20polars%20would%20guess%20numbers).%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22hh_id%2Cplot_no%2Ccrop%2Carea%2Carea_unit%2Cirrigated%2Cproduction_kg%2Cfertilizer_kg%22%2C%0A%20%20%20%20%20%20%20%20'HH-0001%2C1%2CRice%2C0.5%2Cha%2CYes%2C%222%2C100%22%2C40'%2C%0A%20%20%20%20%20%20%20%20%22HH-0001%2C2%2C%2C2500%2Csqm%2CNo%2C300%2C%22%2C%0A%20%20%20%20%20%20%20%20'HH-0002%2C1%2CCorn%2C1.2%2Cha%2CNo%2C%223%2C050%22%2C55.5'%2C%0A%20%20%20%20%20%20%20%20'HH-0001%2C1%2CRice%2C0.5%2Cha%2CYes%2C%222%2C100%22%2C40'%2C%0A%20%20%20%20%20%20%20%20%22HH-0003%2C1%2CRice%2C8000%2Csqm%2CYes%2C950%2C12%22%2C%0A%20%20%20%20%20%20%20%20'HH-0003%2C2%2CRice%2C1.5%2Cha%2CNo%2C%226%2C300%22%2C'%2C%0A%20%20%20%20%5D%20if%20commas%20else%20%5B%0A%20%20%20%20%20%20%20%20%22hh_id%2Cplot_no%2Ccrop%2Carea%2Carea_unit%2Cirrigated%2Cproduction_kg%2Cfertilizer_kg%22%2C%0A%20%20%20%20%20%20%20%20%22HH-0004%2C1%2CBanana%2C8000%2Csqm%2CNo%2C900%2C12%22%2C%0A%20%20%20%20%20%20%20%20%22HH-0004%2C2%2CRice%2C0.75%2Cha%2CYes%2C3200%2C%22%2C%0A%20%20%20%20%20%20%20%20%22HH-0004%2C2%2CRice%2C0.75%2Cha%2CYes%2C3200%2C%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20(%22%5Cn%22.join(rows)%20%2B%20%22%5Cn%22).encode(%22utf-8%22)%0A%0A%0A%40app.function%0Adef%20in_order(table%2C%20by%3A%20list%5Bstr%5D%2C%20*%2C%20types%3A%20bool%20%3D%20False)%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%20(with%20%60types%3DTrue%60%2C%20also%20every%20column's%20type).%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%20table%20%3D%20table.sort(by%2C%20nulls_last%3DTrue)%0A%20%20%20%20return%20course.with_dtypes(table)%20if%20types%20else%20table%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.%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%20elif%20%22does%20not%20support%20%60Series%60%20assignment%22%20in%20message%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20meaning%20%3D%20%22a%20column%20set%20with%20%60table%5B...%5D%20%3D%20...%60%3A%20polars%20tables%20never%20change%20in%20place%3B%20use%20%60with_columns%60.%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%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%20plan%20%3D%20result.explain().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.vstack(%5Bmo.md(f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20**a%20LazyFrame**%3A%20a%20query%2C%20not%20run%20yet.%20The%20plan%20polars%20will%20run%3A%5Cn%5Cn%60%60%60text%5Cn%7Bplan%7D%5Cn%60%60%60%5Cn%5CnIts%20result%2C%20with%20%60.collect()%60%3A%22)%2C%20result.collect()%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%20if%20isinstance(result%2C%20pd.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.dtypes.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%20pandas%20DataFrame%20of%20%7Blen(result)%3A%2C%7D%20rows**%2C%20indexed%20by%20%60%7Bresult.index.name%7D%60.%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%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.1%22%2C%20read_plots%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20file%20with%20a%20repeated%20row%2C%20a%20blank%20crop%20and%20two%20plots%20in%20square%20meters%22%2C%20(plots_csv_fixture(commas%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20file%20with%20no%20thousands%20separators%20at%20all%22%2C%20(plots_csv_fixture(commas%3DFalse)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots.csv%22%2C%20(PATHS.survey%20%2F%20%22plots.csv%22%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%22%2C%20summarize%3Dlambda%20table%3A%20in_order(table%2C%20%5B%22hh_id%22%2C%20%22plot_no%22%5D%2C%20types%3DTrue)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20compares%20the%20values%20and%20every%20column's%20type%2C%20in%20any%20row%20order.%20In%20the%20second%20file%20no%20number%20has%20a%20comma%2C%20so%20polars%20guesses%20whole%20numbers%20unless%20you%20name%20the%20type.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20read%20the%20survey's%20plots%20file%20with%20your%20function.%0A%20%20%20%20show_polars(read_plots%2C%20PATHS.survey%20%2F%20%22plots.csv%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%206.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*InvalidOperationError%3A%20conversion%20from%20%60str%60%20to%20%60i64%60%20failed*%20%7C%20The%20cast%20comes%20before%20the%20commas%20are%20removed.%20%7C%20%60.str.replace_all(%22%2C%22%2C%20%22%22)%60%20first%2C%20then%20%60.cast(pl.Int64)%60%20%7C%0A%20%20%20%20%7C%20*InvalidOperationError%3A%20expected%20String%20type%2C%20got%3A%20i64*%20(second%20file)%20%7C%20polars%20guessed%20numbers%2C%20so%20there%20is%20no%20text%20to%20clean.%20%7C%20%60schema_overrides%3D%7B%22production_kg%22%3A%20pl.String%7D%60%20%7C%0A%20%20%20%20%7C%20the%20first%20file%20has%206%20rows%2C%20not%205%20%7C%20The%20repeated%20row%20is%20still%20there.%20%7C%20%60.unique(maintain_order%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20%60area_ha%60%20is%20huge%20for%20the%20%60sqm%60%20plots%20%7C%20Multiplied%20by%2010%2C000%20instead%20of%20dividing%2C%20or%20the%20condition%20is%20the%20wrong%20way%20round.%20%7C%20%60.then(pl.col(%22area%22)%20%2F%2010_000)%60%20where%20the%20unit%20is%20%60%22sqm%22%60%20%7C%0A%20%20%20%20%7C%20%60production_kg%60%20is%20%60Float64%60%20%7C%20Cast%20to%20a%20decimal%20type.%20%7C%20%60.cast(pl.Int64)%60%20%7C%0A%20%20%20%20%7C%20fewer%20rows%20than%20expected%20%7C%20Rows%20with%20a%20blank%20crop%20or%20fertilizer%20were%20dropped.%20%7C%20no%20%60drop_nulls()%60%3A%20a%20missing%20value%20is%20not%20a%20reason%20to%20lose%20the%20plot%20%7C%0A%20%20%20%20%7C%20*ColumnNotFoundError*%20%7C%20A%20column%20name%20misspelt.%20%7C%20the%20names%20in%20the%20message's%20list%20of%20valid%20columns%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.2.%20Irrigated%20rice%2C%20and%20its%20yield%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20filtering%20and%20adding%20a%20column%20are%20the%20two%20steps%20of%20almost%20every%20analysis.%20In%20%5Bmodule%203%5D(%2F%3Ffile%3D20_dataframes.py)%20you%20wrote%20them%20in%20pandas%3B%20here%20they%20are%20%60filter%60%20and%20%60with_columns%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20clean%20plots%2C%20in%20polars%20(with%20%60area_ha%60)%20%E2%86%92%20only%20the%20plots%20whose%20%60crop%60%20is%20%60%22Rice%22%60%20**and**%20whose%20%60irrigated%60%20is%20%60%22Yes%22%60%2C%20with%20five%20columns%2C%20in%20this%20order%3A%20%60hh_id%60%2C%20%60plot_no%60%2C%20%60area_ha%60%2C%20%60production_kg%60%2C%20and%20a%20new%20%60yield_t_ha%60%2C%20the%20harvest%20in%20tonnes%20per%20hectare.%0A%0A%20%20%20%20%7C%20hh_id%20%7C%20plot_no%20%7C%20area_ha%20%7C%20production_kg%20%7C%20yield_t_ha%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20HH-0001%20%7C%202%20%7C%200.194%20%7C%20700%20%7C%203.608%E2%80%A6%20%7C%0A%0A%20%20%20%20A%20yield%20in%20tonnes%20per%20hectare%20is%20%60production_kg%20%2F%20area_ha%20%2F%201000%60%3A%20kilograms%20per%20hectare%2C%20then%201%2C000%20kilograms%20to%20the%20tonne.%20Use%20%60area_ha%60%2C%20never%20%60area%60%2C%20which%20is%20still%20in%20square%20meters%20for%20some%20plots.%0A%0A%20%20%20%20**Tools%3A**%20%60filter%60%20with%20two%20conditions%20(separated%20by%20a%20comma%2C%20or%20joined%20with%20%60%26%60%20and%20brackets)%2C%20%60with_columns(yield_t_ha%3D...)%60%2C%20%60select(...)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20irrigated_rice(plots%3A%20pl.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20irrigated%20rice%20plots%2C%20with%20their%20yield%20in%20tonnes%20per%20hectare.%22%22%22%0A%20%20%20%20%23%20Exercise%206.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20filter%20the%20rows%20whose%20crop%20is%20%22Rice%22%20and%20whose%20irrigated%20is%20%22Yes%22%0A%20%20%20%20%23%20Step%202%3A%20with_columns%3A%20yield_t_ha%20from%20production_kg%20and%20area_ha%0A%20%20%20%20%23%20Step%203%3A%20select%20the%20five%20columns%2C%20in%20order%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.2%22)%0A%0A%0A%40app.function%0Adef%20plots_fixture()%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Six%20clean%20plots%20of%20three%20households%2C%20for%20the%20checks%20of%20Exercises%206.2%20and%206.3%3A%20rice%20irrigated%20and%20not%2C%20a%20plot%0A%20%20%20%20measured%20in%20square%20meters%2C%20a%20corn%20plot%20and%20a%20plot%20with%20no%20crop.%22%22%22%0A%20%20%20%20return%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%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%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%202%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%2C%20%22Corn%22%2C%20None%2C%20%22Rice%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22area%22%3A%20%5B0.5%2C%202500.0%2C%201.0%2C%200.8%2C%200.4%2C%202.0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22area_unit%22%3A%20%5B%22ha%22%2C%20%22sqm%22%2C%20%22ha%22%2C%20%22ha%22%2C%20%22ha%22%2C%20%22ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22irrigated%22%3A%20%5B%22Yes%22%2C%20%22Yes%22%2C%20%22No%22%2C%20%22Yes%22%2C%20%22Yes%22%2C%20%22Yes%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B2400%2C%201500%2C%203000%2C%202800%2C%20900%2C%209000%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22fertilizer_kg%22%3A%20%5B40.0%2C%20None%2C%2055.0%2C%2030.0%2C%20None%2C%20120.0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22area_ha%22%3A%20%5B0.5%2C%200.25%2C%201.0%2C%200.8%2C%200.4%2C%202.0%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%0A%40app.function%0Adef%20households_fixture()%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20households%20in%20two%20provinces%2C%20with%20very%20different%20weights%2C%20for%20the%20check%20of%20Exercise%206.3.%22%22%22%0A%20%20%20%20return%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%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%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%20%20%20%20%22hh_size%22%3A%20%5B4%2C%206%2C%203%2C%205%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B100.0%2C%20300.0%2C%2050.0%2C%20999.0%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.2%22%2C%20irrigated_rice%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%3A%20irrigated%20and%20rain-fed%20rice%2C%20one%20in%20square%20meters%2C%20corn%2C%20and%20no%20crop%22%2C%20(plots_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20clean%20plots%22%2C%20(pl.from_pandas(course.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%20summarize%3Dlambda%20table%3A%20in_order(table%2C%20%5B%22hh_id%22%2C%20%22plot_no%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Only%20rice%20that%20is%20irrigated%2C%20five%20columns%20in%20the%20order%20given%2C%20and%20the%20yield%20from%20area_ha%2C%20in%20tonnes.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots_pl)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20irrigated%20rice%20plots%20of%20the%20survey.%0A%20%20%20%20show_polars(irrigated_rice%2C%20plots_pl)%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%206.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%20*TypeError%3A%20the%20truth%20value%20of%20an%20Expr%20is%20ambiguous*%20%7C%20%60and%60%20between%20the%20conditions%2C%20or%20%60%26%60%20without%20brackets%20around%20each%20comparison.%20%7C%20%60filter(cond1%2C%20cond2)%60%2C%20or%20%60(cond1)%20%26%20(cond2)%60%20%7C%0A%20%20%20%20%7C%20rain-fed%20rice%20or%20corn%20in%20the%20result%20%7C%20Only%20one%20of%20the%20two%20conditions%20is%20applied.%20%7C%20both%20conditions%20in%20%60filter%60%20%7C%0A%20%20%20%20%7C%20yields%201%2C000%20times%20too%20big%20%7C%20Kilograms%20per%20hectare%2C%20not%20tonnes.%20%7C%20%60%2F%201000%60%20at%20the%20end%20%7C%0A%20%20%20%20%7C%20one%20yield%20thousands%20of%20times%20too%20small%20%7C%20%60area%60%20used%20instead%20of%20%60area_ha%60.%20%7C%20%60pl.col(%22area_ha%22)%60%20%7C%0A%20%20%20%20%7C%20the%20columns%20in%20another%20order%2C%20or%20one%20too%20many%20%7C%20%60select%60%20is%20missing%2C%20or%20lists%20other%20columns.%20%7C%20%60select(%22hh_id%22%2C%20%22plot_no%22%2C%20%22area_ha%22%2C%20%22production_kg%22%2C%20%22yield_t_ha%22)%60%20%7C%0A%20%20%20%20%7C%20*ColumnNotFoundError%3A%20unable%20to%20find%20column%20%22yield_t_ha%22*%20%7C%20%60select%60%20comes%20before%20%60with_columns%60%2C%20so%20the%20column%20does%20not%20exist%20yet.%20%7C%20the%20order%20of%20the%20steps%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.3.%20The%20weighted%20rice%20yield%20by%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20headline%20number%20of%20the%20survey%2C%20from%20%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%3A%20how%20much%20rice%20a%20hectare%20gives%20in%20each%20province%2C%20as%20the%20province's%20farms%20would%20show%20it%2C%20not%20only%20the%20sample's.%20Each%20household%20stands%20for%20%60weight%60%20farm%20households%2C%20so%20a%20plot%20counts%20as%20many%20times%20as%20its%20household's%20weight.%20Unweighted%2C%20the%20rice%20yield%20of%20the%20whole%20survey%20is%204.35%20t%2Fha%3B%20weighted%2C%204.50.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20clean%20plots%20and%20the%20clean%20households%2C%20both%20in%20polars%20%E2%86%92%20one%20row%20per%20province%2C%20sorted%20by%20province%2C%20with%20four%20columns%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20plots%20%7C%20yield_t_ha%20%7C%20weighted_t_ha%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20province%20%7C%20how%20many%20rice%20plots%20%7C%20the%20mean%20of%20the%20plots'%20yields%20%7C%20the%20same%20mean%2C%20weighted%20by%20each%20plot's%20household%20weight%20%7C%0A%0A%20%20%20%20The%20steps%3A%20keep%20the%20rice%20plots%3B%20join%20each%20plot%20to%20its%20household's%20%60province%60%20and%20%60weight%60%20(a%20household%20has%20many%20plots%2C%20one%20plot%20one%20household%3A%20%60validate%3D%22m%3A1%22%60)%3B%20add%20each%20plot's%20yield%2C%20as%20in%20Exercise%206.2%3B%20then%20group%20by%20province.%0A%0A%20%20%20%20A%20worked%20example%3A%20two%20rice%20plots%20in%20one%20province%2C%20yields%203.0%20and%206.0%20t%2Fha%2C%20from%20households%20of%20weight%20300%20and%20100.%20Unweighted%2C%20(3.0%20%2B%206.0)%20%2F%202%20%3D%204.5.%20Weighted%2C%20(3.0%20%C3%97%20300%20%2B%206.0%20%C3%97%20100)%20%2F%20(300%20%2B%20100)%20%3D%203.75.%0A%0A%20%20%20%20**Tools%3A**%20%60join(...%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22%2C%20validate%3D%22m%3A1%22)%60%3B%20%60group_by(%22province%22).agg(plots%3Dpl.len()%2C%20yield_t_ha%3D...%2C%20weighted_t_ha%3D...)%60%3B%20the%20weighted%20mean%20as%20one%20expression%2C%20%60(pl.col(%22x%22)%20*%20pl.col(%22weight%22)).sum()%20%2F%20pl.col(%22weight%22).sum()%60%3B%20%60sort(%22province%22)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weighted_yield_by_province(plots%3A%20pl.DataFrame%2C%20households%3A%20pl.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Rice%20plots%2C%20mean%20yield%20and%20weighted%20mean%20yield%20(t%2Fha)%2C%20one%20row%20per%20province.%22%22%22%0A%20%20%20%20%23%20Exercise%206.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20filter%20the%20rice%20plots%0A%20%20%20%20%23%20Step%202%3A%20join%20hh_id%2C%20province%20and%20weight%20from%20the%20households%20(how%3D%22left%22%2C%20validate%3D%22m%3A1%22)%0A%20%20%20%20%23%20Step%203%3A%20with_columns%3A%20yield_t_ha%2C%20as%20in%20Exercise%206.2%0A%20%20%20%20%23%20Step%204%3A%20group_by%20province%3B%20agg%20plots%2C%20yield_t_ha%20(the%20mean)%20and%20weighted_t_ha%20(the%20weighted%20mean)%0A%20%20%20%20%23%20Step%205%3A%20sort%20by%20province%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.3%22%2C%20weighted_yield_by_province%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%20rice%20plots%20in%20two%20provinces%2C%20households%20weighted%20100%2C%20300%20and%2050%22%2C%20(plots_fixture()%2C%20households_fixture()))%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%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20tables%2C%20Iloilo's%20three%20rice%20plots%20have%20yields%204.8%2C%206.0%20and%203.0%20t%2Fha%3A%20their%20mean%20is%204.6%2C%20their%20weighted%20mean%203.96.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(households_pl%2C%20plots_pl)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20weighted%20rice%20yield%20of%20each%20province%20in%20the%20survey.%0A%20%20%20%20show_polars(weighted_yield_by_province%2C%20plots_pl%2C%20households_pl)%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%206.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60weighted_t_ha%60%20equals%20%60yield_t_ha%60%20%7C%20The%20weight%20is%20not%20in%20the%20formula%2C%20or%20both%20columns%20use%20the%20same%20expression.%20%7C%20%60(pl.col(%22yield_t_ha%22)%20*%20pl.col(%22weight%22)).sum()%20%2F%20pl.col(%22weight%22).sum()%60%20%7C%0A%20%20%20%20%7C%20%60weighted_t_ha%60%20hundreds%20of%20times%20too%20big%20%7C%20The%20mean%20of%20yield%20times%20weight%2C%20not%20divided%20by%20the%20weights.%20%7C%20divide%20by%20%60pl.col(%22weight%22).sum()%60%20%7C%0A%20%20%20%20%7C%20Iloilo's%20mean%20is%203.94%2C%20not%204.6%20%7C%20Total%20harvest%20over%20total%20area%2C%20not%20the%20mean%20of%20the%20plots'%20yields.%20%7C%20the%20yield%20of%20each%20plot%20first%2C%20then%20%60.mean()%60%20%7C%0A%20%20%20%20%7C%20more%20plots%20than%20expected%2C%20or%20Corn%20in%20the%20numbers%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%20%20%20%20%7C%20*ColumnNotFoundError%3A%20unable%20to%20find%20column%20%22province%22*%20%7C%20The%20join%20is%20missing%2C%20or%20comes%20after%20%60group_by%60.%20%7C%20join%20before%20grouping%20%7C%0A%20%20%20%20%7C%20*ComputeError%3A%20join%20keys%20did%20not%20fulfill%20m%3A1%20validation*%20%7C%20The%20households%20table%20lists%20a%20household%20twice.%20%7C%20the%20clean%20households%2C%20or%20%60unique()%60%20on%20them%20first%20%7C%0A%20%20%20%20%7C%20a%20%60province%60%20row%20that%20is%20%60null%60%20%7C%20A%20plot%20whose%20household%20is%20not%20in%20the%20households%20table.%20%7C%20%60how%3D%22left%22%60%20keeps%20it%3A%20check%20the%20keys%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20Lazy%20queries%3B%20moving%20between%20pandas%20and%20polars%3B%20when%20each%20fits%0A%0A%20%20%20%20Every%20month%20brings%20a%20new%20price%20file%2C%20and%20the%20next%20survey%20round%20may%20be%20ten%20times%20bigger.%20%60read_csv%60%20reads%20**every%20row%20and%20every%20column**%20into%20memory%20before%20your%20%60filter%60%20throws%20most%20of%20them%20away.%20For%20one%20survey%20of%20two%20thousand%20plots%20that%20costs%20nothing.%20For%20forty-seven%20price%20files%2C%20or%20a%20file%20of%20ten%20million%20rows%2C%20it%20is%20most%20of%20the%20work%2C%20and%20it%20may%20not%20fit%20in%20memory%20at%20all.%0A%0A%20%20%20%20%23%23%23%20A%20lazy%20query%3A%20describe%20it%20all%2C%20then%20run%20it%0A%0A%20%20%20%20polars%20can%20wait.%20%60pl.scan_csv(path)%60%20reads%20nothing%3A%20it%20gives%20a%20**LazyFrame**%2C%20a%20plan%20to%20read%20the%20file.%20You%20add%20steps%20to%20it%20with%20the%20contexts%20you%20know%20(%60filter%60%2C%20%60with_columns%60%2C%20%60group_by%60%2C%20%60select%60)%2C%20still%20without%20reading%20anything.%20Then%20%60.collect()%60%20runs%20the%20whole%20query%20at%20once%20and%20gives%20an%20ordinary%20DataFrame.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20query%20%3D%20(%0A%20%20%20%20%20%20%20%20pl.scan_csv(PATHS.survey%20%2F%20%22plots.csv%22%2C%20schema_overrides%3D%7B%22production_kg%22%3A%20pl.String%7D)%0A%20%20%20%20%20%20%20%20.with_columns(pl.col(%22production_kg%22).str.replace_all(%22%2C%22%2C%20%22%5C%22).cast(pl.Int64))%0A%20%20%20%20%20%20%20%20.filter(pl.col(%22crop%22)%20%3D%3D%20%22Rice%22)%0A%20%20%20%20%20%20%20%20.group_by(%22irrigated%22)%0A%20%20%20%20%20%20%20%20.agg(pl.col(%22production_kg%22).sum())%0A%20%20%20%20)%0A%20%20%20%20query.collect()%20%20%23%20the%20query%20runs%20only%20here%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Because%20polars%20sees%20the%20whole%20query%20before%20it%20runs%2C%20it%20can%20**rewrite**%20it.%20%60query.explain()%60%20shows%20the%20plan.%20Pick%20which%20plan%20to%20see%3B%20read%20a%20plan%20from%20the%20bottom%20up%2C%20the%20way%20the%20data%20flows.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20A%20lazy%20query%3A%20nothing%20is%20read%20until%20.collect()%0A%20%20%20%20query%20%3D%20(%0A%20%20%20%20%20%20%20%20pl.scan_csv(PATHS.survey%20%2F%20%22plots.csv%22%2C%20schema_overrides%3D%7B%22production_kg%22%3A%20pl.String%7D)%0A%20%20%20%20%20%20%20%20.with_columns(pl.col(%22production_kg%22).str.replace_all(%22%2C%22%2C%20%22%22).cast(pl.Int64))%0A%20%20%20%20%20%20%20%20.filter(pl.col(%22crop%22)%20%3D%3D%20%22Rice%22)%0A%20%20%20%20%20%20%20%20.group_by(%22irrigated%22)%0A%20%20%20%20%20%20%20%20.agg(pl.col(%22production_kg%22).sum())%0A%20%20%20%20)%0A%20%20%20%20type(query)%0A%20%20%20%20return%20(query%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20which_plan%20%3D%20mo.ui.radio(options%3D%5B%22the%20query%20as%20written%22%2C%20%22the%20plan%20polars%20runs%22%5D%2C%20value%3D%22the%20plan%20polars%20runs%22%2C%20label%3D%22Show%22)%0A%20%20%20%20which_plan%0A%20%20%20%20return%20(which_plan%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(query%2C%20which_plan)%3A%0A%20%20%20%20_plan%20%3D%20query.explain(optimized%3Dwhich_plan.value%20%3D%3D%20%22the%20plan%20polars%20runs%22)%0A%20%20%20%20_plan%20%3D%20_plan.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_what%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22the%20query%20as%20written%22%3A%20%22In%20the%20order%20you%20wrote%20it%3A%20read%20**all%208%20columns**%20(%60PROJECT%20*%2F8%20COLUMNS%60)%20of%20every%20row%2C%20fix%20the%20production%2C%20then%20%60FILTER%60%20out%20everything%20that%20is%20not%20rice%2C%20then%20group.%22%2C%0A%20%20%20%20%20%20%20%20%22the%20plan%20polars%20runs%22%3A%20%22The%20filter%20has%20moved%20**into%20the%20reader**%3A%20%60SELECTION%3A%20col(%5C%22crop%5C%22)%20%3D%3D%20%5C%22Rice%5C%22%60%20sits%20under%20%60Csv%20SCAN%60%2C%20so%20rows%20that%20are%20not%20rice%20are%20dropped%20as%20the%20file%20is%20read%20and%20never%20take%20up%20memory.%20This%20is%20**predicate%20pushdown**%20(a%20*predicate*%20is%20a%20condition%20that%20is%20true%20or%20false).%20And%20%60PROJECT%203%2F8%20COLUMNS%60%3A%20only%20the%20three%20columns%20the%20query%20uses%20are%20read%20at%20all.%20That%20is%20**projection%20pushdown**.%22%2C%0A%20%20%20%20%7D%5Bwhich_plan.value%5D%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22%60%60%60text%5Cn%7B_plan%7D%5Cn%60%60%60%22)%2C%20mo.md(_what)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(query)%3A%0A%20%20%20%20%23%20Run%20it%3A%20only%20now%20does%20polars%20read%20the%20file%0A%20%20%20%20query.collect().sort(%22irrigated%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%20Pushdown%20happens%20without%20you%20asking%3A%20you%20write%20the%20steps%20in%20the%20order%20that%20reads%20well%2C%20and%20polars%20works%20out%20the%20order%20that%20does%20least.%20A%20few%20more%20things%20about%20lazy%20frames%3A%0A%0A%20%20%20%20%7C%20you%20want%20%7C%20write%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20result%20%7C%20%60query.collect()%60%20%7C%0A%20%20%20%20%7C%20the%20columns%20and%20types%2C%20without%20running%20the%20query%20%7C%20%60query.collect_schema()%60%20%7C%0A%20%20%20%20%7C%20a%20lazy%20query%20from%20a%20table%20you%20already%20have%20%7C%20%60table.lazy()%60%20%7C%0A%20%20%20%20%7C%20a%20result%20bigger%20than%20memory%2C%20read%20in%20pieces%20%7C%20%60query.collect(engine%3D%22streaming%22)%60%20%7C%0A%20%20%20%20%7C%20many%20files%20at%20once%20%7C%20%60pl.scan_csv(PATHS.prices%20%2F%20%22farmgate_2022-*.csv%22)%60%3A%20a%20%60*%60%20in%20the%20name%20matches%20every%20file%20it%20fits%20(a%20**glob**)%20%7C%0A%0A%20%20%20%20Stay%20eager%20(%60read_csv%60)%20while%20you%20explore%20a%20small%20table%20step%20by%20step%20and%20want%20to%20see%20each%20result.%20Go%20lazy%20for%20big%20files%2C%20many%20files%2C%20and%20a%20query%20you%20will%20run%20again.%0A%0A%20%20%20%20The%20glob%20reads%20many%20files%20as%20one%20table%2C%20but%20only%20if%20they%20share%20one%20layout.%20The%20price%20files%20changed%20theirs%20in%20January%202025.%20Here%20is%20what%20polars%20says%20when%20one%20scan%20crosses%20that%20change%3B%20read%20its%20first%20line%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Twelve%20months%20of%202024%20in%20one%20scan%2C%20then%20a%20scan%20that%20crosses%20into%202025%2C%20whose%20files%20have%20another%20layout%0A%20%20%20%20_months_2024%20%3D%20pl.scan_csv(PATHS.prices%20%2F%20%22farmgate_2024-*.csv%22).collect()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20pl.scan_csv(PATHS.prices%20%2F%20%22farmgate_202%5B45%5D-*.csv%22).collect()%0A%20%20%20%20%20%20%20%20_crossing%20%3D%20%22no%20error%22%0A%20%20%20%20except%20pl.exceptions.SchemaError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_crossing%20%3D%20f%22%7Btype(_error).__name__%7D%3A%20%7B_error%7D%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**2024%20alone%3A**%20%7B_months_2024.height%7D%20rows%20from%20twelve%20files%2C%20columns%20%7B'%2C%20'.join(f'%60%7Bc%7D%60'%20for%20c%20in%20_months_2024.columns)%7D.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**2024%20and%202025%20in%20one%20scan%3A**%5Cn%5Cn%60%60%60text%5Cn%7B_crossing.replace(PATHS.data.as_posix()%20%2B%20'%2F'%2C%20'').replace(str(PATHS.data)%20%2B%20os.sep%2C%20'')%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20first%20file%20of%202025%20has%20a%20column%2C%20%60Month%60%2C%20that%20the%202024%20files%20do%20not.%20Read%20the%20hint%20with%20care%20before%20you%20follow%20it%3A%20with%20%60extra_columns%3D'ignore'%60%2C%20polars%20next%20stops%20with%20*ColumnNotFoundError%3A%20did%20not%20find%20column%20province*%2C%20and%20adding%20the%20%60missing_columns%3D'insert'%60%20that%20message%20suggests%20gives%20672%20rows%2C%20of%20which%20the%20336%20from%202025%20are%20empty%20in%20every%20column.%20The%20error%20was%20right%3A%20these%20files%20need%20reading%20each%20in%20its%20own%20layout.%20Stretch%20exercise%20%5BS2.1%5D(%2F%3Ffile%3D82_stretch_polars.py)%20does%20that%2C%20and%20stacks%20the%20two%20into%20one%20table.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.4.%20A%20lazy%20query%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20query%20written%20lazily%20runs%20as%20it%20is%20on%20a%20file%20a%20thousand%20times%20bigger%2C%20because%20polars%20reads%20only%20what%20it%20needs.%20Returning%20the%20query%2C%20not%20its%20result%2C%20also%20lets%20whoever%20calls%20your%20function%20add%20steps%20before%20anything%20runs.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20raw%20plots%20file%20(a%20path%20or%20bytes%3B%20%60pl.scan_csv%60%20takes%20either)%20%E2%86%92%20a%20**LazyFrame**%2C%20not%20run%2C%20that%20gives%20one%20row%20for%20each%20value%20of%20%60irrigated%60%2C%20among%20the%20**rice**%20plots%20of%20the%20file%20with%20its%20repeated%20rows%20dropped%3A%0A%0A%20%20%20%20%7C%20irrigated%20%7C%20plots%20%7C%20production_kg%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20No%20%7C%20how%20many%20rice%20plots%20%7C%20their%20total%20harvest%2C%20in%20kg%20%7C%0A%20%20%20%20%7C%20Yes%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60pl.scan_csv(source%2C%20schema_overrides%3D...)%60%2C%20then%20the%20steps%20of%20Exercise%206.1%20that%20this%20needs%20(production%20as%20whole%20numbers%2C%20%60unique()%60)%2C%20%60filter%60%2C%20%60group_by(%22irrigated%22).agg(plots%3Dpl.len()%2C%20production_kg%3D...)%60%2C%20%60sort(%22irrigated%22)%60.%20Do%20**not**%20end%20with%20%60.collect()%60%3A%20the%20check%20collects%20it%2C%20after%20making%20sure%20it%20is%20still%20a%20query%20that%20starts%20by%20scanning%20the%20file.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20rice_by_irrigation(source)%20-%3E%20pl.LazyFrame%3A%0A%20%20%20%20%22%22%22A%20lazy%20query%3A%20rice%20plots%20and%20their%20total%20harvest%2C%20irrigated%20and%20not%2C%20from%20the%20raw%20plots%20file.%22%22%22%0A%20%20%20%20%23%20Exercise%206.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20pl.scan_csv%20the%20source%2C%20with%20production_kg%20as%20text%0A%20%20%20%20%23%20Step%202%3A%20production_kg%20as%20whole%20numbers%3B%20drop%20repeated%20rows%0A%20%20%20%20%23%20Step%203%3A%20filter%20the%20rice%20plots%0A%20%20%20%20%23%20Step%204%3A%20group_by%20irrigated%3B%20agg%20plots%20and%20production_kg%3B%20sort%20by%20irrigated%0A%20%20%20%20%23%20Step%205%3A%20return%20the%20LazyFrame%2C%20without%20.collect()%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.4%22)%0A%0A%0A%40app.function%0Adef%20lazy_result(query)%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%206.4%20compares%3A%20whether%20the%20answer%20is%20a%20query%20that%20starts%20by%20scanning%20the%20file%2C%20and%0A%20%20%20%20what%20it%20gives%20when%20it%20runs.%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%22starts%20by%20scanning%20the%20file%22%3A%20%22Csv%20SCAN%22%20in%20query.explain()%2C%20%22result%22%3A%20in_order(query.collect()%2C%20%5B%22irrigated%22%5D)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.4%22%2C%20rice_by_irrigation%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%20small%20file%20with%20a%20repeated%20rice%20plot%22%2C%20(plots_csv_fixture(commas%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20file%20with%20no%20thousands%20separators%22%2C%20(plots_csv_fixture(commas%3DFalse)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20plots.csv%22%2C%20(PATHS.survey%20%2F%20%22plots.csv%22%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%22%2C%20summarize%3Dlazy_result%2C%0A%20%20%20%20%20%20%20%20hint%3D%22A%20LazyFrame%20that%20starts%20with%20pl.scan_csv%20(not%20read_csv)%2C%20the%20repeated%20rows%20dropped%2C%20only%20rice.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20query%20on%20the%20survey's%20raw%20plots%20file%2C%20with%20its%20plan.%0A%20%20%20%20show_polars(rice_by_irrigation%2C%20PATHS.survey%20%2F%20%22plots.csv%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%206.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*not%20a%20LazyFrame%20but%20a%20DataFrame*%20%7C%20The%20query%20ends%20with%20%60.collect()%60%2C%20or%20starts%20with%20%60pl.read_csv%60.%20%7C%20%60pl.scan_csv(...)%60%2C%20and%20no%20%60.collect()%60%20%7C%0A%20%20%20%20%7C%20%60starts%20by%20scanning%20the%20file%60%20is%20%60False%60%20%7C%20%60pl.read_csv(...).lazy()%60%3A%20lazy%2C%20but%20only%20after%20reading%20everything.%20%7C%20%60pl.scan_csv(source%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20one%20plot%20too%20many%20in%20the%20first%20file%20%7C%20The%20repeated%20row%20is%20counted.%20%7C%20%60.unique()%60%20%7C%0A%20%20%20%20%7C%20*InvalidOperationError*%20when%20the%20check%20runs%20it%20%7C%20The%20production%20column%20is%20text%20when%20summed%2C%20or%20numbers%20when%20cleaned.%20%7C%20%60schema_overrides%60%2C%20then%20%60.str.replace_all%60%20and%20%60.cast%60%2C%20as%20in%20Exercise%206.1%20%7C%0A%20%20%20%20%7C%20Corn%20in%20the%20numbers%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%20A%20lazy%20query's%20errors%20appear%20only%20when%20it%20runs%2C%20at%20%60.collect()%60%3A%20the%20line%20the%20error%20points%20to%20is%20the%20%60collect%60%2C%20and%20the%20mistake%20is%20in%20one%20of%20the%20steps%20before%20it.%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%20Moving%20between%20pandas%20and%20polars%0A%0A%20%20%20%20Most%20of%20the%20time%20you%20will%20not%20choose%20one%20library%20for%20good.%20A%20table%20arrives%20in%20pandas%20from%20a%20function%20that%20only%20knows%20pandas%3B%20a%20heavy%20step%20runs%20faster%20in%20polars%3B%20the%20result%20goes%20back%20to%20a%20pandas%20chart.%20Two%20calls%20move%20a%20table%20across%2C%20through%20**pyarrow**%2C%20the%20Arrow%20library%20installed%20with%20the%20course%3A%0A%0A%20%20%20%20-%20%60pl.from_pandas(pandas_table)%60%20gives%20a%20polars%20DataFrame%3B%0A%20%20%20%20-%20%60polars_table.to_pandas()%60%20gives%20a%20pandas%20DataFrame.%0A%0A%20%20%20%20The%20values%20cross%20intact%3B%20some%20of%20the%20pandas%20ideas%20that%20have%20no%20polars%20equivalent%20do%20not.%20Here%20are%20the%20clean%20households%20through%20a%20round%20trip%2C%20pandas%20to%20polars%20and%20back%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(households_pd)%3A%0A%20%20%20%20_there%20%3D%20pl.from_pandas(households_pd)%0A%20%20%20%20_back%20%3D%20_there.to_pandas()%0A%20%20%20%20_trip%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22pandas%20(course.households())%22%3A%20households_pd.dtypes.astype(str)%2C%0A%20%20%20%20%20%20%20%20%22polars%20(pl.from_pandas)%22%3A%20%5Bstr(t)%20for%20t%20in%20_there.dtypes%5D%2C%0A%20%20%20%20%20%20%20%20%22pandas%20again%20(.to_pandas())%22%3A%20_back.dtypes.astype(str)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20_trip.index.name%20%3D%20%22column%22%0A%20%20%20%20_trip%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%60head_sex%60%20and%20%60head_age%60%20went%20in%20as%20pandas'%20nullable%20whole%20numbers%20(%60Int64%60%2C%20with%20%60%3CNA%3E%60%20for%20%22not%20stated%22)%20and%20came%20back%20as%20decimals%20(%60float64%60%2C%20with%20%60NaN%60)%3A%20%60.to_pandas()%60%20turns%20a%20column%20of%20whole%20numbers%20with%20gaps%20into%20decimals%2C%20as%20pandas%20did%20before%20nullable%20types%20existed.%20What%20else%20changes%20on%20the%20way%3A%0A%0A%20%20%20%20%7C%20what%20%7C%20pandas%20%E2%86%92%20polars%20%7C%20polars%20%E2%86%92%20pandas%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20index%20%7C%20dropped%3B%20%60pl.from_pandas(t%2C%20include_index%3DTrue)%60%20keeps%20it%20as%20a%20column%20%7C%20a%20new%20index%200%2C%201%2C%202%E2%80%A6%3A%20%60set_index(...)%60%20again%20if%20you%20need%20one%20%7C%0A%20%20%20%20%7C%20whole%20numbers%20with%20gaps%20%7C%20%60Int64%60%20with%20%60%3CNA%3E%60%20becomes%20%60Int64%60%20with%20%60null%60%20%7C%20%60Int64%60%20with%20%60null%60%20becomes%20%60float64%60%20with%20%60NaN%60%3B%20%60.astype(%22Int64%22)%60%20brings%20back%20the%20nullable%20type%20%7C%0A%20%20%20%20%7C%20text%20%7C%20%60str%60%20becomes%20%60String%60%20%7C%20%60String%60%20becomes%20%60str%60%20%7C%0A%20%20%20%20%7C%20dates%20and%20times%20%7C%20%60datetime64%60%20becomes%20%60Datetime%60%20%7C%20%60Datetime%60%20becomes%20%60datetime64%60%20%7C%0A%20%20%20%20%7C%20a%20month%20(%60Period%60%2C%20from%20module%205)%20%7C%20no%20polars%20type%3A%20it%20arrives%20as%20an%20%22extension%22%20column%20holding%20the%20number%20of%20months%20since%20January%201970%20(%60624%60%20for%20January%202022)%20%7C%20%E2%80%94%20%7C%0A%0A%20%20%20%20For%20a%20month%2C%20convert%20before%20crossing%3A%20%60prices%5B%22month%22%5D.dt.to_timestamp()%60%20gives%20the%20first%20day%20of%20each%20month%20as%20a%20date%20and%20time%2C%20which%20polars%20reads%20as%20%60Datetime%60%2C%20and%20%60.cast(pl.Date)%60%20keeps%20only%20the%20date.%0A%0A%20%20%20%20%60.to_pandas(use_pyarrow_extension_array%3DTrue)%60%20keeps%20polars'%20types%20exactly%2C%20as%20pandas'%20Arrow-backed%20types%20(%60int64%5Bpyarrow%5D%60)%2C%20but%20most%20pandas%20code%20expects%20the%20ordinary%20ones%2C%20so%20the%20exercises%20use%20%60.astype(...)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.5.%20Back%20to%20pandas%2C%20with%20the%20types%20it%20had%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20household%20table%20went%20through%20polars%20for%20a%20fast%20step%2C%20and%20must%20now%20go%20back%20to%20a%20pandas%20function%20from%20%5Bmodule%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%20that%20looks%20households%20up%20by%20%60hh_id%60%20in%20the%20index%2C%20and%20treats%20a%20not-stated%20age%20as%20missing%2C%20not%20as%20a%20decimal.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20households%20in%20polars%20(with%20%60hh_id%60%2C%20and%20%60head_sex%60%20and%20%60head_age%60%20as%20whole%20numbers%20that%20may%20be%20%60null%60)%20%E2%86%92%20a%20pandas%20DataFrame%2C%20**indexed%20by%20%60hh_id%60**%2C%20with%20%60head_sex%60%20and%20%60head_age%60%20as%20pandas'%20nullable%20whole%20numbers%20(%60%22Int64%22%60).%20Every%20other%20column%20as%20%60.to_pandas()%60%20gives%20it.%0A%0A%20%20%20%20%7C%20hh_id%20(index)%20%7C%20province%20%7C%20%E2%80%A6%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%20HH-0001%20%7C%20Ilocos%20Norte%20%7C%20%E2%80%A6%20%7C%201%20%7C%2058%20%7C%0A%20%20%20%20%7C%20HH-0012%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%60%3CNA%3E%60%20%7C%2047%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60.to_pandas()%60%2C%20%60.set_index(%22hh_id%22)%60%2C%20%60.astype(%7B%22head_sex%22%3A%20%22Int64%22%2C%20%22head_age%22%3A%20%22Int64%22%7D)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20households_for_pandas(households%3A%20pl.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20as%20a%20pandas%20table%20indexed%20by%20hh_id%2C%20with%20the%20not-stated%20codes%20still%20missing%20whole%20numbers.%22%22%22%0A%20%20%20%20%23%20Exercise%206.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20turn%20the%20polars%20table%20into%20a%20pandas%20one%0A%20%20%20%20%23%20Step%202%3A%20make%20hh_id%20the%20index%0A%20%20%20%20%23%20Step%203%3A%20head_sex%20and%20head_age%20as%20%22Int64%22%2C%20then%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.5%22)%0A%0A%0A%40app.function%0Adef%20polars_households_fixture(gaps%3A%20bool)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20households%20in%20polars%2C%20for%20the%20check%20of%20Exercise%206.5%3A%20with%20not-stated%20sex%20and%20age%20(null)%2C%20or%20with%20none.%22%22%22%0A%20%20%20%20return%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%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%20%20%20%20%22province%22%3A%20%5B%22Ilocos%20Norte%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22head_sex%22%3A%20%5B1%2C%20None%2C%202%5D%20if%20gaps%20else%20%5B1%2C%202%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22head_age%22%3A%20%5B58%2C%2047%2C%20None%5D%20if%20gaps%20else%20%5B58%2C%2047%2C%2039%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B279.77%2C%20349.61%2C%20252.51%5D%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20schema_overrides%3D%7B%22head_sex%22%3A%20pl.Int64%2C%20%22head_age%22%3A%20pl.Int64%7D%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.5%22%2C%20households_for_pandas%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20households%2C%20one%20with%20sex%20not%20stated%20and%20one%20with%20age%20not%20stated%22%2C%20(polars_households_fixture(gaps%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20households%20with%20nothing%20missing%22%2C%20(polars_households_fixture(gaps%3DFalse)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20clean%20households%2C%20through%20polars%22%2C%20(pl.from_pandas(course.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%20index%2C%20the%20values%20and%20every%20column's%20type%3A%20head_sex%20and%20head_age%20must%20be%20%5C%22Int64%5C%22%2C%20with%20a%20capital%20I%2C%20even%20when%20nothing%20is%20missing.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(households_pl)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20households%2C%20back%20in%20pandas.%0A%20%20%20%20show_polars(households_for_pandas%2C%20households_pl)%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%206.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%60head_age%60%20is%20%60float64%60%20%7C%20%60.to_pandas()%60%20alone%3A%20a%20whole-number%20column%20with%20gaps%20comes%20back%20as%20decimals.%20%7C%20%60.astype(%7B%22head_sex%22%3A%20%22Int64%22%2C%20%22head_age%22%3A%20%22Int64%22%7D)%60%20%7C%0A%20%20%20%20%7C%20only%20the%20second%20table%20fails%20%7C%20%60astype%60%20only%20where%20there%20are%20gaps%3B%20with%20none%2C%20the%20type%20is%20%60int64%60.%20%7C%20%60%22Int64%22%60%20in%20every%20case%20%7C%0A%20%20%20%20%7C%20%60int64%60%2C%20not%20%60Int64%60%20%7C%20A%20lowercase%20%60%22int64%22%60%3A%20the%20ordinary%20type%2C%20which%20cannot%20hold%20%60%3CNA%3E%60.%20%7C%20%60%22Int64%22%60%2C%20with%20a%20capital%20I%20%7C%0A%20%20%20%20%7C%20%60int64%5Bpyarrow%5D%60%20%7C%20%60use_pyarrow_extension_array%3DTrue%60%3A%20Arrow%20types%2C%20not%20pandas'%20own.%20%7C%20plain%20%60.to_pandas()%60%20%7C%0A%20%20%20%20%7C%20the%20index%20is%200%2C%201%2C%202%E2%80%A6%20and%20%60hh_id%60%20is%20a%20column%20%7C%20The%20index%20is%20not%20set.%20%7C%20%60.set_index(%22hh_id%22)%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'DataFrame'%20object%20has%20no%20attribute%20'set_index'*%20%7C%20%60set_index%60%20called%20on%20the%20polars%20table%2C%20before%20%60.to_pandas()%60.%20%7C%20the%20order%20of%20the%20steps%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.6.%20The%20prices%2C%20into%20polars%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%60course.prices()%60%20gives%20every%20farmgate%20price%20in%20one%20pandas%20table%2C%20with%20%60month%60%20as%20a%20monthly%20period%2C%20as%20%5Bmodule%205%5D(%2F%3Ffile%3D40_cleaning_and_time.py)%20left%20it.%20polars%20has%20no%20period%20type%2C%20so%20the%20month%20must%20become%20something%20polars%20understands%20before%20it%20crosses%3A%20a%20date%2C%20the%20first%20day%20of%20the%20month.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20prices%20in%20pandas%20(%60month%60%20a%20monthly%20%60Period%60%2C%20then%20%60province%60%2C%20%60commodity%60%2C%20%60price%60)%20%E2%86%92%20the%20same%20rows%20in%20polars%2C%20with%20%60month%60%20as%20%60Date%60%20and%20the%20other%20three%20columns%20as%20they%20were.%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%20Ilocos%20Norte%20%7C%20Palay%20%7C%2018.2%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60prices%5B%22month%22%5D.dt.to_timestamp()%60%20(in%20pandas%3A%20the%20first%20moment%20of%20each%20month)%2C%20%60prices.assign(...)%60%2C%20%60pl.from_pandas(...)%60%2C%20then%20%60.with_columns(pl.col(%22month%22).cast(pl.Date))%60%20(in%20polars%3A%20the%20date%2C%20without%20a%20time%20of%20day).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20prices_for_polars(prices%3A%20pd.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20prices%20in%20polars%2C%20with%20each%20month%20as%20the%20date%20of%20its%20first%20day.%22%22%22%0A%20%20%20%20%23%20Exercise%206.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20in%20pandas%2C%20replace%20month%20by%20its%20first%20moment%20(.dt.to_timestamp())%0A%20%20%20%20%23%20Step%202%3A%20pl.from_pandas%20the%20table%0A%20%20%20%20%23%20Step%203%3A%20in%20polars%2C%20cast%20month%20to%20pl.Date%2C%20then%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.6%22)%0A%0A%0A%40app.function%0Adef%20pandas_prices_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20prices%20in%20pandas%2C%20with%20monthly%20periods%2C%20across%20a%20year's%20end%2C%20for%20the%20check%20of%20Exercise%206.6.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22month%22%3A%20pd.PeriodIndex(%5B%222023-08%22%2C%20%222023-10%22%2C%20%222023-12%22%2C%20%222024-01%22%5D%2C%20freq%3D%22M%22)%2C%0A%20%20%20%20%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%20%20%20%20%22commodity%22%3A%20%5B%22Palay%22%2C%20%22Tomato%22%2C%20%22Palay%22%2C%20%22Yellow%20corn%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22price%22%3A%20%5B19.4%2C%2031.25%2C%2020.1%2C%2015.8%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.6%22%2C%20prices_for_polars%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%20prices%20from%20August%202023%20to%20January%202024%22%2C%20(pandas_prices_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20price%20of%20the%20course%2C%20from%20course.prices()%22%2C%20(course.prices()%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%20values%20and%20types%3A%20month%20must%20be%20polars'%20Date%20(not%20Datetime%2C%20not%20text)%2C%20the%20first%20day%20of%20each%20month.%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%20every%20farmgate%20price%2C%20in%20polars.%0A%20%20%20%20show_polars(prices_for_polars%2C%20course.prices())%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%206.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60month%60%20shows%20numbers%20such%20as%20624%2C%20its%20type%20%60Extension(...)%60%20%7C%20The%20period%20crossed%20as%20it%20is.%20%7C%20%60.dt.to_timestamp()%60%20before%20%60pl.from_pandas%60%20%7C%0A%20%20%20%20%7C%20%60month%60%20is%20%60Datetime(time_unit%3D'us'%2C%20...)%60%20%7C%20The%20time%20of%20day%20is%20still%20there.%20%7C%20%60.cast(pl.Date)%60%20%7C%0A%20%20%20%20%7C%20%60month%60%20is%20%60String%60%20%7C%20%60astype(str)%60%3A%20text%2C%20not%20a%20date.%20Sorting%20and%20date%20ranges%20then%20work%20on%20letters.%20%7C%20%60.dt.to_timestamp()%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'DataFrame'%20object%20has%20no%20attribute%20'assign'*%20%7C%20%60assign%60%20called%20after%20%60pl.from_pandas%60%3A%20polars%20says%20%60with_columns%60.%20%7C%20convert%20the%20month%20in%20pandas%2C%20before%20crossing%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20Can%20only%20use%20.dt%20accessor%20with%20datetimelike%20values*%20%7C%20%60.dt.to_timestamp()%60%20on%20a%20column%20that%20is%20not%20a%20period.%20%7C%20%60prices%5B%22month%22%5D%60%2C%20from%20%60course.prices()%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%20When%20each%20fits%0A%0A%20%20%20%20Neither%20library%20is%20better%20at%20everything.%20An%20honest%20comparison%2C%20for%20the%20kind%20of%20work%20this%20course%20does%3A%0A%0A%20%20%20%20%7C%20question%20%7C%20pandas%20%7C%20polars%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20size%20of%20data%20%7C%20comfortable%20up%20to%20a%20few%20million%20rows%20that%20fit%20in%20memory%20%7C%20faster%20on%20large%20tables%3B%20a%20lazy%20query%20reads%20only%20what%20it%20needs%2C%20and%20%60collect(engine%3D%22streaming%22)%60%20handles%20data%20bigger%20than%20memory%20%7C%0A%20%20%20%20%7C%20speed%20on%20this%20survey%20%7C%20instant%20%7C%20instant%3A%20on%20two%20thousand%20rows%20the%20difference%20is%20a%20few%20thousandths%20of%20a%20second%2C%20either%20way%20%7C%0A%20%20%20%20%7C%20other%20libraries%20%7C%20almost%20every%20Python%20data%20library%20takes%20pandas%3A%20statsmodels%2C%20scikit-learn%2C%20geopandas%2C%20and%20seaborn%20%7C%20a%20growing%20number%20take%20polars%20directly%3B%20the%20rest%20after%20%60.to_pandas()%60%20%7C%0A%20%20%20%20%7C%20time%20series%20%7C%20a%20date%20index%3A%20%60resample%60%2C%20%60rolling%60%2C%20%60shift%60%2C%20as%20in%20module%205%20%7C%20dates%20as%20columns%3A%20%60group_by_dynamic%60%2C%20%60rolling%60%2C%20%60shift%60%20%7C%0A%20%20%20%20%7C%20strictness%20%7C%20forgiving%3A%20a%20column%20with%20one%20stray%20value%20is%20read%20as%20text%2C%20and%20whole%20numbers%20with%20a%20gap%20become%20decimals%2C%20without%20a%20word%20%7C%20strict%3A%20a%20cast%20that%20fails%20is%20an%20error%20that%20names%20the%20values%2C%20never%20a%20quiet%20change%20of%20type%20%7C%0A%20%20%20%20%7C%20how%20code%20reads%20%7C%20many%20ways%20to%20write%20the%20same%20step%20%7C%20expressions%3A%20one%20way%2C%20read%20top%20to%20bottom%20%7C%0A%20%20%20%20%7C%20help%20when%20stuck%20%7C%20most%20tutorials%2C%20answers%20and%20colleagues'%20notebooks%20%7C%20younger%2C%20with%20good%20official%20documentation%20at%20docs.pola.rs%20%7C%0A%0A%20%20%20%20**The%20course's%20charts%20take%20either%2C%20with%20one%20step%20for%20seaborn.**%20Altair%20draws%20a%20polars%20DataFrame%20directly%2C%20as%20the%20chart%20below%20does.%20matplotlib%20takes%20polars%20columns%3A%20%60ax.bar(table%5B%22crop%22%5D%2C%20table%5B%22area_ha%22%5D)%60.%20seaborn%200.13%20refuses%20a%20polars%20table%20(*TypeError%3A%20Data%20source%20must%20be%20a%20DataFrame%20or%20Mapping*)%2C%20so%20give%20it%20%60table.to_pandas()%60.%0A%0A%20%20%20%20A%20rule%20to%20start%20from%3A%20use%20pandas%20when%20the%20data%20fits%20comfortably%20and%20the%20next%20step%20is%20a%20library%20that%20expects%20pandas%3B%20use%20polars%20when%20files%20are%20big%20or%20many%2C%20when%20the%20work%20is%20a%20query%20you%20will%20run%20again%2C%20or%20when%20speed%20matters.%20Moving%20between%20them%20is%20one%20line%2C%20so%20one%20notebook%20can%20use%20both.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(households_pl%2C%20plots_pl)%3A%0A%20%20%20%20_result%20%3D%20attempt(weighted_yield_by_province%2C%20plots_pl%2C%20households_pl)%0A%20%20%20%20mo.stop(_result%20is%20NOT_YET%2C%20waiting_for(%22Exercise%206.3%22))%0A%20%20%20%20%23%20Altair%20takes%20the%20polars%20DataFrame%20as%20it%20is%3A%20no%20.to_pandas()%0A%20%20%20%20alt.Chart(_result%2C%20title%3D%22Rice%20yield%20by%20province%2C%20weighted%20by%20household%20(t%2Fha)%22).mark_bar().encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22weighted_t_ha%3AQ%22%2C%20title%3D%22tonnes%20per%20hectare%22)%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22province%3AN%22%2C%20sort%3D%22-x%22%2C%20title%3DNone)%2C%0A%20%20%20%20%20%20%20%20tooltip%3D%5B%22province%22%2C%20%22plots%22%2C%20alt.Tooltip(%22weighted_t_ha%3AQ%22%2C%20format%3D%22.2f%22)%5D%2C%0A%20%20%20%20).properties(width%3D%22container%22%2C%20height%3D220)%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%23%20Measure%20it%20yourself%0A%0A%20%20%20%20Claims%20about%20speed%20are%20easy%20to%20make.%20This%20cell%20measures%20them%20on%20your%20computer%3A%20it%20copies%20the%20survey's%20plots%20until%20the%20table%20has%20the%20size%20you%20pick%2C%20then%20times%20the%20same%20group-and-sum%20in%20both%20libraries.%20The%20numbers%20change%20from%20one%20computer%20and%20one%20run%20to%20the%20next%3B%20the%20pattern%20is%20what%20to%20look%20at.%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%20copies%20%3D%20mo.ui.dropdown(options%3D%7B%221%20copy%20(2%2C160%20rows)%22%3A%201%2C%20%22100%20copies%20(216%2C000%20rows)%22%3A%20100%2C%20%22500%20copies%20(1%2C080%2C000%20rows)%22%3A%20500%7D%2C%20value%3D%22100%20copies%20(216%2C000%20rows)%22%2C%20label%3D%22Table%20size%22)%0A%20%20%20%20copies%0A%20%20%20%20return%20(copies%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(copies%2C%20plots_pd)%3A%0A%20%20%20%20_big_pd%20%3D%20pd.concat(%5Bplots_pd%5D%20*%20copies.value%2C%20ignore_index%3DTrue)%0A%20%20%20%20_big_pl%20%3D%20pl.from_pandas(_big_pd)%0A%0A%20%20%20%20def%20_best_of_three(step)%3A%0A%20%20%20%20%20%20%20%20times%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(3)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20%20%20%20%20step()%0A%20%20%20%20%20%20%20%20%20%20%20%20times.append(time.perf_counter()%20-%20start)%0A%20%20%20%20%20%20%20%20return%20min(times)%20*%201000%20%20%23%20milliseconds%2C%20the%20fastest%20of%20three%20runs%0A%0A%20%20%20%20_pandas_ms%20%3D%20_best_of_three(lambda%3A%20_big_pd.groupby(%22crop%22)%5B%22production_kg%22%5D.sum())%0A%20%20%20%20_polars_ms%20%3D%20_best_of_three(lambda%3A%20_big_pl.group_by(%22crop%22).agg(pl.col(%22production_kg%22).sum()))%0A%20%20%20%20_faster%20%3D%20%22polars%22%20if%20_polars_ms%20%3C%20_pandas_ms%20else%20%22pandas%22%0A%20%20%20%20_ratio%20%3D%20max(_pandas_ms%2C%20_polars_ms)%20%2F%20max(min(_pandas_ms%2C%20_polars_ms)%2C%201e-6)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20%7Blen(_big_pd)%3A%2C%7D%20rows%2C%20total%20harvest%20by%20crop%20%7C%20time%20(milliseconds%2C%20fastest%20of%20three%20runs)%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20pandas%3A%20%60groupby(%22crop%22)%5B%22production_kg%22%5D.sum()%60%20%7C%20%7B_pandas_ms%3A%2C.2f%7D%20%7C%0A%20%20%20%20%7C%20polars%3A%20%60group_by(%22crop%22).agg(pl.col(%22production_kg%22).sum())%60%20%7C%20%7B_polars_ms%3A%2C.2f%7D%20%7C%0A%0A%20%20%20%20Here%20**%7B_faster%7D**%20was%20about%20%7B_ratio%3A%2C.1f%7D%20times%20as%20fast.%20On%20the%20survey's%20own%20size%2C%20both%20finish%20before%20you%20could%20notice%3B%20the%20gap%20opens%20as%20the%20table%20grows%2C%20and%20with%20it%20the%20case%20for%20polars.%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(%226.2%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
1174b724742b8affa7e8f1f7225fa6ee