-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from USEPA/v1.2.3-release
V1.2.3 release
- Loading branch information
Showing
25 changed files
with
311 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# BLS_QCEW.py (flowsa) | ||
# !/usr/bin/env python3 | ||
# coding=utf-8 | ||
""" | ||
Pulls Quarterly Census of Employment and Wages data in NAICS from Bureau | ||
of Labor Statistics. Writes out to various FlowBySector class files for | ||
these data items | ||
EMP = Number of employees, Class = Employment | ||
PAYANN = Annual payroll ($1,000), Class = Money | ||
ESTAB = Number of establishments, Class = Other | ||
This script is designed to run with a configuration parameter | ||
--year = 'year' e.g. 2015 | ||
""" | ||
|
||
import json | ||
import pandas as pd | ||
import numpy as np | ||
from flowsa.location import get_all_state_FIPS_2, get_county_FIPS, US_FIPS | ||
from flowsa.common import fba_default_grouping_fields, load_api_key | ||
from flowsa.schema import flow_by_activity_wsec_fields, \ | ||
flow_by_activity_mapped_wsec_fields | ||
from flowsa.flowbyfunctions import assign_fips_location_system, \ | ||
aggregator | ||
from flowsa.dataclean import add_missing_flow_by_fields, \ | ||
replace_strings_with_NoneType | ||
|
||
|
||
def census_qwi_url_helper(*, build_url, year, config, **_): | ||
""" | ||
This helper function uses the "build_url" input from flowbyactivity.py, | ||
which is a base url for data imports that requires parts of the url text | ||
string to be replaced with info specific to the data year. This function | ||
does not parse the data, only modifies the urls from which data is | ||
obtained. | ||
:param build_url: string, base url | ||
:param config: dictionary, items in FBA method yaml | ||
:param args: dictionary, arguments specified when running flowbyactivity.py | ||
flowbyactivity.py ('year' and 'source') | ||
:return: list, urls to call, concat, parse, format into Flow-By-Activity | ||
format | ||
""" | ||
quarters = [1, 2, 3, 4] | ||
urls = [] | ||
if int(year) >= 2015: | ||
fips_year = str(2015) | ||
elif int(year) >= 2013: | ||
fips_year = str(2013) | ||
else: | ||
fips_year = str(2010) | ||
county_fips_df = get_county_FIPS(fips_year) | ||
county_fips = county_fips_df.FIPS | ||
for q in quarters: | ||
for d in county_fips: | ||
url = build_url | ||
url = url.replace('__year__', str(year)) | ||
userAPIKey = load_api_key(config['api_name']) | ||
url = url.replace("__apiKey__", userAPIKey) | ||
state_digit = str(d[0]) + str(d[1]) | ||
county_digit = str(d[2]) + str(d[3]) + str(d[4]) | ||
url = url.replace("__state__", state_digit) | ||
url = url.replace("__county__", county_digit) | ||
url = url.replace("__quarter__", str(q)) | ||
urls.append(url) | ||
|
||
return urls | ||
|
||
|
||
def census_qwi_call(*, resp, **_): | ||
""" | ||
Convert response for calling url to pandas dataframe, | ||
begin parsing df into FBA format | ||
:param resp: df, response from url call | ||
:return: pandas dataframe of original source data | ||
""" | ||
try: | ||
json_load = json.loads(resp.text) | ||
# convert response to dataframe | ||
df = pd.DataFrame(data=json_load[1:len(json_load)], | ||
columns=json_load[0]) | ||
except: | ||
print(resp) | ||
df = pd.DataFrame() | ||
finally: | ||
return df | ||
|
||
|
||
def census_qwi_parse(*, df_list, year, **_): | ||
""" | ||
Combine, parse, and format the provided dataframes | ||
:param df_list: list of dataframes to concat and format | ||
:param args: dictionary, used to run flowbyactivity.py | ||
('year' and 'source') | ||
:return: df, parsed and partially formatted to flowbyactivity | ||
specifications | ||
""" | ||
# Concat dataframes | ||
df = pd.concat(df_list, ignore_index=True) | ||
# drop rows don't need | ||
# get rid of None values in EmpTotal | ||
df = df[df.EmpTotal.notnull()] | ||
df.loc[df['ownercode'] == 'A00', 'Owner'] = 'State and local government ' \ | ||
'plus private ownership' | ||
df.loc[df['ownercode'] == 'A01', 'Owner'] = 'Federal government' | ||
df.loc[df['ownercode'] == 'A05', 'Owner'] = 'All Private' | ||
df = df.reindex() | ||
|
||
# Combine the State and County into the location. | ||
df['Location'] = df['state'] + df['county'] | ||
|
||
# industry needs to be renamed Activity Produced by. | ||
# add the Quarter and ownership codes to flowname. | ||
df['FlowName'] = df.apply( | ||
lambda x: f'Number of employees, {x["Owner"]}, Quarter {x["quarter"]}', | ||
axis=1) | ||
df = df.rename(columns={'EmpTotal': 'FlowAmount', | ||
'year': 'Year', | ||
'industry': "ActivityProducedBy"}) | ||
|
||
df = df.drop(columns=['state', 'county', 'Owner', 'ownercode']) | ||
# add location system based on year of data | ||
df = assign_fips_location_system(df, year) | ||
# add hard code data | ||
df['SourceName'] = 'Census_QWI' | ||
df['FlowType'] = "ELEMENTARY_FLOW" | ||
df['Class'] = "Employment" | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## Current Status | ||
|
||
Known issues with exisiting FBA and FBS methods are shown in [method_status.yaml](method_status.yaml) | ||
|
||
``` | ||
<Method_Name> | ||
Active: True or False indicates whether the method can still be run. | ||
Status: Description of the current status. | ||
Type: Type of error generated by the method, e.g. 'HTTPError' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ years: | |
- 2016 | ||
- 2017 | ||
- 2018 | ||
- 2019 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
author: US Census Bureau | ||
source_name: Quarterly Workforce Indicators | ||
source_url: https://www.census.gov/data/developers/data-sets/qwi.html#ownership | ||
bib_id: Census_QWI | ||
api_name: Census | ||
api_key_required: True | ||
format: csv # comma delineated data | ||
url: | ||
base_url: https://api.census.gov/data/timeseries/qwi/se?get=industry,EmpTotal,ownercode&for=county:__county__&in=state:__state__&year=__year__&quarter=__quarter__&key=__apiKey__ | ||
url_replace_fxn: !script_function:Census_QWI census_qwi_url_helper | ||
call_response_fxn: !script_function:Census_QWI census_qwi_call | ||
parse_response_fxn: !script_function:Census_QWI census_qwi_parse | ||
years: | ||
- 2002 | ||
- 2010 | ||
- 2011 | ||
- 2012 | ||
- 2013 | ||
- 2014 | ||
- 2015 | ||
- 2016 | ||
- 2017 | ||
- 2018 | ||
- 2019 | ||
- 2020 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.