Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions _delphi_utils_python/data_proc/geomap/geo_data_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
FIPS_MSA_OUT_FILENAME = "fips_msa_table.csv"
FIPS_HRR_OUT_FILENAME = "fips_hrr_table.csv"
FIPS_ZIP_OUT_FILENAME = "fips_zip_table.csv"
FIPS_HHS_FILENAME = "fips_hhs_table.csv"
FIPS_POPULATION_OUT_FILENAME = "fips_pop.csv"

ZIP_HSA_OUT_FILENAME = "zip_hsa_table.csv"
Expand All @@ -42,6 +43,7 @@
ZIP_MSA_OUT_FILENAME = "zip_msa_table.csv"
ZIP_POPULATION_OUT_FILENAME = "zip_pop.csv"
ZIP_STATE_CODE_OUT_FILENAME = "zip_state_code_table.csv"
ZIP_HHS_FILENAME = "zip_hhs_table.csv"
STATE_OUT_FILENAME = "state_codes_table.csv"
STATE_HHS_OUT_FILENAME = "state_code_hhs_region_number_table.csv"
JHU_FIPS_OUT_FILENAME = "jhu_uid_fips_table.csv"
Expand Down Expand Up @@ -531,6 +533,56 @@ def derive_zip_to_state_code():
)


def derive_fips_hhs_crosswalk():
"""
Builds a crosswalk between FIPS county codes and HHS regions.
"""
if not isfile(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME)):
create_state_hhs_crosswalk()
if not isfile(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME)):
create_fips_population_table()

fips_pop = pd.read_csv(
join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME), dtype={"fips": str, "pop": int}
)
state_hhs = pd.read_csv(
join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME),
dtype={"state_code": str, "hhs_region_number": str},
)

fips_pop["state_code"] = fips_pop["fips"].str[:2]
(
fips_pop.merge(state_hhs, on="state_code", how="left")
.drop(columns=["state_code", "pop"])
.to_csv(join(OUTPUT_DIR, FIPS_HHS_FILENAME), index=False)
)


def derive_zip_hhs_crosswalk():
"""
Builds a crosswalk between zip code and HHS regions.
"""
if not isfile(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME)):
create_state_hhs_crosswalk()
if not isfile(join(OUTPUT_DIR, ZIP_STATE_CODE_OUT_FILENAME)):
derive_zip_to_state_code()

zip_state = pd.read_csv(
join(OUTPUT_DIR, ZIP_STATE_CODE_OUT_FILENAME),
dtype={"zip": str, "pop": int, "state_code": str}
)
state_hhs = pd.read_csv(
join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME),
dtype={"state_code": str, "hhs_region_number": str},
)

(
zip_state.merge(state_hhs, on="state_code", how="left")
.drop(columns=["state_code", "state_id", "state_name"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to reflect changed column names on line 576. Should be the same as line 556.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the zip_state file is the one introducing these columns. The other function uses the fips population file (was just copying an different fips -> * example) which only has the population column to drop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

.to_csv(join(OUTPUT_DIR, ZIP_HHS_FILENAME), index=False)
)


if __name__ == "__main__":
create_fips_zip_crosswalk()
create_zip_hsa_hrr_crosswalk()
Expand All @@ -545,3 +597,5 @@ def derive_zip_to_state_code():
derive_zip_to_state_code()
derive_fips_state_crosswalk()
derive_zip_population_table()
derive_fips_hhs_crosswalk()
derive_zip_hhs_crosswalk()
Loading