def test_bad_dcids(self, urlopen): """ Calling get_observations with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get the input dcids_1 = ['dc/p/x6t44d8jd95rd', 'dc/MadDcid'] dcids_2 = ['dc/MadDcid', 'dc/MadderDcid'] # Call get_observations actual_1 = dc.get_observations( dcids_1, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') actual_2 = dc.get_observations( dcids_2, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') # Verify the results self.assertDictEqual(actual_1, {'dc/p/x6t44d8jd95rd': 18704962.0}) self.assertDictEqual(actual_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_observations with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') actual = dc.get_observations([], 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') self.assertDictEqual(actual, {})
def test_multiple_dcids(self, urlopen): """ Calling get_observations with proper dcids returns valid results. """ dcids = ['dc/p/x6t44d8jd95rd', 'dc/p/lr52m1yr46r44', 'dc/p/fs929fynprzs'] expected = { 'dc/p/lr52m1yr46r44': 3075662.0, 'dc/p/fs929fynprzs': 1973955.0, 'dc/p/x6t44d8jd95rd': 18704962.0 } actual = dc.get_observations(dcids, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') self.assertDictEqual(actual, expected)
def test_series_multiple_dcids(self, post_mock): """ Calling get_observations 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') dcids = pd.Series( ['dc/p/x6t44d8jd95rd', 'dc/p/lr52m1yr46r44', 'dc/p/fs929fynprzs']) expected = pd.Series([18704962.0, 3075662.0, 1973955.0]) actual = dc.get_observations(dcids, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') assert_series_equal(actual, expected)
def test_series_bad_dcids(self, post_mock): """ Calling get_observations 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 the input and expected output dcids_1 = pd.Series(['dc/p/x6t44d8jd95rd', 'dc/MadDcid']) dcids_2 = pd.Series(['dc/MadDcid', 'dc/MadderDcid']) expected_1 = pd.Series([18704962.0, float('NaN')]) expected_2 = pd.Series([float('NaN'), float('NaN')]) # Call get_observations actual_1 = dc.get_observations(dcids_1, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') actual_2 = dc.get_observations(dcids_2, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') # Verify the results assert_series_equal(actual_1, expected_1) assert_series_equal(actual_2, expected_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. 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_observations with no dcids returns empty results. """ actual = dc.get_observations([], 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') self.assertDictEqual(actual, {})