#!/usr/bin/env python # coding: utf-8 # # Sample workflow: 2000 block group parts to 2010 tracts # # ## Starting from a subset of 2010 Delaware blocks # # For further background information see: # * **Schroeder, J. P**. 2007. *Target-density weighting interpolation and uncertainty evaluation for temporal analysis of census data*. Geographical Analysis 39 (3):311–335. # # #### NHGIS [block crosswalks](https://www.nhgis.org/user-resources/geographic-crosswalks) # In[1]: get_ipython().run_line_magic('load_ext', 'watermark') get_ipython().run_line_magic('watermark', '') # In[2]: import nhgisxwalk import inspect import pandas get_ipython().run_line_magic('load_ext', 'autoreload') get_ipython().run_line_magic('autoreload', '2') get_ipython().run_line_magic('watermark', '-w') get_ipython().run_line_magic('watermark', '-iv') # ### Source and target years for the crosswalk # In[3]: source_year, target_year = "2000", "2010" # ### Source-target building base # In[4]: subset_data_dir = "../testing_data_subsets" base_xwalk_name = "/nhgis_blk%s_blk%s_gj.csv.zip" % (source_year, target_year) base_xwalk_file = subset_data_dir + base_xwalk_name data_types = nhgisxwalk.str_types(["GJOIN%s"%source_year, "GJOIN%s"%target_year]) base_xwalk = pandas.read_csv(base_xwalk_file, index_col=0, dtype=data_types) base_xwalk.head() # ### Convenience code shorthand/lookup # In[5]: print(inspect.getsource(nhgisxwalk.valid_geo_shorthand)) # In[6]: nhgisxwalk.valid_geo_shorthand(shorthand_name=False) # ### Instantiate an `nhgisxwalk.GeoCrossWalk` object # ##### see [nhgisxwalk.GeoCrossWalk](https://github.com/jGaboardi/nhgisxwalk/blob/92b4fe55de0a9c53d0315dcda8ec121faaf20aef/nhgisxwalk/geocrosswalk.py#L19) for full details # In[7]: nhgisxwalk.desc_code_2000_SF1b # In[8]: input_vars = [ nhgisxwalk.desc_code_2000_SF1b["Persons"]["Total"], nhgisxwalk.desc_code_2000_SF1b["Families"]["Total"], nhgisxwalk.desc_code_2000_SF1b["Households"]["Total"], nhgisxwalk.desc_code_2000_SF1b["Housing Units"]["Total"] ] input_vars # In[9]: input_var_tags = ["pop", "fam", "hh", "hu"] # In[10]: subset_state = "10" bgp2000_to_trt2010 = nhgisxwalk.GeoCrossWalk( base_xwalk, source_year=source_year, target_year=target_year, source_geo="bgp", target_geo="trt", base_source_table=subset_data_dir+"/2000_block.csv.zip", input_var=input_vars, weight_var=input_var_tags, stfips=subset_state, keep_base=True, add_geoid=True ) bgp2000_to_trt2010.xwalk # ### Write crosswalk to a `.csv` # In[11]: state_dir = "../../crosswalks/nhgis_bgp2000_trt2010_state/" nhgisxwalk.xwalk_df_to_csv( cls=bgp2000_to_trt2010, path=state_dir ) # ### Read crosswalk from a `.csv` # In[12]: fname = state_dir + bgp2000_to_trt2010.xwalk_name bgp2000_to_trt2010_df = nhgisxwalk.xwalk_df_from_csv(fname) bgp2000_to_trt2010_df # -----------------------------------------------