def test_bad_dcids(self, urlopen): """ Calling get_populations with dcids that do not exist returns empty results. """ # Call get_populations pops_1 = dc.get_populations(['geoId/06085', 'dc/MadDcid'], 'Person', constraining_properties=self._constraints) pops_2 = dc.get_populations(['dc/MadDcid', 'dc/MadderDcid'], 'Person', constraining_properties=self._constraints) # Verify the results self.assertDictEqual(pops_1, {'geoId/06085': 'dc/p/crgfn8blpvl35'}) self.assertDictEqual(pops_2, {})
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)
def test_no_dcids(self, post_mock): """ Calling get_populations with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') pops = dc.get_populations( [], 'Person', constraining_properties=self._constraints) self.assertDictEqual(pops, {})
def test_multiple_dcids(self, urlopen): """ Calling get_populations with proper dcids returns valid results. """ # Call get_populations populations = dc.get_populations(['geoId/06085', 'geoId/4805000'], 'Person', constraining_properties=self._constraints) self.assertDictEqual(populations, { 'geoId/06085': 'dc/p/crgfn8blpvl35', 'geoId/4805000': 'dc/p/f3q9whmjwbf36' })
def test_series_no_dcids(self, post_mock): """ Calling get_populations with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') dcids = pd.Series([]) expected = pd.Series([]) # Call get_populations actual = dc.get_populations( dcids, 'Person', constraining_properties=self._constraints) assert_series_equal(actual, expected)
def test_multiple_dcids(self, post_mock): """ Calling get_populations with proper dcids returns valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_populations populations = dc.get_populations(['geoId/06085', 'geoId/4805000'], 'Person', constraining_properties=self._constraints) self.assertDictEqual(populations, { 'geoId/06085': 'dc/p/crgfn8blpvl35', 'geoId/4805000': 'dc/p/f3q9whmjwbf36' })
def test_series_bad_dcids(self, post_mock): """ Calling get_populations with a Pandas Series and dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get input and expected output dcids_1 = pd.Series(['geoId/06085', 'dc/MadDcid']) dcids_2 = pd.Series(['dc/MadDcid', 'dc/MadderDcid']) expected_1 = pd.Series(['dc/p/crgfn8blpvl35', '']) expected_2 = pd.Series(['', '']) # Call get_populations actual_1 = dc.get_populations( dcids_1, 'Person', constraining_properties=self._constraints) actual_2 = dc.get_populations( dcids_2, 'Person', constraining_properties=self._constraints) # Assert that the results are correct assert_series_equal(actual_1, expected_1) assert_series_equal(actual_2, expected_2)
def test_series_multiple_dcids(self, post_mock): """ Calling get_populations with a Pandas Series and proper dcids returns a Pandas Series with valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get the input and expected output dcids = pd.Series(['geoId/06085', 'geoId/4805000']) expected = pd.Series(['dc/p/crgfn8blpvl35', 'dc/p/f3q9whmjwbf36']) # Call get_populations actual = dc.get_populations( dcids, 'Person', constraining_properties=self._constraints) assert_series_equal(actual, expected)
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)
def test_no_dcids(self, urlopen): """ Calling get_populations with no dcids returns empty results. """ pops = dc.get_populations( [], 'Person', constraining_properties=self._constraints) self.assertDictEqual(pops, {})