Skip to content

Commit 9ee3589

Browse files
committed
Require raw ACS allocation flags
1 parent f89bc1a commit 9ee3589

2 files changed

Lines changed: 59 additions & 13 deletions

File tree

policyengine_us_data/datasets/acs/acs.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
construct_tax_units_acs,
66
)
77
from policyengine_us_data.storage import STORAGE_FOLDER
8+
from policyengine_us_data.utils.source_quality import require_columns_present
89
from pandas import DataFrame
910
import numpy as np
1011
import pandas as pd
@@ -76,21 +77,24 @@ def add_person_variables(
7677
.loc[person["household_id"]][["RNTP", "TAXAMT"]]
7778
.values
7879
)
79-
for source_flag, output_flag in [
80+
allocation_flag_columns = [
8081
("FRNTP", "rent_is_allocated"),
8182
("FTAXP", "real_estate_taxes_is_allocated"),
82-
]:
83-
if source_flag in household:
84-
person[output_flag] = (
85-
household.set_index("household_id")
86-
.loc[person["household_id"]][source_flag]
87-
.fillna(0)
88-
.astype(int)
89-
.ne(0)
90-
.values
91-
)
92-
else:
93-
person[output_flag] = False
83+
]
84+
require_columns_present(
85+
household.columns,
86+
[source_flag for source_flag, _ in allocation_flag_columns],
87+
source_name="raw Census ACS household table",
88+
)
89+
for source_flag, output_flag in allocation_flag_columns:
90+
person[output_flag] = (
91+
household.set_index("household_id")
92+
.loc[person["household_id"]][source_flag]
93+
.fillna(0)
94+
.astype(int)
95+
.ne(0)
96+
.values
97+
)
9498
acs["is_household_head"] = person.SPORDER == 1
9599
factor = person.SPORDER == 1
96100
person.rent *= factor * 12

tests/unit/datasets/test_acs_tax_unit_construction.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,45 @@ def test_acs_add_id_variables_writes_related_to_head_or_spouse():
235235

236236
assert person_tax_unit_id.tolist() == [1, 1]
237237
assert is_related_to_head_or_spouse.tolist() == [True, False]
238+
239+
240+
def test_acs_add_person_variables_requires_raw_allocation_flags():
241+
person = _acs_person_fixture(household_id=[0])
242+
household = pd.DataFrame(
243+
{
244+
"household_id": [0],
245+
"RNTP": [1_200],
246+
"TAXAMT": [500],
247+
"TEN": [3],
248+
}
249+
)
250+
251+
with h5py.File("memory", mode="w", driver="core", backing_store=False) as acs:
252+
with pytest.raises(KeyError, match="FRNTP"):
253+
ACS.add_person_variables(acs, person, household)
254+
255+
256+
def test_acs_add_person_variables_writes_allocation_flags_for_heads_only():
257+
person = _acs_person_fixture(
258+
household_id=[0, 0],
259+
SPORDER=[1, 2],
260+
AGEP=[40, 10],
261+
)
262+
household = pd.DataFrame(
263+
{
264+
"household_id": [0],
265+
"RNTP": [1_200],
266+
"TAXAMT": [500],
267+
"FRNTP": [1],
268+
"FTAXP": [0],
269+
"TEN": [3],
270+
}
271+
)
272+
273+
with h5py.File("memory", mode="w", driver="core", backing_store=False) as acs:
274+
ACS.add_person_variables(acs, person, household)
275+
rent_is_allocated = acs["rent_is_allocated"][:]
276+
real_estate_taxes_is_allocated = acs["real_estate_taxes_is_allocated"][:]
277+
278+
assert rent_is_allocated.tolist() == [True, False]
279+
assert real_estate_taxes_is_allocated.tolist() == [False, False]

0 commit comments

Comments
 (0)