datamade / census

A Python wrapper for the US Census API.
BSD 3-Clause "New" or "Revised" License
628 stars 137 forks source link

Support census blocks? #111

Open Kirkman opened 2 years ago

Kirkman commented 2 years ago

Thanks for the fantastic library. I noticed that there is support for obtaining block-group-level data, but not block-level. Is there any chance you can add support for blocks?

WrightWillT commented 1 year ago

IIRC, while the Census collects data at the block level, they only report at the block-group level and above.

Kirkman commented 1 year ago

No, their API does provide block-level data for some datasets. For example, the 2020 Redistricting Data Summary File (PL 94-171) has block-level data.

You can test this for yourself by adding the following function to core.py:

@supported_years()
def state_county_block(self, fields, state_fips, county_fips, block, tract=None, **kwargs):
    geo = {
        'for': 'block:{}'.format(block),
        'in': 'state:{} county:{}'.format(state_fips, county_fips),
    }
    if tract:
        geo['in'] += ' tract:{}'.format(tract)
    return self.get(fields, geo=geo, **kwargs)

And then running a query like this:

data = c.pl.state_county_block(
    ('NAME', 'P1_001N'), # table
    '29', # state
    '189', # county
    Census.ALL, # blockgroup
    year='2020'
)
PhilipMathieu commented 1 year ago

@Kirkman mind if I copy this and do a pull request?