예제 #1
0
def _get_dt_format():
    """
    Return the datetime format that should be used in the outputs
    """
    config = util.get_configuration()
    format = config['Output']['DateAndTimeFormat']
    return format
예제 #2
0
    def __init__(self, year=None, key=None):
        """
        Initialize a new ACS object

        Parameters
        ----------
        year : None or int, default None
            The last year to retrieve data from. For example, if
            year=2015, retrieve data from 2011-2015.

            If None, use the year specified in the configuration file.
        key : None or str, default None
            A key used to make requests for the data. If None, use the
            key specified in the configuration file.
        """
        # retrieve default values from configuration
        if year is None or key is None:
            config = util.get_configuration()
            if year is None:
                year = config['Census'].getint('Year')
            if key is None:
                key = config['Census']['Key']

        self.year = year
        self.key = key
예제 #3
0
    def _save_processed(self, df):
        # convert Timestamp column to str
        _config = util.get_configuration()
        _dt_format = _config['Output']['DateAndTimeFormat']
        df['INCIDENT_DATE'] = df['INCIDENT_DATE'].dt.strftime(_dt_format)

        df.to_csv(self.processed_path, index=False)
예제 #4
0
    def __init__(self, year=None):
        """
        Initialize a TIGER object

        Parameters
        ----------
        year : None or int
            Year to retrieve geographies from. If None, use the
            year present in the configuration file.
        """
        if year is None:
            config = util.get_configuration()
            year = config['Census'].getint('Year')

        self.year = year
예제 #5
0
def _download(url, out):
    """
    Download a file, no checks

    Parameters
    ----------
    url : str
    out : str or Path
    """
    ua = util.get_configuration()['Downloads']['UserAgent']
    subprocess.run([
        'http',
        '--ignore-stdin',
        '--check-status',
        '--timeout=2.0',
        '--print=',
        '--output', str(out),
        '--download',
        url,
        'User-Agent:' + ua,
    ], check=True)
예제 #6
0
    def _query(self, variables, geography='us', inside=None):
        """
        Query the ACS API and returns the result as a list of lists

        Parameters
        ----------
        variables : list of str
            A list of variable names to query for.

            The list must not contain more than 50 elements.

            Example:

            ["B01001_002E", "B01001_026E"]
        geography : str, default 'us'
            The unit to retrieve statistics from. For example, if you
            want to retrieve statistics for census tracts, set
            geography='tract'.

            A complete list of geographies can be found here (look at
            the leftmost column):

            https://api.census.gov/data/2016/acs/acs5/examples.html
        inside : str, default None
            Restricts the search inside a specified area. For example,
            if you only want results inside the state of Alabama, you
            can set inside to:

            'state:01'

            If you want to nest deeper, you can specify the needed
            geographies in order. For example:

            'state:01 county:001'

            Will filter the results to Autauga County, Alabama.

        Returns
        -------
        list of lists
            The first list contains the variable names while the others
            carry the requested values.
        """
        # API limit
        assert len(variables) <= 50

        # generate query params
        params = {
            'get': ','.join(variables),
            'for': '{}:*'.format(geography),
        }
        if inside is not None:
            params['in'] = inside
        if self.key != '':
            params['key'] = self.key

        # generate headers
        ua = util.get_configuration()['Downloads']['UserAgent']
        headers = {'User-Agent': ua}

        # generate query url
        query_url = f'https://api.census.gov/data/{self.year}/acs/acs5'

        r = requests.get(query_url, params=params, headers=headers)
        r.raise_for_status()

        o = r.json()

        return o