The purpose of this package is to facilitate a user's interaction with the U.S. Bureau of Labor Statistics API in connection with their personal interests and summarize and visualize the results in a Pandas Dataframe and PyPlot line plot. The data requested from the API is the U.S. city average, non-seasonally adjusted, Consumer Price Index for all urban consumers by month.
The following Python packages must be installed before using the module:
- pandas
- requests
- json
- ip2geotools
- python-dotenv
- os
- matplotlib
The module works with V2 of the U.S. Bureau of Labor Statistics API, which requires a personal API key. A blank .env file is provided with a place to insert your API key, which you can sign up for here.
def __init__(self, interest):
This is the initializer of the class.
Attributes:
-
api_key: the user’s api_key for the database stored in the .env file
-
interest: the user’s interest
-
interest_Series: a list of series codes that relate to the user’s interest
-
data: the relevant data from the database
-
startyear: the start year of the data
-
endyear: the end year of the data
Parameters:
- Interest: the user’s interest. Other functions in the class will use this to find series relevant to this interest.
Return:
- None
get_api_key(self):
This function gets the api key stored in the .env file for the user.
Parameters:
- None
Returns:
- the api key so that the user is able to request the data from the BLS API
def set_interest_Series(self):
Function to generate a list of relevant codes related to a user’s keyword input related to their interests.
Parameters:
- self (to initialize an object of the class, user already inputs an interest keyword)
Return:
- N/A (does not return anything, only updates values).
def get_request(self, start_year, end_year, series_list = None):
This function runs the API request to get the data from the BLS API related to specific series codes and assigns it to the self.data attribute.
Parameters:
-
start_year: the year that the user will start begin getting data from
-
end_year: the last year that the user will get data from
-
series_list: The default value for this is none. If the user does not specify a list of series codes, the function will use the series codes that were generated by set_interest_Series().
Returns:
- None
def error_handling(self, response): Note: Nested and run in the get_request() method
This function handles the two error messages that a user might encounter with a failed API request by letting a user know if their request succeeded or failed, and ending their interaction with the program if the request fails or is not processed.
Parameters:
- response: the response from the BLS API server
Returns:
- None
def summary_stats(self):
Function to generate summary stats of the specified series.
Parameters:
- self (self.data already an attribute of the class)
Return:
- N/A (does not return anything, instead prints out the summary stats in a clean manner)
def visualizer(self):
Function to generate plots demonstrating change over time in the consumer price index.
Parameters:
- self (self.data, self.startyear, self.endyear already an attribute of the class that gets updated).
Return:
- N/A (does not return anything, instead generates a plot)