Skip to content

Commit 739169b

Browse files
committed
fix: Clean up function, changelog
1 parent 7f2cb6c commit 739169b

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

changelog_entry.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
1+
- bump: patch
2+
changes:
3+
added:
4+
- Conversion of county FIPS codes to county enum items
5+
- Helper function to convert string county names to enum keys
6+
changed:
7+
- Modified county variable to depend on FIPS input, then on ZIP code
8+
- Modified county variable to use helper function for conversion from county names to enum keys
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pandas as pd
2+
import numpy as np
3+
from policyengine_us.variables.household.demographic.geographic.county.county_enum import (
4+
County,
5+
)
6+
7+
8+
def map_county_string_to_enum(
9+
county_name: "pd.Series[str]", state_code: "pd.Series[str]"
10+
) -> "pd.Series[int]":
11+
"""Helper function to map county name and state code to County enum value."""
12+
county_key = county_name.apply(
13+
lambda name: name.replace(" ", "_")
14+
.replace("-", "_")
15+
.replace(".", "")
16+
.replace("'", "_")
17+
.strip()
18+
.upper()
19+
)
20+
county_state = county_key.str.cat(state_code, sep="_")
21+
county_names = pd.Series(
22+
np.arange(len(County._member_names_)),
23+
index=County._member_names_,
24+
)
25+
return county_names[county_state]
Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from policyengine_us.model_api import *
2+
from policyengine_us.tools.geography.county_helpers import (
3+
map_county_string_to_enum,
4+
)
25
from policyengine_us.variables.household.demographic.geographic.county.county_enum import (
36
County,
47
)
@@ -21,36 +24,11 @@ def formula(household, period, parameters):
2124
county_fips_codes = COUNTY_FIPS_DATASET.set_index("county_fips")
2225
county_name = county_fips_codes.loc[county_fips, "county_name"]
2326
state_code = county_fips_codes.loc[county_fips, "state"]
24-
county_key = county_name.apply(
25-
lambda name: name.replace(" ", "_")
26-
.replace("-", "_")
27-
.replace(".", "")
28-
.replace("'", "_")
29-
.strip()
30-
.upper()
31-
)
32-
county_state = county_key.str.cat(state_code, sep="_")
33-
county_names = pd.Series(
34-
np.arange(len(County._member_names_)),
35-
index=County._member_names_,
36-
)
37-
return county_names[county_state]
27+
return map_county_string_to_enum(county_name, state_code)
3828

3929
# Attempt to look up from ZIP code
4030
zip_code = household("zip_code", period).astype(int)
4131
zip_codes = ZIP_CODE_DATASET.set_index("zip_code")
4232
county_name = zip_codes.county[zip_code]
4333
state_code = zip_codes.state[zip_code]
44-
county_key = county_name.apply(
45-
lambda name: name.replace(" ", "_")
46-
.replace("-", "_")
47-
.replace(".", "")
48-
.replace("'", "_")
49-
.strip()
50-
.upper()
51-
)
52-
county_state = county_key.str.cat(state_code, sep="_")
53-
county_names = pd.Series(
54-
np.arange(len(County._member_names_)), index=County._member_names_
55-
)
56-
return county_names[county_state]
34+
return map_county_string_to_enum(county_name, state_code)

0 commit comments

Comments
 (0)