Created
August 24, 2019 00:20
-
-
Save kperry2215/0d9a66d03467b60dbe454dc4f62ee9ff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import pandas as pd | |
import matplotlib.pyplot as plt | |
import eia | |
def retrieve_time_series(api, series_ID): | |
""" | |
Return the time series dataframe, based on API and unique Series ID | |
Arguments: | |
api: API that we're connected to | |
series_ID: string. Name of the series that we want to pull from the EIA API | |
Outputs: | |
df: Pandas dataframe of time series | |
""" | |
#Retrieve Data By Series ID | |
series_search = api.data_by_series(series=series_ID) | |
##Create a pandas dataframe from the retrieved time series | |
df = pd.DataFrame(series_search) | |
return df | |
def scatterplot(x_data, y_data, x_label, y_label, title): | |
""" | |
Arguments: | |
x_data: Series. Desired x-axis for scatterplot. | |
y_data: Series. Desired y-axis for scatterplot. | |
x_label: String. Label for x-axis. | |
y_label: String. Label for y-axis. | |
title: String. Title of plot | |
Outputs: | |
Scatterplot in console. | |
""" | |
fig, ax = plt.subplots() | |
ax.scatter(x_data, y_data, s = 30, color = '#539caf', alpha = 0.75) | |
ax.set_title(title) | |
ax.set_xlabel(x_label) | |
ax.set_ylabel(y_label) | |
fig.autofmt_xdate() | |
#####EXECUTE IN MAIN BLOCK | |
#Create EIA API using your specific API key | |
api_key = 'YOUR API KEY HERE' | |
api = eia.API(api_key) | |
#Pull the oil WTI price data | |
series_ID='PET.EER_EPMRU_PF4_RGC_DPG.D' | |
gasoline_price_df=retrieve_time_series(api, series_ID) | |
gasoline_price_df.reset_index(level=0, inplace=True) | |
#Rename the columns for easer analysis | |
gasoline_price_df.rename(columns={'index':'Date', | |
gasoline_price_df.columns[1]:'Gasoline_Price'}, | |
inplace=True) | |
#Visualize anomalies using matplotlib function | |
scatterplot(gasoline_price_df['Date'], | |
gasoline_price_df['Gasoline_Price'], | |
'Date', | |
'Gasoline Price (Dollars Per Gallon)', | |
'US Gulf Coast Gasoline Price Time Series: 2014-Present') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment