%22%22%22Tutorial%204%3A%20put%20a%20district's%20population%20series%20on%20today's%20borders%2C%20then%20label%20it%20with%20EU%20region%20codes.%0A%0ARun%20with%3A%20uv%20run%20marimo%20edit%20tutorials%2F04_todays_borders.py%20(needs%20network%20access%20and%20a%20free%20GENESIS-Online%20account).%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Tutorial%204%3A%20districts%20on%20today's%20borders%0A%0A%20%20%20%20**Goal%3A**%20put%20a%20district's%20population%20series%20on%20today's%20borders%2C%20then%20label%20it%20with%20EU%20region%20codes.%0A%0A%20%20%20%20**Time%3A**%20about%2015%20minutes.%20The%20first%20run%20downloads%20about%2020%20MB%20of%20reference%20files.%20**You%20need%3A**%20basic%0A%20%20%20%20Python%2C%20a%20network%20connection%20and%20the%20GENESIS-Online%20token%20from%20tutorial%203.%0A%0A%20%20%20%20**Setup%3A**%20clone%20the%20repository%20and%20run%20%60uv%20sync%20--all-extras%60%20(see%20the%20README)%2C%20put%20your%20token%20in%0A%20%20%20%20%60.env%60%20as%20in%20tutorial%203%2C%20then%20%60uv%20run%20marimo%20edit%20tutorials%2F04_todays_borders.py%60.%0A%0A%20%20%20%20Germany%20is%20split%20into%20about%20400%20districts%2C%20*Kreise*%20(singular%20*Kreis*).%20Their%20borders%20change%3A%20on%0A%20%20%20%201%20November%202016%20the%20districts%20of%20G%C3%B6ttingen%20and%20Osterode%20am%20Harz%2C%20in%20the%20*Land*%20(federal%20state)%20of%0A%20%20%20%20Lower%20Saxony%2C%20merged%20into%20one%20new%20district%2C%20also%20named%20G%C3%B6ttingen.%20A%20population%20series%20per%20district%20then%0A%20%20%20%20breaks%20in%20two.%20This%20tutorial%20joins%20it%20back%20together.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20json%0A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20from%20mloda.user%20import%20DataAccessCollection%2C%20Feature%2C%20mloda%0A%20%20%20%20from%20mloda_plugin_govdata.feature_groups.destatis%20import%20(%0A%20%20%20%20%20%20%20%20GENESIS_ONLINE%2C%0A%20%20%20%20%20%20%20%20DestatisCredentials%2C%0A%20%20%20%20%20%20%20%20DestatisLocator%2C%0A%20%20%20%20%20%20%20%20DestatisReader%2C%0A%20%20%20%20%20%20%20%20MissingCredentialsError%2C%0A%20%20%20%20)%0A%20%20%20%20from%20mloda_plugin_govdata.feature_groups.harmonization%20import%20AgsToNutsFeature%2C%20KreisRebaseFeature%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20AgsToNutsFeature%2C%0A%20%20%20%20%20%20%20%20DataAccessCollection%2C%0A%20%20%20%20%20%20%20%20DestatisCredentials%2C%0A%20%20%20%20%20%20%20%20DestatisLocator%2C%0A%20%20%20%20%20%20%20%20DestatisReader%2C%0A%20%20%20%20%20%20%20%20Feature%2C%0A%20%20%20%20%20%20%20%20GENESIS_ONLINE%2C%0A%20%20%20%20%20%20%20%20KreisRebaseFeature%2C%0A%20%20%20%20%20%20%20%20MissingCredentialsError%2C%0A%20%20%20%20%20%20%20%20alt%2C%0A%20%20%20%20%20%20%20%20json%2C%0A%20%20%20%20%20%20%20%20mloda%2C%0A%20%20%20%20%20%20%20%20pd%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DataAccessCollection%2C%0A%20%20%20%20DestatisCredentials%2C%0A%20%20%20%20DestatisLocator%2C%0A%20%20%20%20GENESIS_ONLINE%2C%0A%20%20%20%20MissingCredentialsError%2C%0A%20%20%20%20mo%2C%0A)%3A%0A%20%20%20%20%23%20Every%20later%20cell%20takes%20a%20name%20from%20this%20cell%2C%20so%20mo.stop%20holds%20them%20all%20back.%0A%20%20%20%20_why%20%3D%20None%0A%20%20%20%20_collection%20%3D%20None%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20_credentials%20%3D%20DestatisCredentials.from_env(GENESIS_ONLINE)%0A%20%20%20%20%20%20%20%20if%20_credentials%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_why%20%3D%20%22No%20token%20found%3A%20%60GENESIS_TOKEN%60%20is%20not%20set%20(nor%20%60GENESIS_USER%60%20and%20%60GENESIS_PASSWORD%60).%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_collection%20%3D%20DataAccessCollection(credentials%3D%7B%22genesis%22%3A%20_credentials.to_credential()%7D)%0A%20%20%20%20except%20MissingCredentialsError%3A%20%20%23%20a%20user%20name%20without%20a%20password%2C%20or%20the%20reverse%0A%20%20%20%20%20%20%20%20_why%20%3D%20%22Set%20both%20%60GENESIS_USER%60%20and%20%60GENESIS_PASSWORD%60%2C%20or%20only%20%60GENESIS_TOKEN%60.%22%0A%20%20%20%20except%20ValueError%3A%20%20%23%20a%20character%20that%20cannot%20be%20part%20of%20a%20token%2C%20e.g.%20a%20copied%20quote%20mark%0A%20%20%20%20%20%20%20%20_why%20%3D%20%22The%20token%20contains%20a%20character%20that%20does%20not%20belong%20in%20it.%20Copy%20it%20again.%22%0A%20%20%20%20mo.stop(%0A%20%20%20%20%20%20%20%20_why%20is%20not%20None%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**Stopped.**%20%7B_why%7D%20See%20*Get%20a%20token*%20in%20tutorial%203%2C%20then%20start%20marimo%20again.%22)%2C%0A%20%20%20%20)%0A%20%20%20%20collection%20%3D%20_collection%0A%20%20%20%20TABLE%20%3D%20%2212411-0015%22%0A%20%20%20%20goettingen%20%3D%20DestatisLocator(%0A%20%20%20%20%20%20%20%20TABLE%2C%20regionalvariable%3D%22KREISE%22%2C%20regionalkey%3D%5B%2203152%22%2C%20%2203156%22%2C%20%2203159%22%5D%2C%20startyear%3D2011%0A%20%20%20%20)%0A%20%20%20%20return%20TABLE%2C%20collection%2C%20goettingen%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%201%3A%20read%20the%20three%20districts%0A%0A%20%20%20%20Destatis%20table%20%6012411-0015%60%20counts%20the%20population%20of%20every%20district%20on%2031%20December%20of%20each%20year.%20Each%0A%20%20%20%20district%20has%20an%20official%20five-digit%20key%2C%20the%20first%20digits%20of%20its%20*AGS*%20(the%20official%20region%20key)%3A%0A%20%20%20%20%6003152%60%20(G%C3%B6ttingen%20before%20the%20merger)%2C%20%6003156%60%20(Osterode%20am%20Harz)%20and%20%6003159%60%20(the%20new%20G%C3%B6ttingen).%20%60regionalvariable%3D%22KREISE%22%60%20and%0A%20%20%20%20%60regionalkey%60%20pick%20these%20three.%20The%20years%20start%20in%202011%20and%20run%20to%20the%20latest%20one%20GENESIS%20has.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DestatisReader%2C%20Feature%2C%20collection%2C%20goettingen%2C%20mloda)%3A%0A%20%20%20%20_columns%20%3D%20%5B%22time%22%2C%20%221_variable_attribute_code%22%2C%20%221_variable_attribute_label%22%2C%20%22value%22%2C%20%22value_marker%22%5D%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(_name%2C%20options%3D%7BDestatisReader%3A%20goettingen%7D)%20for%20_name%20in%20_columns%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20column_ordering%3D%22request_order%22%2C%0A%20%20%20%20%20%20%20%20data_access_collection%3Dcollection%2C%0A%20%20%20%20)%0A%20%20%20%20raw%20%3D%20_result%5B0%5D.to_pandas()%0A%20%20%20%20raw.pivot(index%3D%22time%22%2C%20columns%3D%221_variable_attribute_label%22%2C%20values%3D%22value%22)%20%20%23%20one%20column%20per%20district%0A%20%20%20%20return%20(raw%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20raw)%3A%0A%20%20%20%20_merged%20%3D%20raw%5B(raw%5B%221_variable_attribute_code%22%5D%20%3D%3D%20%2203159%22)%20%26%20(raw%5B%22value_marker%22%5D%20!%3D%20%22-%22)%5D%0A%20%20%20%20_first%20%3D%20_merged%5B%22time%22%5D.min()%0A%20%20%20%20_jump%20%3D%20_merged%5B_merged%5B%22time%22%5D%20%3D%3D%20_first%5D%5B%22value%22%5D.iloc%5B0%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20Three%20columns.%20The%20two%20old%20districts%20have%20numbers%20until%20%7B_first%20-%201%7D%2C%20the%20new%20one%20from%20%7B_first%7D.%20Every%0A%20%20%20%20other%20cell%20is%20a%200%20with%20the%20sign%20%60-%60%20in%20%60value_marker%60%3A%20the%20district%20did%20not%20exist%20on%20that%2031%20December%0A%20%20%20%20(tutorial%203%20met%20the%20same%20sign).%20Drawn%20per%20district%2C%20the%20new%20G%C3%B6ttingen%20would%20seem%20to%20grow%20from%200%20to%0A%20%20%20%20%7B_jump%3A%2C.0f%7D%20people%20in%20one%20year.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%202%3A%20put%20every%20year%20on%20today's%20borders%0A%0A%20%20%20%20The%20*BBSR*%2C%20a%20federal%20research%20office%20for%20regional%20planning%2C%20publishes%20conversion%20keys%20between%20the%0A%20%20%20%20borders%20of%20consecutive%20years%3A%20which%20share%20of%20each%20old%20district's%20population%20went%20to%20which%20new%0A%20%20%20%20district.%20Each%20pair%20of%20years%20is%20one%20sheet%20of%20its%20key%20file.%20Sheet%202015-2016%20converts%20the%20borders%20of%2031%0A%20%20%20%20December%202015%20into%20those%20of%2031%20December%202016.%20These%20districts%20kept%20their%20borders%20from%202011%20to%202015%2C%20so%0A%20%20%20%20the%20same%20sheet%20serves%20those%20years%20too%3B%20the%20plugin%20checks%20this%20and%20raises%20an%20error%20if%20a%20district%20changed%0A%20%20%20%20earlier.%0A%0A%20%20%20%20%60KreisRebaseFeature.fetch_reference()%60%20downloads%20the%20key%20file%20on%20the%20first%20run%20and%20reads%20it%20from%20disk%0A%20%20%20%20afterwards.%20It%20checks%20the%20file%20against%20a%20fixed%20fingerprint%2C%20so%20a%20changed%20file%20raises%20an%20error%20instead%0A%20%20%20%20of%20silently%20changing%20your%20numbers.%0A%0A%20%20%20%20Then%20ask%20for%20%60value__rebased%60%3A%20the%20feature%20%60value%60%2C%20re-based.%20You%20do%20not%20ask%20for%20%60value%60%20itself%3B%20the%0A%20%20%20%20re-basing%20asks%20the%20reader%20for%20it.%20The%20result%20has%20several%20columns%2C%20one%20per%20part%2C%20named%0A%20%20%20%20%60value__rebased~%3Cpart%3E%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DestatisReader%2C%0A%20%20%20%20Feature%2C%0A%20%20%20%20KreisRebaseFeature%2C%0A%20%20%20%20collection%2C%0A%20%20%20%20goettingen%2C%0A%20%20%20%20mloda%2C%0A)%3A%0A%20%20%20%20KreisRebaseFeature.fetch_reference()%0A%20%20%20%20rebase_options%20%3D%20%7BDestatisReader%3A%20goettingen%2C%20%22rebase_from_year%22%3A%202015%2C%20%22rebase_to_year%22%3A%202016%7D%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(%22value__rebased%22%2C%20options%3Drebase_options)%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20data_access_collection%3Dcollection%2C%0A%20%20%20%20)%0A%20%20%20%20rebased%20%3D%20_result%5B0%5D.to_pandas().sort_values(%22value__rebased~year%22%2C%20ignore_index%3DTrue)%0A%20%20%20%20rebased%5B%5Bf%22value__rebased~%7B_part%7D%22%20for%20_part%20in%20(%22key%22%2C%20%22year%22%2C%20%22value%22%2C%20%22flag%22%2C%20%22sources%22)%5D%5D%0A%20%20%20%20return%20rebase_options%2C%20rebased%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20raw%2C%20rebased)%3A%0A%20%20%20%20_flag%20%3D%20rebased%5B%22value__rebased~flag%22%5D%0A%20%20%20%20_years%20%3D%20rebased%5B%22value__rebased~year%22%5D%0A%20%20%20%20_old%20%3D%20raw%5B(raw%5B%22time%22%5D%20%3D%3D%202015)%20%26%20raw%5B%221_variable_attribute_code%22%5D.isin(%5B%2203152%22%2C%20%2203156%22%5D)%5D%0A%20%20%20%20_old%20%3D%20_old.sort_values(%221_variable_attribute_code%22)%5B%22value%22%5D%0A%20%20%20%20_rebased_2015%20%3D%20rebased%5B_years%20%3D%3D%202015%5D%5B%22value__rebased~value%22%5D.iloc%5B0%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20One%20row%20per%20year%2C%20all%20on%20the%20key%20%6003159%60.%20%60~flag%60%20says%20how%20each%20value%20came%20about%3A%20%60rebased%60%20for%0A%20%20%20%20%7B_years%5B_flag%20%3D%3D%20%22rebased%22%5D.min()%7D%20to%20%7B_years%5B_flag%20%3D%3D%20%22rebased%22%5D.max()%7D%2C%20built%20from%20the%20districts%20in%0A%20%20%20%20%60~sources%60%3B%20%60observed%60%20from%20%7B_years%5B_flag%20%3D%3D%20%22observed%22%5D.min()%7D%2C%20counted%20by%20GENESIS%20for%20the%20new%20district%0A%20%20%20%20itself.%20Both%20old%20districts%20went%20in%20whole%2C%20so%20a%20re-based%20year%20is%20their%20sum%3A%20for%202015%2C%0A%20%20%20%20%7B_old.iloc%5B0%5D%3A%2C.0f%7D%20%2B%20%7B_old.iloc%5B1%5D%3A%2C.0f%7D%20%3D%20%7B_old.sum()%3A%2C.0f%7D%2C%20and%20the%20re-based%202015%20value%20is%0A%20%20%20%20%7B_rebased_2015%3A%2C.0f%7D.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%203%3A%20add%20EU%20region%20codes%0A%0A%20%20%20%20Outside%20Germany%2C%20regions%20go%20by%20*NUTS*%20codes%2C%20Eurostat's%20classification%20of%20the%20EU's%20regions.%20A%20code%0A%20%20%20%20grows%20by%20one%20character%20per%20level%3A%20%60DE%60%20is%20Germany%2C%20NUTS-1%20the%20L%C3%A4nder%2C%20NUTS-2%20regions%20within%20them%2C%0A%20%20%20%20NUTS-3%20the%20districts.%20Eurostat%20revises%20the%20codes%20every%20few%20years%2C%20so%20a%20code%20belongs%20to%20a%20version%2C%20here%0A%20%20%20%202024.%0A%0A%20%20%20%20%60AgsToNutsFeature.fetch_reference()%60%20downloads%20Eurostat's%20table%20from%20municipalities%20to%20NUTS%20codes%0A%20%20%20%20(about%2020%20MB%2C%20once)%20and%20a%20Destatis%20file%20of%20the%202016%20border%20changes%2C%20which%20leads%20an%20old%20key%20such%20as%0A%20%20%20%20%6003152%60%20to%20its%20successor.%20Then%20chain%20the%20mapping%20onto%20the%20re-based%20keys.%20Read%0A%20%20%20%20%60value__rebased~key__nuts2024%60%20from%20left%20to%20right%3A%20re-base%20%60value%60%2C%20take%20the%20%60key%60%20part%2C%20map%20it%20to%20NUTS%0A%20%20%20%202024.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(AgsToNutsFeature%2C%20Feature%2C%20collection%2C%20mloda%2C%20rebase_options)%3A%0A%20%20%20%20AgsToNutsFeature.fetch_reference()%0A%20%20%20%20_name%20%3D%20%22value__rebased~key__nuts2024%22%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(_name%2C%20options%3Drebase_options)%5D%2C%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%20data_access_collection%3Dcollection%0A%20%20%20%20)%0A%20%20%20%20nuts%20%3D%20_result%5B0%5D.to_pandas()%0A%20%20%20%20nuts%5B%5Bf%22%7B_name%7D~%7B_part%7D%22%20for%20_part%20in%20(%22key%22%2C%20%22nuts1%22%2C%20%22nuts2%22%2C%20%22nuts3%22%2C%20%22version%22)%5D%5D.drop_duplicates()%0A%20%20%20%20return%20(nuts%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20nuts)%3A%0A%20%20%20%20_codes%20%3D%20nuts.iloc%5B0%5D%0A%20%20%20%20_prefix%20%3D%20%22value__rebased~key__nuts2024~%22%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7Blen(nuts)%7D%20rows%2C%20one%20per%20year%2C%20all%20with%20the%20same%20codes%2C%20so%20the%20table%20shows%20one.%20%60%7B_codes%5B_prefix%20%2B%20%22nuts1%22%5D%7D%60%0A%20%20%20%20is%20Lower%20Saxony%2C%20%60%7B_codes%5B_prefix%20%2B%20%22nuts2%22%5D%7D%60%20the%20region%20of%20Braunschweig%20(named%20after%20a%20former%0A%20%20%20%20administrative%20region)%20and%20%60%7B_codes%5B_prefix%20%2B%20%22nuts3%22%5D%7D%60%20the%20district%20of%20G%C3%B6ttingen.%20With%20these%20codes%2C%20the%20series%20can%20be%20compared%20with%20regions%20in%20other%20EU%20countries.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Pitfall%3A%20new%20borders%2C%20not%20new%20counting%0A%0A%20%20%20%20Re-basing%20fixes%20the%20borders.%20It%20cannot%20fix%20a%20change%20in%20how%20people%20were%20counted.%20About%20every%20ten%20years%0A%20%20%20%20a%20census%20(*Zensus*)%20counts%20the%20population%20afresh%2C%20and%20the%20yearly%20numbers%20after%20it%20start%20from%20the%20new%0A%20%20%20%20count.%20The%20%60~provenance%60%20column%20records%20which%20census%20changes%20fall%20inside%20your%20years%2C%20as%0A%20%20%20%20%60census_breaks%60.%20Nothing%20in%20%60~value%60%20marks%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(json%2C%20mo%2C%20rebased)%3A%0A%20%20%20%20_breaks%20%3D%20json.loads(rebased%5B%22value__rebased~provenance%22%5D.iloc%5B0%5D)%5B%22census_breaks%22%5D%0A%20%20%20%20_value%20%3D%20rebased.set_index(%22value__rebased~year%22)%5B%22value__rebased~value%22%5D%0A%20%20%20%20_lines%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22-%20%7B_year%7D%3A%20%7B_value%5B_year%20-%201%5D%3A%2C.0f%7D%20people%20in%20%7B_year%20-%201%7D%2C%20%7B_value%5B_year%5D%3A%2C.0f%7D%20in%20%7B_year%7D.%20Part%20of%20%22%0A%20%20%20%20%20%20%20%20%22this%20step%20comes%20from%20the%20new%20census%2C%20not%20from%20people%20moving.%22%0A%20%20%20%20%20%20%20%20for%20_year%20in%20_breaks%0A%20%20%20%20%20%20%20%20if%20_year%20-%201%20in%20_value.index%0A%20%20%20%20%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%60census_breaks%60%3A%20%7B_breaks%7D%0A%0A%20%20%20%20%7Bchr(10).join(_lines)%20or%20%22No%20census%20change%20inside%20these%20years.%22%7D%0A%0A%20%20%20%20Compare%20years%20on%20both%20sides%20of%20a%20break%20with%20care%2C%20and%20say%20so%20when%20you%20publish%20such%20a%20comparison.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Try%20it%3A%20another%20merger%0A%0A%20%20%20%20On%201%20July%202021%20the%20town%20of%20Eisenach%20(%6016056%60)%2C%20until%20then%20a%20district%20of%20its%20own%20(*kreisfreie%20Stadt*)%2C%0A%20%20%20%20joined%20the%20Wartburgkreis%20(%6016063%60)%20in%20the%20Land%20of%20Thuringia.%20This%20time%20the%20bigger%20district%20kept%20its%20key%2C%0A%20%20%20%20so%20its%20raw%20line%20jumps%20instead%20of%20starting%20from%20zero.%20Pick%20a%20merger%3B%20the%20chart%20shows%20the%20raw%20districts%0A%20%20%20%20(without%20the%20%60-%60%20rows)%20and%20the%20re-based%20series.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DestatisLocator%2C%20TABLE%2C%20goettingen%2C%20mo)%3A%0A%20%20%20%20%23%20Each%20choice%3A%20the%20districts%20to%20read%20and%20the%20first%20year%20of%20its%20key%20sheet.%0A%20%20%20%20_eisenach%20%3D%20DestatisLocator(TABLE%2C%20regionalvariable%3D%22KREISE%22%2C%20regionalkey%3D%5B%2216056%22%2C%20%2216063%22%5D%2C%20startyear%3D2019)%0A%20%20%20%20merger%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22G%C3%B6ttingen%20and%20Osterode%20am%20Harz%20(2016)%22%3A%20(goettingen%2C%202015)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Eisenach%20into%20the%20Wartburgkreis%20(2021)%22%3A%20(_eisenach%2C%202020)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20value%3D%22G%C3%B6ttingen%20and%20Osterode%20am%20Harz%20(2016)%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Merger%22%2C%0A%20%20%20%20)%0A%20%20%20%20merger%0A%20%20%20%20return%20(merger%2C)%0A%0A%0A%40app.cell%0Adef%20_(DestatisReader%2C%20Feature%2C%20alt%2C%20collection%2C%20merger%2C%20mloda%2C%20pd%2C%20rebased)%3A%0A%20%20%20%20_after_step_2%20%3D%20rebased%20%20%23%20Step%202%20fetched%20the%20key%20file%20this%20cell%20needs%0A%20%20%20%20_locator%2C%20_from_year%20%3D%20merger.value%0A%20%20%20%20_options%20%3D%20%7BDestatisReader%3A%20_locator%7D%0A%20%20%20%20_columns%20%3D%20%5B%22time%22%2C%20%221_variable_attribute_label%22%2C%20%22value%22%2C%20%22value_marker%22%5D%0A%20%20%20%20_raw%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(_name%2C%20options%3D_options)%20for%20_name%20in%20_columns%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20column_ordering%3D%22request_order%22%2C%0A%20%20%20%20%20%20%20%20data_access_collection%3Dcollection%2C%0A%20%20%20%20)%5B0%5D.to_pandas()%0A%20%20%20%20_years%20%3D%20%7B%22rebase_from_year%22%3A%20_from_year%2C%20%22rebase_to_year%22%3A%20_from_year%20%2B%201%7D%0A%20%20%20%20_rebased%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5BFeature(%22value__rebased%22%2C%20options%3D%7B**_options%2C%20**_years%7D)%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20data_access_collection%3Dcollection%2C%0A%20%20%20%20)%5B0%5D.to_pandas()%0A%20%20%20%20%23%20Formatting%20only%3A%20one%20long%20table%2C%20one%20line%20per%20series.%0A%20%20%20%20_lines%20%3D%20pd.concat(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20_raw%5B_raw%5B%22value_marker%22%5D%20!%3D%20%22-%22%5D.rename(columns%3D%7B%221_variable_attribute_label%22%3A%20%22series%22%7D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22time%22%3A%20_rebased%5B%22value__rebased~year%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20_rebased%5B%22value__rebased~value%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22series%22%3A%20%22re-based%20onto%20today's%20borders%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20alt.Chart(_lines%2C%20title%3D%22Population%20on%2031%20December%22).mark_line(point%3DTrue).encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22time%3AQ%22%2C%20title%3D%22Year%22%2C%20axis%3Dalt.Axis(format%3D%22d%22))%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22value%3AQ%22%2C%20title%3D%22People%22)%2C%0A%20%20%20%20%20%20%20%20color%3Dalt.Color(%22series%3AN%22%2C%20title%3DNone%2C%20legend%3Dalt.Legend(orient%3D%22bottom%22%2C%20columns%3D1))%2C%0A%20%20%20%20%20%20%20%20tooltip%3D%5B%22series%22%2C%20%22time%22%2C%20%22value%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Where%20the%20data%20came%20from%0A%0A%20%20%20%20Every%20published%20number%20needs%20its%20sources.%20Steps%201%20to%203%20read%20four%20files%3B%20the%20copies%20on%20your%20disk%20record%0A%20%20%20%20when%20you%20downloaded%20each%20one.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20AgsToNutsFeature%2C%0A%20%20%20%20DestatisReader%2C%0A%20%20%20%20KreisRebaseFeature%2C%0A%20%20%20%20TABLE%2C%0A%20%20%20%20goettingen%2C%0A%20%20%20%20json%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20nuts%2C%0A%20%20%20%20rebased%2C%0A)%3A%0A%20%20%20%20def%20_retrieved(record)%3A%0A%20%20%20%20%20%20%20%20%23%20A%20missing%20record%20or%20date%20means%20the%20cache%20was%20cleared%20since%20the%20step%20ran.%0A%20%20%20%20%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Brecord.retrieved_at%3A%25Y-%25m-%25d%7D%22%20if%20record%20and%20record.retrieved_at%20else%20%22not%20in%20the%20local%20cache%20any%20more%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20_sheet%20%3D%20json.loads(rebased%5B%22value__rebased~provenance%22%5D.iloc%5B0%5D)%5B%22sheet%22%5D%20%20%23%20the%20key%20sheet%20Step%202%20used%0A%20%20%20%20_version%20%3D%20nuts%5B%22value__rebased~key__nuts2024~version%22%5D.iloc%5B0%5D%0A%20%20%20%20(_keys%2C)%20%3D%20KreisRebaseFeature.reference_sources()%0A%20%20%20%20_codes%2C%20_changes%20%3D%20AgsToNutsFeature.reference_sources()%20%20%23%20the%20NUTS%20table%20and%20the%202016%20change%20file%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20-%20**Population%3A**%20Statistisches%20Bundesamt%20(Destatis)%2C%20GENESIS-Online%2C%20table%20%7BTABLE%7D%2C%0A%20%20%20%20%20%20https%3A%2F%2Fgenesis.destatis.de%2Fdatenbank%2Fonline%2Fstatistic%2F12411%2Ftable%2F%7BTABLE%7D%2C%20retrieved%0A%20%20%20%20%20%20%7B_retrieved(DestatisReader.retrieval(goettingen))%7D.%20License%20%60%7BDestatisReader.LICENSE%7D%60.%0A%20%20%20%20-%20**Conversion%20keys%3A**%20%7B_keys.source.name%7D%2C%20sheet%20%7B_sheet%7D%2C%20%7B_keys.source.url%7D%2C%20retrieved%0A%20%20%20%20%20%20%7B_retrieved(_keys)%7D.%20Attribution%3A%20%22%7B_keys.source.attribution%7D%22.%20License%20%60%7B_keys.source.license%7D%60.%0A%20%20%20%20-%20**NUTS%20%7B_version%7D%20codes%3A**%20%7B_codes.source.name%7D%2C%20%7B_codes.source.url%7D%2C%20retrieved%20%7B_retrieved(_codes)%7D.%0A%20%20%20%20%20%20Attribution%3A%20%22%7B_codes.source.attribution%7D%22.%20License%20%60%7B_codes.source.license%7D%60.%0A%20%20%20%20-%20**Border%20changes%20of%202016%3A**%20Destatis%20municipality%20register%20(*Gemeindeverzeichnis*)%2C%20change%20file%202016%2C%0A%20%20%20%20%20%20%7B_changes.source.url%7D%2C%20retrieved%20%7B_retrieved(_changes)%7D.%20Attribution%3A%20%22%7B_changes.source.attribution%7D%22.%0A%20%20%20%20%20%20License%20%60%7B_changes.source.license%7D%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20Both%20licenses%20(%5BData%20License%20Germany%2C%20attribution%2C%20version%202.0%5D(https%3A%2F%2Fwww.govdata.de%2Fdl-de%2Fby-2-0)%0A%20%20%20%20and%20%5BCC%20BY%204.0%5D(https%3A%2F%2Fcreativecommons.org%2Flicenses%2Fby%2F4.0%2F))%20let%20you%20reuse%20the%20data%20if%20you%20name%20the%0A%20%20%20%20source%2C%20link%20the%20license%20and%20say%20what%20you%20changed.%20Here%3A%20each%20reference%20date%20became%20its%20year%20and%20each%0A%20%20%20%20%60-%60%20became%200%20(the%20plugin%20does%20this%20for%20every%20Destatis%20table)%2C%20the%20years%20before%202016%20were%20summed%20onto%20the%0A%20%20%20%202016%20borders%20with%20the%20BBSR%20population%20shares%2C%20and%20the%20district%20keys%20were%20mapped%20to%20NUTS%202024%20codes.%0A%0A%20%20%20%20%23%23%20Next%0A%0A%20%20%20%20The%20next%20tutorial%20runs%20a%20recipe%3A%20a%20file%20that%20holds%20the%20features%2C%20options%20and%20sources%20of%20a%20result%2C%20so%0A%20%20%20%20someone%20else%20can%20run%20it%20again.%20%5BHarmonization%5D(https%3A%2F%2Fgithub.com%2Fmloda-ai%2Fmloda-plugin-govdata%2Fblob%2Fmain%2Fdocs%2Fharmonization.md)%0A%20%20%20%20lists%20every%20option%20of%20%60value__rebased%60%20and%20the%20NUTS%20mapping%2C%20and%20every%20part%20they%20return.%0A%20%20%20%20%22%22%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
9a6ad00df23a81e932c9058f14402074