Code Monkey home page Code Monkey logo

publicdata's Introduction

Public Data Application Urls

This library defines a set of Application Urls and Row Generators that allow access to public datasets. For instance, using the Census Reporter URLs, you can define access to a American Community Survey data table on the Census Reporter website. Then, using the associated Row Generator, you can download the data as a sequences of rows.

For instance, this code will return rows from ACS table B17001 for tracts in San Diego County

from publicdata import  parse_app_url

url = parse_app_url("census://CA/140/B17001")

# Or: url = CensusReporterUrl(table='B17001',summarylevel='140',geoid='CA')

for row in url.generator:
    print(row)

The library uses the appurl and rowgenerator python entrypoints, so all libraries that you install that use the entrypoints can be accessed via the parse_app_url and get_generator functions.

Census Urls

There are two URL types for Census access, distinguished by their scheme names, census and censusreporter. The census URLs download files directly from the Census department's FTP servers, and the censusreporter urls get data from the CensusReporter.org apis. Both also have access to geography files, but the census urls have a richer interface for metadata.

The general schemes for the URLS are:

<scheme>:/[/year/release]/<geoid>/<summarylevel>/<table>

Where:

  • <scheme> is either census or censusreporter
  • <year> is the year of the release, which work from 2010 on for census urls and are ignored for censusreporter
  • <release> is 1, 3, or 5, and is applied or ignored as with <year>
  • <geoid> defined the geographic region that contains the returned data, and is either an ACS style geoid, or a state abbreviation, or 'US'
  • <summarylevel> is either a summary level numeric code, or a summary level name
  • <table> is a census table id

The Geoid is an ACS style geoid, such as '04000US06', or a US state abbreviation ( 'AZ' or "CA' ) or 'US'. The geoid defines the area that contains the data to be returned. See the Geographic Codes Lookup web application for more details about geoids and how to find them.

The summary level can also be expressed in text names, as described in the geoid package documentation . The most common of these names, and their numeric codes, are:

'us': 10,
'region': 20,
'division': 30,
'state': 40,
'county': 50,
'cosub': 60,
'place': 160,
'ua': 400,
'tract': 140,
'blockgroup': 150,
'block': 101,
'sdelm': 950,
'sdsec': 960,
'sduni': 970,
'zcta': 860

Usually the geoid is the abbreviation for a state or a county, and then the summary level describes the type of sub region within that state or county. For instance, all of the counties in California is '04000US06/140' or 'CA/county', and all of the places in San Diego county is '05000US06073/160' or '05000US06073/place'.

You can look up the table ideas at Census Reporter or American Fact Finder .

Creating Census Reporter Urls

The censusreporter: url scheme retrieves data from Census Reporter . The structure of the URL is:

censusreporter://<geoid>/<summarylevel>/<table>

Or, use the URL directly:

from publicdata import CensusReporterUrl
from rowgenerators import Downloader
CensusReporterUrl(table='B17001',summarylevel='140',geoid='CA', downloader=Downloader())

So, to get the population of all of the counties in California:

CensusReporterUrl(table='B17001',summarylevel='county',geoid='CA')

or:

from rowgenerators import parse_app_url
parse_app_url('census://CA/county/B17001')

The geoid may either be a census Geoid ( like '04000US06' ) , or a US state abbreviation, or 'US' for indicating all US states.

Creating Census File Urls

The Census file URLS retrieve data directly from the Census FTP server. You can use the same 3-part url scheme as with Census Reporter, in which case you will get the 2016 5-year ACS. Or, you can specify the year and release:

census://<year>/<release>/<geoid>/<summarylevel>/<table>

Such as:

census://2015/5/CA/140/B17001

or:

from publicdata import CensusFileUrl
rom rowgenerators import Downloader CensusFileUrl(year=2016,release=5,table='B17001',summarylevel='140',geoid='CA', downloader=Downloader())

Census Dataframes

For a general overview of the features of the Census URLs, see the ACS Notebook.

The .dataframe function returns a CensusDataFrame which has some special features for working with Census data, including margin-aware summation, ratios, proportions and margin manipulations. See the Special Features of Census Dataframes notebook for details.

Census Geography

You can easily get Geopandas GeoDataFrame objects with either the census: scheme, or the censusgeo: scheme. The geoframe is avilable directly from the Url, or with the rowgenerators package's geoframe() convenience function/

from rowgenerators import parse_app_url
parse_app_url('census://CA/place').geoframe()

# or

from rowgenerators import geoframe
geoframe('censusgeo://CA/place')

Note that the state ( 'CA' in the examples above ) should be 'US' for national regions, such as CSA and CBSA:

geoframe('censusgeo://US/csa')
geoframe('censusgeo://US/cbsa')
# but ...
geoframe('censusgeo://CA/county')

Common Operations

Construct the URL:

url = parse_app_url('census://2015/5/CA/140/B17001')

Iterate rows, header first, then data:

for row in url.generator:
    print(row)

or, to return dict-ish object:

for row in url.generator.iterrows:
    print(row.geometry, row['geometry])

Get a pandas dataframe ( Actually a CensusDataframe):

url.dataframe

Get a Geopandas dataframe:

url.geoframe

Goals

The ADSFree online book has an excellent list of datasets ( and R code for downloading them ) that this library should incorporate. The author also has downloading code for these datasets in the lowdown R package

publicdata's People

Contributors

ericbusboom avatar

Stargazers

Maya Beck avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.