In [1]:
try:
    import settings
    assert type(settings.CENSUS_KEY) == str or type(settings.CENSUS_KEY) == unicode
except Exception as e:
    print ("error in importing settings to get at settings.CENSUS_KEY", e)

In [2]:
from census import Census
from us import states

c = Census(settings.CENSUS_KEY)

Does the census module need to be updated to accomodate Census Bureau's API Updated with 2013 ACS (1-yr) and New Features? Maybe. I may need to formulate URL by hand.


In [3]:
# 2012 ACS-1year sample call
url = "http://api.census.gov/data/2012/acs1/profile?get=DP02_0001PE&for=state:*&key={key}".format(key=settings.CENSUS_KEY)

import requests

r = requests.get(url)
r.content


Out[3]:
b'[["DP02_0001PE","state"],\n["1845169","1"],\n["251651","2"],\n["2392168","4"],\n["1143859","5"],\n["12552658","6"],\n["1996088","8"],\n["1357812","9"],\n["340308","10"],\n["266662","11"],\n["7197943","12"],\n["3532908","13"],\n["447748","15"],\n["583106","16"],\n["4770194","17"],\n["2480077","18"],\n["1227048","19"],\n["1113911","20"],\n["1707004","21"],\n["1719473","22"],\n["554543","23"],\n["2157717","24"],\n["2522394","25"],\n["3819068","26"],\n["2111943","27"],\n["1090521","28"],\n["2359135","29"],\n["408938","30"],\n["733570","31"],\n["1006605","32"],\n["519137","33"],\n["3198799","34"],\n["764996","35"],\n["7238922","36"],\n["3731325","37"],\n["290944","38"],\n["4554672","39"],\n["1446667","40"],\n["1516957","41"],\n["4958249","42"],\n["413083","44"],\n["1787340","45"],\n["323765","46"],\n["2480090","47"],\n["8970959","48"],\n["895691","49"],\n["258520","50"],\n["3038967","51"],\n["2636817","53"],\n["741544","54"],\n["2288362","55"],\n["223513","56"]]'

In [4]:
# http://api.census.gov/data/2013/acs1/profile/variables/DP02_0001PE.json exists

requests.get("http://api.census.gov/data/2013/acs1/profile/variables/DP02_0001PE.json").json()


Out[4]:
{'concept': 'SELECTED SOCIAL CHARACTERISTICS IN THE UNITED STATES',
 'label': 'HOUSEHOLDS BY TYPE!!Total households',
 'name': 'DP02_0001PE',
 'predicateType': 'int'}

In [5]:
# 2013 ACS-1year sample call

url = "http://api.census.gov/data/2013/acs1/profile?get=DP02_0001PE&for=state:*&key={key}".format(key=settings.CENSUS_KEY)

import requests

r = requests.get(url)
r.content


Out[5]:
b'[["DP02_0001PE","state"],\n["1822439","01"],\n["246015","02"],\n["2400809","04"],\n["1125899","05"],\n["12650592","06"],\n["2002800","08"],\n["1339860","09"],\n["339071","10"],\n["271651","11"],\n["7211584","12"],\n["3546965","13"],\n["450120","15"],\n["588489","16"],\n["4783421","17"],\n["2498395","18"],\n["1236209","19"],\n["1113729","20"],\n["1705623","21"],\n["1728149","22"],\n["547686","23"],\n["2161680","24"],\n["2536321","25"],\n["3832466","26"],\n["2119954","27"],\n["1091002","28"],\n["2362853","29"],\n["406288","30"],\n["730579","31"],\n["1002571","32"],\n["519246","33"],\n["3176139","34"],\n["753507","35"],\n["7219356","36"],\n["3757480","37"],\n["298298","38"],\n["4564745","39"],\n["1447277","40"],\n["1523799","41"],\n["4938894","42"],\n["406366","44"],\n["1794989","45"],\n["331406","46"],\n["2490249","47"],\n["9110853","48"],\n["899475","49"],\n["253234","50"],\n["3055863","51"],\n["2644557","53"],\n["738653","54"],\n["2289424","55"],\n["224003","56"]]'

In [6]:
# trying to get total population for 2013-1year

# http://censusreporter.org/data/table/?table=B01003&geo_ids=04000US06&primary_geo_id=04000US06
# reports 38,332,521 +/- 0

requests.get("http://api.census.gov/data/2013/acs1/variables/B01003_001E.json").json()


Out[6]:
{'concept': 'B01003.  Total Population',
 'label': 'Total',
 'name': 'B01003_001E',
 'predicateType': 'int'}

In [7]:
url = "http://api.census.gov/data/2013/acs1/profile?get=B01003_001E&for=state:*&key={key}".format(key=settings.CENSUS_KEY)
r = requests.get(url)
r.content


Out[7]:
b"error: error: unknown variable 'B01003_001E'"

In [8]:
# how about from the following tutorial:
# http://www.digital-geography.com/many-people-live-area/#.VDrwINTF8ds

Census(settings.CENSUS_KEY, year=2012).acs.get(["B01003_001E"],{'for':'state:*'})


