Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added acs1st and variable functions #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions census/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,46 @@ def tables(self, year=None):

# Pass it out
return resp.json()['groups']

def variables(self, year=None) -> list: # added
Copy link

@antidipyramid antidipyramid Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea here. We do plan on taking on a more general project of fetching API metadata from the
Census Data API Discovery Tool. Would you mind separating this out into another PR?

"""
Returns a list of the data variables available from this source.
"""
# Set the default year if one hasn't been passed
if year is None:
year = self.default_year

# Query the table metadata as raw JSON
tables_url = self.definitions_url % (year, 'acs/'+self.dataset)
resp = self.session.get(tables_url)

# Pass it out
return resp.json()['variables']

def find_variables(self, label: str, group: str, year:str=None) -> list:
"""
Returns a list of partial matches of sub variables
"""

variables = self.variables(year)
keys = list(
filter(
lambda row: variables[row]['group']==group and label in variables[row]['label'],
variables
)
)

matches = [[key, variables[key]['label']] for key in keys]

return matches


@supported_years()
def fields(self, year=None, flat=False):
if year is None:
year = self.default_year

data = {}

fields_url = self.definitions_url % (year, self.dataset)

resp = self.session.get(fields_url)
Expand Down Expand Up @@ -398,6 +430,16 @@ def _switch_endpoints(self, year):

dataset = 'acs5/subject'

class ACS1StClient(ACSClient): # added
default_year = 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the supported years to this class?

For example:

class ACS1DpClient(ACS1Client):

    dataset = 'acs1/profile'

    years = (2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012)
    ...

def _switch_endpoints(self, year):
self.endpoint_url = 'https://api.census.gov/data/%s/acs/%s'
self.definitions_url = 'https://api.census.gov/data/%s/acs/%s/variables.json'
self.definition_url = 'https://api.census.gov/data/%s/acs/%s/variables/%s.json'
self.groups_url = 'https://api.census.gov/data/%s/acs/%s/groups.json'

dataset = 'acs1/subject'


class ACS3Client(ACSClient):

Expand Down Expand Up @@ -595,7 +637,8 @@ def __init__(self, key, year=None, session=None):
self.acs5 = ACS5Client(key, year, session)
self.acs3 = ACS3Client(key, year, session)
self.acs1 = ACS1Client(key, year, session)
self.acs5st = ACS5StClient(key, year, session)
self.acs1st = ACS1StClient(key, year, session)
self.acs5st = ACS5StClient(key, year, session) # added
self.acs5dp = ACS5DpClient(key, year, session)
self.acs3dp = ACS3DpClient(key, year, session)
self.acs1dp = ACS1DpClient(key, year, session)
Expand Down