Skip to main content

A wrapper for the US Census Bureau's API

Project description

https://github.com/datamade/census/workflows/Python%20package/badge.svg

A simple wrapper for the United States Census Bureau’s API.

Provides access to ACS and SF1 data sets.

Install

pip install census

You may also want to install a complementary library, us, which help you figure out the FIPS codes for many geographies. We use it in the examples below.

pip install us

Usage

First, get yourself a Census API key.

from census import Census
from us import states

c = Census("MY_API_KEY")
c.acs5.get(('NAME', 'B25034_010E'),
          {'for': 'state:{}'.format(states.MD.fips)})

The call above will return the name of the geographic area and the number of homes that were built before 1939 for the state of Maryland. Helper methods have been created to simplify common geometry calls:

c.acs5.state(('NAME', 'B25034_010E'), states.MD.fips)

Full details on geometries and the states module can be found below.

The get method is the core data access method on both the ACS and SF1 data sets. The first parameter is either a single string column or a tuple of columns. The second parameter is a geoemtry dict with a for key and on option in key. The for argument accepts a “*” wildcard character or Census.ALL. The wildcard is not valid for the in parameter.

By default, the year for a dataset is the most recent year available. To access earlier data, pass a year parameter to the API call:

c.acs5.state(('NAME', 'B25034_010E'), states.MD.fips, year=2010)

The default year may also be set client-wide:

c = Census("MY_API_KEY", year=2010)

Datasets

For each dataset, the first year listed is the default.

Geographies

The API supports a wide range of geographic regions. The specification of these can be quite complicated so a number of convenience methods are provided. Refer to the Census API documentation for more geographies beyond the convenience methods.

Not all geographies are supported in all years. Calling a convenience method with a year that is not supported will raise census.UnsupportedYearException.

Geographic relationship files are provided on the Census developer site as a tool to help users compare the geographies from the 1990, 2000 and 2010 Censuses. From these files, data users may determine how geographies from one Census relate to those from the prior Census.

ACS5 Geographies

  • state(fields, state_fips)

  • state_county(fields, state_fips, county_fips)

  • state_county_blockgroup(fields, state_fips, county_fips, blockgroup)

  • state_county_subdivision(fields, state_fips, county_fips, subdiv_fips)

  • state_county_tract(fields, state_fips, county_fips, tract)

  • state_place(fields, state_fips, place)

  • state_congressional_district(fields, state_fips, congressional_district)

  • state_legislative_district_upper(fields, state_fips, legislative_district)

  • state_legislative_district_lower(fields, state_fips, legislative_district)

  • us(fields)

  • zipcode(fields, zip5)

ACS1 Geographies

  • state(fields, state_fips)

  • state_congressional_district(fields, state_fips, district)

  • us(fields)

SF1 Geographies

  • state(fields, state_fips)

  • state_county(fields, state_fips, county_fips)

  • state_county_subdivision(fields, state_fips, county_fips, subdiv_fips)

  • state_county_tract(fields, state_fips, county_fips, tract)

  • state_place(fields, state_fips, place)

  • state_congressional_district(fields, state_fips, district)

  • state_msa(fields, state_fips, msa)

  • state_csa(fields, state_fips, csa)

  • state_district_place(fields, state_fips, district, place)

  • state_zipcode(fields, state_fips, zip5)

States

This package previously had a census.states module, but now uses the us package.

>>> from us import states
>>> print states.MD.fips
u'24'

Convert FIPS to state abbreviation using lookup():

>>> print states.lookup('24').abbr
u'MD'

BYOS - Bring Your Own Session

If you’d prefer to use a custom configured requests.Session, you can pass it to the Census constructor:

s = requests.session()
s.headers.update({'User-Agent': 'census-demo/0.0'})

c = Census("MY_API_KEY", session=s)

You can also replace the session used by a specific data set:

c.sf1.session = s

Examples

The geographic name for all census tracts for county 170 in Alaska:

c.sf1.get('NAME', geo={'for': 'tract:*',
                       'in': 'state:{} county:170'.format(states.AK.fips)})

The same call using the state_county_tract convenience method:

c.sf1.state_county_tract('NAME', states.AK.fips, '170', Census.ALL)

Total number of males age 5 - 9 for all states:

c.acs5.get('B01001_004E', {'for': 'state:*'})

The same call using the state convenience method:

c.acs5.state('B01001_004E', Census.ALL)

Don’t know the list of tables in a survey, try this:

c.acs5.tables()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

census-0.8.17.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

census-0.8.17-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file census-0.8.17.tar.gz.

File metadata

  • Download URL: census-0.8.17.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for census-0.8.17.tar.gz
Algorithm Hash digest
SHA256 cbf0cf6a1dde600a34c0b52239d694e144c527dd98f385361a6708f9bf42410c
MD5 a305a1d2245215d6555664e5f58b1d3b
BLAKE2b-256 39287f185f7d8ec0bcefe05068b5db3c2230d7d0d74dc0d60d1c216f3a97c7c8

See more details on using hashes here.

File details

Details for the file census-0.8.17-py3-none-any.whl.

File metadata

  • Download URL: census-0.8.17-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for census-0.8.17-py3-none-any.whl
Algorithm Hash digest
SHA256 15764c9374a4a00b63bd94e8861cae31e0e220248597eedeef037e017c11a299
MD5 5cf2dfc55bd2a1ba267abe069653a830
BLAKE2b-256 415f89cc90cc24bfc0684929d45c5a76283b003b4482e0e5af9bacb9626219e5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page