Skip to content

Commit

Permalink
Fix single-tract census queries
Browse files Browse the repository at this point in the history
When doing a single-tract census query you want to specify the tract ID
in the "for" spec and not the "in" spec.
  • Loading branch information
jiffyclub committed Jul 15, 2015
1 parent bbbcb6c commit bcfe7d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
20 changes: 10 additions & 10 deletions synthpop/census_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def block_group_query(self, census_columns, state, county, tract=None,
tract=tract, year=year)

def tract_query(self, census_columns, state, county, tract=None,
year=None, id=None):
if id is None:
id = "*"
year=None):
if tract is None:
tract = "*"
return self._query(census_columns, state, county,
forstr="tract:%s" % id,
tract=tract, year=year)
forstr="tract:%s" % tract,
year=year)

def _query(self, census_columns, state, county, forstr,
tract=None, year=None):
Expand Down Expand Up @@ -165,8 +165,8 @@ def _read_csv(self, loc):
"ST": "object"
})
pums_df = pums_df.rename(columns = {
'PUMA10':'puma10',
'PUMA00':'puma00',
'PUMA10':'puma10',
'PUMA00':'puma00',
'SERIALNO':'serialno'
})
self.pums_cache[loc] = pums_df
Expand All @@ -190,10 +190,10 @@ def download_household_pums(self, state, puma10=None, puma00=None):
if puma00 is not None:
pums00 = self._read_csv(self.pums00_household_base_url % (state, puma00))
pums = pd.concat([pums, pums00], ignore_index = True)

# filter out gq and empty units (non-hh records)
pums = pums[(pums.RT == 'H') & (pums.NP > 0) & (pums.TYPE == 1)]
pums = pums[(pums.RT == 'H') & (pums.NP > 0) & (pums.TYPE == 1)]

return pums

def try_fips_lookup(self, state, county=None):
Expand Down
6 changes: 4 additions & 2 deletions synthpop/test/test_censushelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def test_block_group_and_tract_query(c):
tract="030600")

assert len(df) == 3
assert_series_equal(df["B11001_001E"], df["B08201_001E"])
assert_series_equal(
df["B11001_001E"], df["B08201_001E"], check_names=False)
assert np.all(df.state == "06")
assert np.all(df.county == "075")

Expand All @@ -39,7 +40,8 @@ def test_block_group_and_tract_query(c):

# number of block groups in San Francisco
assert len(df) == 581
assert_series_equal(df["B11001_001E"], df["B08201_001E"])
assert_series_equal(
df["B11001_001E"], df["B08201_001E"], check_names=False)
assert np.all(df.state == "06")
assert np.all(df.county == "075")

Expand Down

0 comments on commit bcfe7d0

Please sign in to comment.