Skip to main content

Explore and download data from Census APIs

Project description

https://img.shields.io/static/v1.svg?label=documentation&message=latest&color=blueviolet https://github.com/cenpy-devs/cenpy/workflows/.github/workflows/build.yml/badge.svg https://img.shields.io/pypi/dm/cenpy.svg https://zenodo.org/badge/36956226.svg https://img.shields.io/badge/Census%20Slack--lightgrey.svg

An interface to explore and query the US Census API and return Pandas Dataframes. This package is intended for exploratory data analysis and draws inspiration from sqlalchemy-like interfaces and acs.R. With separate APIs for application developers and folks who only want to get their data quickly & painlessly, cenpy should meet the needs of most who aim to get US Census Data from Python.

A few examples are available from our website:

Installation

Cenpy is easiest to install using conda, a commonly-used package manager for scientific python. First, install Anaconda.

Then, cenpy is available on the conda-forge channel:

conda install -c conda-forge cenpy

Alternatively, you can install cenpy via pip, the python package manager, if you have installed geopandas and rtree:

pip install cenpy

For Users

Most of the time, users want a simple and direct interface to the US Census Bureau’s main products: the 2010 Census and the American Community Survey. Fortunately, cenpy provides a direct interface to these products. For instance, the American Community Survey’s most recent 5-year estimates can be accessed using:

import cenpy
acs = cenpy.products.ACS()
acs.from_place('Chicago, IL')

Likewise, the decennial census can be accessed using:

import cenpy
decennial = cenpy.products.Decennial2010()
decennial.from_place('Seattle, WA')

For more information on how the product API works, consult the notebook on the topic.

For Developers

The API reference is available at cenpy-devs.github.io/cenpy. The products are typically what most end-users will want to interact with. If you want more fine-grained access to the USCB APIs, you will likely want to build on top of APIConnection and TigerConnection.

At a high level, the APIConnection object connects to resources exposed on the US Census Bureau’s API at https://api.census.gov/data.json. Its methods and relevant utilities are defined in cenpy.remote. The TigerConnection wraps one map service exposed at http://tigerweb.geo.census.gov/arcgis/rest/services/TIGERweb and is defined in cenpy.tiger. Each TigerConnection is composed of many ESRILayer objects, which wrap an individual geography within the ESRI map service. For instance, an ACS TigerConnection may contain State, County, and Tract ESRILayer objects within their layer attribute.

To use the developer-focused API, you can create an APIConnection using its shortcode:

cxn = cenpy.remote.APIConnection('DECENNIALSF12010')

Check the variables required and geographies supported:

cxn.variables #is a pandas dataframe containing query-able vbls
cxn.geographies #is a pandas dataframe containing query-able geographies

Note that some geographies (like tract) have requirements higher in the hierarchy that you’ll have to specify for the query to work.

The structure of the query function maps to the Census API’s use of get, for, and in. The main arguments for the query function are cols, geo_unit and geo_filter, and map back to those predicates, respectively. If more predicates are required for the search, they can be added as keyword arguments at the end of the query.

The cols argument must be a list of columns to retrieve from the dataset. Then, you must specify the geo_unit and geo_filter, which provide what the unit of aggregation should be and where the units should be. geo_unit must be a string containing the unit of analysis and an identifier. For instance, if you want all counties in Arizona, you specify geo_unit = 'county:*' and geo_filter = {'state':'04'}.

To create a TigerConnection:

cxn = cenpy.tiger.TigerConnection('tigerWMS_ACS2013')

Then, all of the ESRILayer objects are contained in the layer attribute:

cxn.layers

the cxn.query method passes the relevant query down to the chosen layer and returns a geopandas dataframe. The actual query is structured like SQL, and follows the ESRI documentation.

Contributing

To contribute to cenpy:

  1. Use cenpy! Every user is a contributor in kind. If you feel like it, file an issue:

    • to tell us how you use cenpy.

    • to post a code snippit, a jupyter notebook, or whatever you can.

    • to tell us about your blog posts!

    • to ask questions about how you might use census data from Python, and we’ll try to help.

  2. If you’re using cenpy and something goes wrong, file an issue telling us:

    • what you want that is not in cenpy or doesn’t work well in other packages

    • what functionality in cenpy isn’t working how you believe it ought

    • what in the documentation isn’t spelled correctly or is confusing

  3. Fork the cenpy-devs/cenpy github repository, make changes, and send us a pull request

ToDo:

  • A product in cenpy.products for County Business Statistics

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

cenpy-1.0.1.tar.gz (30.8 kB view details)

Uploaded Source

File details

Details for the file cenpy-1.0.1.tar.gz.

File metadata

  • Download URL: cenpy-1.0.1.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.1

File hashes

Hashes for cenpy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 077fd2cc6d69b99da3d85b16a560087e3e455e67a43e934fe5fa1f84c4635f58
MD5 b036a7addf5cfe12335d1781eaa3ac70
BLAKE2b-256 4c9a7718386aa4b7b35d9d3bf7197f4e82aeff49916bde73e23a6f4d5dd6c094

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