Out[8]:
[{'B01003_001E': '4777326', 'state': '01'},
 {'B01003_001E': '711139', 'state': '02'},
 {'B01003_001E': '6410979', 'state': '04'},
 {'B01003_001E': '2916372', 'state': '05'},
 {'B01003_001E': '37325068', 'state': '06'},
 {'B01003_001E': '5042853', 'state': '08'},
 {'B01003_001E': '3572213', 'state': '09'},
 {'B01003_001E': '900131', 'state': '10'},
 {'B01003_001E': '605759', 'state': '11'},
 {'B01003_001E': '18885152', 'state': '12'},
 {'B01003_001E': '9714569', 'state': '13'},
 {'B01003_001E': '1362730', 'state': '15'},
 {'B01003_001E': '1567803', 'state': '16'},
 {'B01003_001E': '12823860', 'state': '17'},
 {'B01003_001E': '6485530', 'state': '18'},
 {'B01003_001E': '3047646', 'state': '19'},
 {'B01003_001E': '2851183', 'state': '20'},
 {'B01003_001E': '4340167', 'state': '21'},
 {'B01003_001E': '4529605', 'state': '22'},
 {'B01003_001E': '1329084', 'state': '23'},
 {'B01003_001E': '5785496', 'state': '24'},
 {'B01003_001E': '6560595', 'state': '25'},
 {'B01003_001E': '9897264', 'state': '26'},
 {'B01003_001E': '5313081', 'state': '27'},
 {'B01003_001E': '2967620', 'state': '28'},
 {'B01003_001E': '5982413', 'state': '29'},
 {'B01003_001E': '990785', 'state': '30'},
 {'B01003_001E': '1827306', 'state': '31'},
 {'B01003_001E': '2704204', 'state': '32'},
 {'B01003_001E': '1317474', 'state': '33'},
 {'B01003_001E': '8793888', 'state': '34'},
 {'B01003_001E': '2055287', 'state': '35'},
 {'B01003_001E': '19398125', 'state': '36'},
 {'B01003_001E': '9544249', 'state': '37'},
 {'B01003_001E': '676253', 'state': '38'},
 {'B01003_001E': '11533561', 'state': '39'},
 {'B01003_001E': '3749005', 'state': '40'},
 {'B01003_001E': '3836628', 'state': '41'},
 {'B01003_001E': '12699589', 'state': '42'},
 {'B01003_001E': '1052471', 'state': '44'},
 {'B01003_001E': '4630351', 'state': '45'},
 {'B01003_001E': '815871', 'state': '46'},
 {'B01003_001E': '6353226', 'state': '47'},
 {'B01003_001E': '25208897', 'state': '48'},
 {'B01003_001E': '2766233', 'state': '49'},
 {'B01003_001E': '625498', 'state': '50'},
 {'B01003_001E': '8014955', 'state': '51'},
 {'B01003_001E': '6738714', 'state': '53'},
 {'B01003_001E': '1850481', 'state': '54'},
 {'B01003_001E': '5687219', 'state': '55'},
 {'B01003_001E': '562803', 'state': '56'},
 {'B01003_001E': '3716727', 'state': '72'}]

In [9]:
import json
fp = open("/Users/raymondyee/Downloads/variables_acs1_2013.json")
variables_acs1_2013 = json.load(fp)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-9-32c997eac7f9> in <module>()
      1 import json
----> 2 fp = open("/Users/raymondyee/Downloads/variables_acs1_2013.json")
      3 variables_acs1_2013 = json.load(fp)

FileNotFoundError: [Errno 2] No such file or directory: '/Users/raymondyee/Downloads/variables_acs1_2013.json'

In [ ]:
len(variables_acs1_2013['variables'].keys())

In [ ]:
'B01003_001E' in variables_acs1_2013['variables'].keys()

In [ ]:
variables_acs1_2013 = json.load

In [ ]:
# trying to get the 1-year total population for 2012 --> en route to the 2013 1-year

c.acs1dp.get('DP05_0004E', {'for': 'state:*'})

In [ ]:
c.acs.get('B01001_004E', {'for': 'state:*'})

In [ ]:
# age and sex
# http://censusreporter.org/data/table/?table=B01001&geo_ids=16000US0606000&primary_geo_id=16000US0606000

In [ ]:
requests.get("http://api.census.gov/data/2013/acs1/variables/B01001_001E.json").json()

In [ ]:
url = "http://api.census.gov/data/2013/acs1/profile?get=B01001_001E&for=state:*&key={key}".format(key=settings.CENSUS_KEY)


r = requests.get(url)
r.content

I was looking at the wrong place!!

Summary File: API Call: http://api.census.gov/data/2013/acs1? 2013 ACS Summary File Variables [ html | xml | json ] ACS Technical Documentation

Data Profile: Example Call: http://api.census.gov/data/2013/acs1/profile?get=DP02_0001PE&for=state:*&key=... 2013 ACS Data Profile Variables [ html | xml | json ] ACS Technical Documentation


In [ ]:
requests.get("http://api.census.gov/data/2013/acs1/profile/variables/DP05_0028E.json").json()

In [ ]:
url = "http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E&for=state:*&key={key}".format(key=settings.CENSUS_KEY)
r = requests.get(url)
r.json()

In [ ]:
c.acs1dp.get('DP05_0028E', {'for': 'state:*'}, year=2013)

In [ ]:
# how to get the total population of Berkeley, CA
# let's do Alameda County first

requests.get("http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E,NAME&for=county:001&in=state:06").json()

In [ ]:
requests.get("http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E,NAME&for=place:06000&in=state:06").json()

In [ ]:
# age
# http://censusreporter.org/profiles/16000US0606000-berkeley-ca/#age

requests.get("http://api.census.gov/data/2013/acs1/profile/variables/DP05_0004E.json").json()

In [ ]:
r = requests.get("http://api.census.gov/data/2013/acs1/profile?get=DP05_0028E,DP05_0004E,DP05_0005E,NAME&for=place:06000&in=state:06").json()
r

In [ ]:
from itertools import izip
d = dict(izip(r[0],r[1]))
d

In [ ]:
# number of population 0->9

int(d[u'DP05_0004E']) + int(d[u'DP05_0004E'])

In [ ]: