def main():
  # Create a list of dcids for California, Kentucky, and Maryland
  ca, ky, md = 'geoId/06', 'geoId/21', 'geoId/24'
  dcids = [ca, ky, md]

  # Get the population of all employed individuals in the above states.
  print('Get Populations for All Employed Individuals')
  employed = dc.get_populations(dcids, 'Person', constraining_properties={
                  'employment': 'BLS_Employed'})
  print(json.dumps(employed, indent=2))

  # Get the count for all male / females for the above states in 2016
  print('Get Population Counts for Employed Individuals in Maryland')
  pop_dcids = [employed[md]]
  obs = dc.get_observations(pop_dcids,
                            'count',
                            'measuredValue',
                            '2018-12',
                            observation_period='P1M',
                            measurement_method='BLSSeasonallyAdjusted')
  print(json.dumps(obs, indent=2))

  # Get all population and observation data of Mountain View.
  print('Get Mountain View population and observation')
  popobs = dc.get_pop_obs("geoId/0649670")
  pprint.pprint(popobs)
Exemple #2
0
    def test_valid_dcid(self, urlopen):
        """ Calling get_pop_obs with valid dcid returns valid results. """
        # Set the API key
        dc.set_api_key('TEST-API-KEY')

        # Call get_pop_obs
        pop_obs = dc.get_pop_obs('geoId/06085')
        self.assertDictEqual(
            pop_obs, {
                'name': 'Mountain View',
                'placeType': 'City',
                'populations': {
                    'dc/p/013ldrstf6lnf': {
                        'numConstraints':
                        6,
                        'observations': [{
                            'marginOfError': 119,
                            'measuredProp': 'count',
                            'measuredValue': 225,
                            'measurementMethod': 'CensusACS5yrSurvey',
                            'observationDate': '2014'
                        }, {
                            'marginOfError': 108,
                            'measuredProp': 'count',
                            'measuredValue': 180,
                            'measurementMethod': 'CensusACS5yrSurvey',
                            'observationDate': '2012'
                        }],
                        'popType':
                        'Person',
                        'propertyValues': {
                            'age': 'Years16Onwards',
                            'gender': 'Male',
                            'income': 'USDollar30000To34999',
                            'incomeStatus': 'WithIncome',
                            'race': 'USC_HispanicOrLatinoRace',
                            'workExperience': 'USC_NotWorkedFullTime'
                        }
                    }
                }
            })
def main():
    # Create a list of dcids for California, Kentucky, and Maryland
    ca, ky, md = 'geoId/06', 'geoId/21', 'geoId/24'
    dcids = [ca, ky, md]

    # Get the population of all employed individuals in the above states.
    utils._print_header('Get Populations for All Employed Individuals')
    employed = dc.get_populations(
        dcids,
        'Person',
        constraining_properties={'employment': 'BLS_Employed'})
    print('> Printing all populations of employed individuals\n')
    print(json.dumps(employed, indent=2))

    # Get the count for all male / females for the above states in 2016
    utils._print_header(
        'Get Population Counts for Employed Individuals in Maryland')
    pop_dcids = [employed[md]]
    print('> Requesting observations for {} in December 2018\n'.format(
        pop_dcids))
    obs = dc.get_observations(pop_dcids,
                              'count',
                              'measuredValue',
                              '2018-12',
                              observation_period='P1M',
                              measurement_method='BLSSeasonallyAdjusted')
    print(json.dumps(obs, indent=2))

    # We perform the same workflow using a Pandas DataFrame. First, initialize a
    # DataFrame with Santa Clara and Montgomery County.
    utils._print_header('Initialize the DataFrame')
    pd_frame = pd.DataFrame({'state': ['geoId/06', 'geoId/21', 'geoId/24']})
    pd_frame['state_name'] = dc.get_property_values(pd_frame['state'], 'name')
    pd_frame = dc.flatten_frame(pd_frame)
    print(pd_frame)

    # Get populations for employed individuals
    utils._print_header('Add Population and Observation to DataFrame')
    pd_frame['employed_pop'] = dc.get_populations(
        pd_frame['state'],
        'Person',
        constraining_properties={'employment': 'BLS_Employed'})

    # Add the observation for employed individuals
    pd_frame['employed_count'] = dc.get_observations(
        pd_frame['employed_pop'],
        'count',
        'measuredValue',
        '2018-12',
        observation_period='P1M',
        measurement_method='BLSSeasonallyAdjusted')
    print(pd_frame)

    # Final dataframe. Use the convenience function "clean_frame" to convert
    # columns to numerical types.
    utils._print_header('Final Data Frame')
    pd_frame = dc.clean_frame(pd_frame)
    print(pd_frame)

    # Get all population and observation data of Mountain View.
    utils._print_header('Get Mountain View population and observation')
    popobs = dc.get_pop_obs("geoId/0649670")
    pprint.pprint(popobs)