Skip to content

Commit 27bb43f

Browse files
authored
Merge pull request #6891 from PolicyEngine/fix-state-group-enum-warnings
Fix StateGroup enum warnings for contiguous US states
2 parents 8ef35eb + a13b6c1 commit 27bb43f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: patch
2+
changes:
3+
fixed:
4+
- StateGroup enum warnings for contiguous US states in state_group variable.

policyengine_us/tests/policy/baseline/household/demographic/geographic/state_group.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,24 @@
1111
state_code: AK
1212
output:
1313
state_group: AK
14+
15+
- name: Contiguous state does not cause empty array indexing error
16+
period: 2024
17+
input:
18+
state_code: TX
19+
output:
20+
state_group: CONTIGUOUS_US
21+
22+
- name: Hawaii
23+
period: 2024
24+
input:
25+
state_code: HI
26+
output:
27+
state_group: HI
28+
29+
- name: Puerto Rico
30+
period: 2024
31+
input:
32+
state_code: PR
33+
output:
34+
state_group: PR

policyengine_us/variables/household/demographic/geographic/state_group.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ class state_group(Variable):
2222
def formula(household, period, parameters):
2323
NON_CONTIGUOUS_STATES = ("AK", "HI", "GU", "PR", "VI")
2424
state_code = household("state_code", period).decode_to_str()
25-
return where(
26-
np.isin(state_code, NON_CONTIGUOUS_STATES),
27-
StateGroup.encode(state_code).decode(),
28-
StateGroup.CONTIGUOUS_US,
29-
)
25+
is_non_contiguous = np.isin(state_code, NON_CONTIGUOUS_STATES)
26+
encoded = np.empty(state_code.shape, dtype=object)
27+
encoded[:] = StateGroup.CONTIGUOUS_US
28+
if is_non_contiguous.any():
29+
encoded[is_non_contiguous] = StateGroup.encode(
30+
state_code[is_non_contiguous]
31+
).decode()
32+
return encoded

0 commit comments

Comments
 (0)