-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.py
161 lines (124 loc) · 4.24 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# %% [markdown]
# ---
# title: "Python script file"
# ---
# %%
import requests
import json
import pandas as pd
import pins
from pins import board_connect
from dotenv import load_dotenv
load_dotenv()
import os
current_dir = os.getcwd()
file_path = os.path.join(current_dir, "all_data_report.json")
# BLS API Key obtained at https://www.bls.gov/developers/home.htm
bls_key = os.environ.get("BLS_KEY")
class c_bls_data_api:
"""
File name: c_bls_data_api.py
Class name: c_bls_data_api
Author: Originally by Randy Runtsch, adapted by Isabella Velásquez
Date: October 24, 2023
Description: Call BLS Data API with a query and handle the results.
Reference: https://www.bls.gov/developers/api_python.htm
"""
def __init__(self, parameters):
self.data = self.get_report(parameters)
def get_report(self, parameters):
headers = {"Content-type": "application/json"}
response = requests.post(
"https://api.bls.gov/publicAPI/v2/timeseries/data/",
data=parameters,
headers=headers,
)
if response.status_code != 200:
raise Exception(f"API Error: {response.status_code}")
return response.json()
table_ids = [
"CUUR0000SA0",
"CUUR0000SA0L1E",
"CUUR0000SAF1",
"CUUR0000SA0E",
"CUUR0000SETB01",
"CUUR0000SAM",
"CUUR0000SEMC01",
"CUUR0000SEMD01",
"CUUR0000SEMF01",
"CUUR0000SAH1",
]
id_to_label = {
"CUUR0000SA0": "All groups CPI",
"CUUR0000SA0L1E": "All items less food and energy",
"CUUR0000SAF1": "Food",
"CUUR0000SA0E": "Energy",
"CUUR0000SETB01": "Gasoline",
"CUUR0000SAM": "Medical care",
"CUUR0000SEMC01": "Physicians' services",
"CUUR0000SEMD01": "Hospital services",
"CUUR0000SEMF01": "Prescription drugs",
"CUUR0000SAH1": "Shelter",
}
all_data = {}
print("Program started.")
for table_id in table_ids:
bls_key = os.environ.get("BLS_KEY")
parameters = json.dumps(
{
"registrationkey": bls_key,
"seriesid": [table_id],
"startyear": "2018",
"endyear": "2024",
"calculations": "true",
}
)
bls_data_object = c_bls_data_api(parameters)
all_data[table_id] = bls_data_object.data
with open(file_path, "w") as f:
json.dump(all_data, f, indent=6)
print("Program completed.")
with open(file_path, "r") as f:
data = json.load(f)
print("Loaded JSON data")
dfs = []
for key, series in data.items():
try:
series_data = series["Results"]["series"][0]["data"]
df_temp = pd.DataFrame(series_data)
df_temp["Category"] = key
dfs.append(df_temp)
except KeyError:
print(f"Proper keys not found for: {key}")
df = pd.concat(dfs, ignore_index=True)
df["Year-Month"] = df["year"] + "-" + df["period"].str[1:]
df["value"] = pd.to_numeric(df["value"], errors="coerce")
df["Category_Label"] = df["Category"].map(id_to_label)
df['Year-Month'] = df['year'].astype(str) + '-' + df['periodName']
df["Year-Month"] = pd.to_datetime(df["Year-Month"])
january_2018_data = df[
(df["Year-Month"].dt.year == 2018) & (df["Year-Month"].dt.month == 1)
]
january_2018_values = dict(
zip(january_2018_data["Category_Label"], january_2018_data["value"])
)
df["Value_January_2018"] = df["Category_Label"].map(january_2018_values)
df["Difference_from_January_2018"] = df["value"] - df["Value_January_2018"]
df["Percent_Change_from_January_2018"] = 0
for category_label in january_2018_values.keys():
df.loc[
df["Category_Label"] == category_label, "Percent_Change_from_January_2018"
] = (
df.loc[df["Category_Label"] == category_label, "Difference_from_January_2018"]
/ january_2018_values[category_label]
) * 100
df = df.reset_index(drop=True)
# Calculate change from previous month
df = df.sort_values(by="Year-Month")
grouped = df.groupby("Category_Label")
df["Percent_Change_from_Previous_Month"] = grouped["value"].pct_change() * 100
df = df.reset_index(drop=True)
connect_server = os.environ.get("CONNECT_SERVER")
connect_api_key = os.environ.get("CONNECT_API_KEY")
board = board_connect(server_url=connect_server, api_key=connect_api_key)
board.pin_write(df, "isabella.velasquez/bls-cpi-data", type="csv")