· 6 years ago · Sep 04, 2019, 11:28 PM
1import pandas as pd
2import matplotlib.pyplot as plt
3import eia
4
5def retrieve_time_series(api, series_ID):
6 """
7 Return the time series dataframe, based on API and unique Series ID
8 Arguments:
9 api: API that we're connected to
10 series_ID: string. Name of the series that we want to pull from the EIA API
11 Outputs:
12 df: Pandas dataframe of time series
13 """
14 #Retrieve Data By Series ID
15 series_search = api.data_by_series(series=series_ID)
16 ##Create a pandas dataframe from the retrieved time series
17 df = pd.DataFrame(series_search)
18 return df
19
20def scatterplot(x_data, y_data, x_label, y_label, title):
21 """
22 Arguments:
23 x_data: Series. Desired x-axis for scatterplot.
24 y_data: Series. Desired y-axis for scatterplot.
25 x_label: String. Label for x-axis.
26 y_label: String. Label for y-axis.
27 title: String. Title of plot
28 Outputs:
29 Scatterplot in console.
30 """
31 fig, ax = plt.subplots()
32 ax.scatter(x_data, y_data, s = 30, color = '#539caf', alpha = 0.75)
33 ax.set_title(title)
34 ax.set_xlabel(x_label)
35 ax.set_ylabel(y_label)
36 fig.autofmt_xdate()
37
38#####EXECUTE IN MAIN BLOCK
39
40#Create EIA API using your specific API key
41api_key = 'YOUR API KEY HERE'
42api = eia.API(api_key)
43
44#Pull the oil WTI price data
45series_ID='PET.EER_EPMRU_PF4_RGC_DPG.D'
46gasoline_price_df=retrieve_time_series(api, series_ID)
47gasoline_price_df.reset_index(level=0, inplace=True)
48#Rename the columns for easer analysis
49gasoline_price_df.rename(columns={'index':'Date',
50 gasoline_price_df.columns[1]:'Gasoline_Price'},
51 inplace=True)
52
53#Visualize anomalies using matplotlib function
54scatterplot(gasoline_price_df['Date'],
55 gasoline_price_df['Gasoline_Price'],
56 'Date',
57 'Gasoline Price (Dollars Per Gallon)',
58 'US Gulf Coast Gasoline Price Time Series: 2014-Present')