Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix US BEA GDP script #985

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions scripts/us_bea/states_gdp/import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ class StateGDPDataLoader:
'West Virginia', 'Wisconsin', 'Wyoming'
]
_ZIP_LINK = 'https://apps.bea.gov/regional/zip/SQGDP.zip'
_STATE_QUARTERLY_GDP_FILE = 'SQGDP1__ALL_AREAS_2005_2020.csv'
_STATE_QUARTERLY_GDP_FILE_PREFIX = 'SQGDP1__ALL_AREAS_'
_QUARTER_MONTH_MAP = {'Q1': '03', 'Q2': '06', 'Q3': '09', 'Q4': '12'}

def __init__(self):
"""Initializes instance, assigning member data frames to None."""
self.raw_df = None
self.clean_df = None

def download_data(self, zip_link=None, file=None):
def download_data(self, zip_link=None, file_prefix=None):
"""Downloads ZIP file, extracts the desired CSV, and puts it into a data

frame. Stores that data frame in the instance raw_df variable.
Expand All @@ -69,20 +69,27 @@ def download_data(self, zip_link=None, file=None):
zip_link: Link to the raw data to be downloaded in ZIP format. If
None or unspecified, this value gets overriden by the class
constant _ZIP_LINK.
file: File within the specified ZIP file that should be downloaded
and stored. If None or unspecified, this value gets overriden by
the class constant _STATE_QUARTERLY_GDP_FILE.
file_prefix: Prefix of the file within the specified ZIP file that
should be downloaded and stored.
If None or unspecified, this value gets overriden by
the file matching class constant _STATE_QUARTERLY_GDP_FILE_PREFIX.
"""
if zip_link is None:
zip_link = self._ZIP_LINK
if file is None:
file = self._STATE_QUARTERLY_GDP_FILE
# Open zip file from link.
resp = urlopen(zip_link)

# Read the file, interpret it as bytes, and create a ZipFile instance
# from it for easy handling.
zip_file = zipfile.ZipFile(io.BytesIO(resp.read()))
file = None
if file_prefix is None:
file_prefix = self._STATE_QUARTERLY_GDP_FILE_PREFIX
# Get the file matching the file prefix.
for file_name in zip_file.namelist():
if file_name.startswith(file_prefix):
file = file_name
break

# Open the specific desired file (CSV) from the folder, and decode it.
# This results in a string representation of the file. Interpret that
Expand Down
5 changes: 3 additions & 2 deletions scripts/us_bea/states_gdp/import_industry_data_and_gen_mcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ class StateGDPIndustryDataLoader(import_data.StateGDPDataLoader):
Attributes:
df: DataFrame (DF) with the cleaned data.
"""
_STATE_QUARTERLY_INDUSTRY_GDP_FILE = 'SQGDP2__ALL_AREAS_2005_2020.csv'
_STATE_QUARTERLY_INDUSTRY_GDP_FILE_PREFIX = 'SQGDP2__ALL_AREAS_'

def download_data(self, zip_link=None, file=None):
"""Downloads ZIP file, extracts the desired CSV, and puts it into a data

frame. Stores that data frame in the instance raw_df variable.
"""
super().download_data(file=self._STATE_QUARTERLY_INDUSTRY_GDP_FILE)
super().download_data(
file_prefix=self._STATE_QUARTERLY_INDUSTRY_GDP_FILE_PREFIX)

def process_data(self, raw_data=None):
"""Cleans raw_df and converts it from wide to long format.
Expand Down
Loading
Loading