Skip to content

Commit 82b46e7

Browse files
committed
Add support for 2020 DHC data
1 parent e3df949 commit 82b46e7

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ For each dataset, the first year listed is the default.
7474
* acs3dp: `ACS 3 Year Estimates, Data Profiles <https://www.census.gov/data/developers/data-sets/acs-3year.html>`_ (2013, 2012)
7575
* acs1dp: `ACS 1 Year Estimates, Data Profiles <https://www.census.gov/data/developers/data-sets/acs-1year.html>`_ (2023, 2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011)
7676
* acs5st: `ACS 5 Year Estimates, Subject Tables <https://www.census.gov/data/developers/data-sets/acs-5year.html>`_ (2023, 2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009)
77+
* dhc: `Demographic and Housing Characteristics File <https://www.census.gov/data/tables/2023/dec/2020-census-dhc.html>`_ (2020)
7778
* sf1: `Census Summary File 1 <https://www.census.gov/data/datasets/2010/dec/summary-file-1.html>`_ (2010)
7879
* pl: `Redistricting Data Summary File <https://www.census.gov/programs-surveys/decennial-census/about/rdo/summary-files.2020.html>`_ (2020, 2010, 2000)
7980

census/core.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,86 @@ class ACS1DpClient(ACS1Client):
442442

443443
years = (2023, 2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012)
444444

445+
class DHCClient(Client):
446+
447+
default_year = 2020
448+
dataset = 'dhc'
449+
450+
years = (2020)
451+
452+
def _switch_endpoints(self, year):
453+
454+
self.endpoint_url = 'https://api.census.gov/data/%s/dec/%s'
455+
self.definitions_url = 'https://api.census.gov/data/%s/dec/%s/variables.json'
456+
self.definition_url = 'https://api.census.gov/data/%s/dec/%s/variables/%s.json'
457+
self.groups_url = 'https://api.census.gov/data/%s/dec/%s/groups.json'
458+
459+
def tables(self, *args, **kwargs):
460+
self._switch_endpoints(kwargs.get('year', self.default_year))
461+
return super(DHCClient, self).tables(*args, **kwargs)
462+
463+
def get(self, *args, **kwargs):
464+
self._switch_endpoints(kwargs.get('year', self.default_year))
465+
466+
return super(DHCClient, self).get(*args, **kwargs)
467+
468+
@supported_years()
469+
def state_county_subdivision(self, fields, state_fips,
470+
county_fips, subdiv_fips, **kwargs):
471+
return self.get(fields, geo={
472+
'for': 'county subdivision:{}'.format(subdiv_fips),
473+
'in': 'state:{} county:{}'.format(state_fips, county_fips),
474+
}, **kwargs)
475+
476+
@supported_years()
477+
def state_county_tract(self, fields, state_fips,
478+
county_fips, tract, **kwargs):
479+
return self.get(fields, geo={
480+
'for': 'tract:{}'.format(tract),
481+
'in': 'state:{} county:{}'.format(state_fips, county_fips),
482+
}, **kwargs)
483+
484+
@supported_years()
485+
def state_county_blockgroup(self, fields, state_fips, county_fips,
486+
blockgroup, tract=None, **kwargs):
487+
geo = {
488+
'for': 'block group:{}'.format(blockgroup),
489+
'in': 'state:{} county:{}'.format(state_fips, county_fips),
490+
}
491+
if tract:
492+
geo['in'] += ' tract:{}'.format(tract)
493+
return self.get(fields, geo=geo, **kwargs)
494+
495+
@supported_years(2020)
496+
def state_msa(self, fields, state_fips, msa, **kwargs):
497+
return self.get(fields, geo={
498+
'for': ('metropolitan statistical area/' +
499+
'micropolitan statistical area (or part):{}'.format(msa)),
500+
'in': 'state:{}'.format(state_fips),
501+
}, **kwargs)
502+
503+
@supported_years(2020)
504+
def state_csa(self, fields, state_fips, csa, **kwargs):
505+
return self.get(fields, geo={
506+
'for': 'combined statistical area (or part):{}'.format(csa),
507+
'in': 'state:{}'.format(state_fips),
508+
}, **kwargs)
509+
510+
@supported_years(2020)
511+
def state_district_place(self, fields, state_fips,
512+
district, place, **kwargs):
513+
return self.get(fields, geo={
514+
'for': 'place/remainder (or part):{}'.format(place),
515+
'in': 'state:{} congressional district:{}'.format(
516+
state_fips, district),
517+
}, **kwargs)
518+
519+
@supported_years(2020)
520+
def state_zipcode(self, fields, state_fips, zcta, **kwargs):
521+
return self.get(fields, geo={
522+
'for': 'zip code tabulation area (or part):{}'.format(zcta),
523+
'in': 'state:{}'.format(state_fips),
524+
}, **kwargs)
445525

446526
class SF1Client(Client):
447527

@@ -601,6 +681,7 @@ def __init__(self, key, year=None, session=None):
601681
self.acs1dp = ACS1DpClient(key, year, session)
602682
self.sf1 = SF1Client(key, year, session)
603683
self.pl = PLClient(key, year, session)
684+
self.dhc = DHCClient(key, year, session)
604685

605686
@property
606687
def acs(self):

0 commit comments

Comments
 (0)