Skip to main content

Census data for arbitrary geographies

Project description

This Python library extends the Sunlight Foundation’s Census API Wrapper to allow querying Census tracts, block groups, and blocks by Census place, as well as by arbitrary geographies.

Setup

Get the library and its dependencies using pip:

pip install census_area

Usage

from census_area import Census

c = Census("MY_API_KEY")
old_homes = c.acs5.state_place_tract(('NAME', 'B25034_010E'), 17, 14000)

The call above will return the name of the census tract and the number of homes that were built before 1939 for every tract in the City of Chicago. 17 is the FIPS code for Illinois and 14000 is the FIPS code for Chicago.

By default, this method will return a list of dictionaries, where each dictionary represents the data for one tract.

With the return_geometry argument, you can have the method return a geojson-like dictionary. Each tract is a feature, and the census variables about the tract appear in the feature’s property attributes.

old_homes_geojson = c.acs5.state_place_tract(('NAME', 'B25034_010E'), 17, 14000), return_geometry=True)

There are similar methods for block groups

old_home_block_groups = c.acs5.state_place_blockgroup(('NAME', 'B25034_010E'), 17, 14000))

And blocks. Note that block level geographies are only available for the short-form data from the Decennial Census

owner_occupied = c.sf1.state_place_block(('NAME', 'H016F0002'), 17, 14000)

The tract and blockgroup methods are also available for the Decennial Census.

owner_occupied_blockgroup = c.sf1.state_place_tract(('NAME', 'H016F0002'), 17, 14000)
owner_occupied_tract = c.sf1.state_place_blockgroup(('NAME', 'H016F0002'), 17, 14000)

old_homes = c.sf1.state_place_tract('NAME', 'H034010'), 17, 14000)
old_homes = c.sf1.state_place_blockgroup('NAME', 'H034010'), 17, 14000)

In addition to these convenient methods, there are three lower level ways to get census tracts, blocks, and groups for arbitrary geometries.

import json

with open('my_shape.geojson') as infile:
    my_shape_geojson = json.load(infile)
features = []
old_homes = c.acs5.geo_tract(('NAME', 'B25034_010E'), my_shape_geojson['geometry'])
for tract_geojson, tract_data, tract_proportion in old_homes:
     tract_geojson['properties'].update(tract_data)
     features.append(tract)

my_shape_with_new_data_geojson = {'type': "FeatureCollection", 'features': features}

The method takes in the census variables you want and a geojson geometry, and returns a generator of the tract shapes, as geojson features, and the variables for that tract. Additionally, the generator returns a “tract proportion”; this is the proportion of the area of the tract that falls within your target shape.

Similar methods are provided for block groups and blocks, for the ACS 5-year and Decennial Census.

c.acs5.geo_blockgroup(('NAME', 'B25034_010E'), my_shape_geojson['geometry'])

c.sf1.geo_block(('NAME', 'H016F0002'), my_shape_geojson['geometry'])
c.sf1.geo_blockgroup(('NAME', 'H016F0002'), my_shape_geojson['geometry'])
c.sf1.geo_tract(('NAME', 'H016F0002'), my_shape_geojson['geometry'])

c.sf1.state_place_tract('NAME', 'H034010'), my_shape_geojson['geometry'])
c.sf1.state_place_blockgroup('NAME', 'H034010'), my_shape_geojson['geometry'])

Team

  • Jean Cochrane, DataMade

  • Forest Gregg, DataMade

Errors and bugs

If something is not behaving intuitively, it is a bug and should be reported. Report it here by creating an issue: https://github.com/datamade/census_area/issues

Help us fix the problem as quickly as possible by following Mozilla’s guidelines for reporting bugs.

Patches and pull requests

Your patches are welcome. Here’s our suggested workflow:

  • Fork the project.

  • Make your feature addition or bug fix.

  • Send us a pull request with a description of your work. Bonus points for topic branches!

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_area-0.4.3.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

census_area-0.4.3-py2.py3-none-any.whl (10.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file census_area-0.4.3.tar.gz.

File metadata

  • Download URL: census_area-0.4.3.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for census_area-0.4.3.tar.gz
Algorithm Hash digest
SHA256 e08804550f3779dc91b82063b59cce7c870eb7ac2448b60c9c1d674fe8a88932
MD5 b20b69ed77b0b6d36ce51e32bf3067e9
BLAKE2b-256 cfe6ebd6e918858998cdb601928613ee56c916e1f5f5e7bfc78dff37acc23be8

See more details on using hashes here.

File details

Details for the file census_area-0.4.3-py2.py3-none-any.whl.

File metadata

  • Download URL: census_area-0.4.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for census_area-0.4.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f62576a392da783c3bb130d8eedd5cc32556bcad95766aaab6d13af79482eff9
MD5 3b2092ebef68f80c5589073ad1c35985
BLAKE2b-256 9d28403ea697c6c35b3ce3051175d7e8b3e4c9060ad7dc5568f42a8e88f0e18e

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