-
Notifications
You must be signed in to change notification settings - Fork 0
/
epc_by_api.py
308 lines (252 loc) · 9.54 KB
/
epc_by_api.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#%%
import polars as pl
import get_ca_data as get_ca # functions for retrieving CA \ common data
import requests
import os
import yaml
#%%
epc_key = yaml.safe_load(open('../config.yml'))['epc']['auth_token']
#%%
ca_la_df = get_ca.get_ca_la_df(2023, inc_ns=True) # include NS
la_list = (ca_la_df['ladcd'])
# %%
def get_epc_csv(la: str, epc_key: str):
'''
Uses the opendatacommunities API to get EPC data for a given local authority and writes it to a CSV file
the epc auth_token is in the config.yml file in parent directory
This function uses pagination cribbed from https://epc.opendatacommunities.org/docs/api/domestic#domestic-pagination
'''
# Page size (max 5000)
query_size = 5000
# Output file name
output_file = f'data/epc_csv/epc_{la}.csv'
# Base url and example query parameters
base_url = 'https://epc.opendatacommunities.org/api/v1/domestic/search'
query_params = {'size': query_size, 'local-authority': la}
# Set up authentication
headers = {
'Accept': 'text/csv',
'Authorization': f'Basic {epc_key}'
}
# Keep track of whether we have made at least one request for CSV headers and search-after
first_request = True
# Keep track of search-after from previous request
search_after = None
# Loop over entries in query blocks of up to 5000 to write all the data into a file
with open(output_file, 'w') as file:
# Perform at least one request; if there's no search_after, there are no further results
while search_after != None or first_request:
# Only set search-after if this isn't the first request
if not first_request:
query_params["search-after"] = search_after
# Make the request
response = requests.get(base_url, headers=headers, params=query_params)
response.raise_for_status()
body = response.text
search_after = response.headers.get('X-Next-Search-After')
# For CSV data, only keep the header row from the first response
if not first_request and body:
body = body.split("\n", 1)[1]
# Write received data
file.write(body)
print(f"Written {len(body)} bytes to {output_file}")
first_request = False
#%%
# for la in la_list:
# get_epc_csv(la, epc_key)
#%%
# schema for the EPC data
cols_schema_adjusted = {
'lmk-key': pl.Utf8,
'postcode': pl.Utf8,
'local-authority': pl.Utf8,
'property-type': pl.Utf8,
'lodgement-datetime': pl.Utf8,
'transaction-type': pl.Utf8,
'tenure': pl.Utf8,
'mains-gas-flag': pl.Utf8,
'hot-water-energy-eff': pl.Utf8,
'windows-description': pl.Utf8,
'windows-energy-eff': pl.Utf8,
'walls-description': pl.Utf8,
'walls-energy-eff': pl.Utf8,
'roof-description': pl.Utf8,
'roof-energy-eff': pl.Utf8,
'mainheat-description': pl.Utf8,
'mainheat-energy-eff': pl.Utf8,
'mainheat-env-eff': pl.Utf8,
'main-heating-controls': pl.Utf8,
'mainheatcont-description': pl.Utf8,
'mainheatc-energy-eff': pl.Utf8,
'main-fuel': pl.Utf8,
'solar-water-heating-flag': pl.Utf8,
'construction-age-band': pl.Utf8,
'current-energy-rating': pl.Utf8,
'potential-energy-rating': pl.Utf8,
'current-energy-efficiency': pl.Utf8,
'potential-energy-efficiency': pl.Utf8,
'built-form': pl.Utf8,
'constituency': pl.Utf8,
'floor-description': pl.Utf8,
'environment-impact-current': pl.Int64,
'environment-impact-potential': pl.Int64,
'energy-consumption-current': pl.Int64,
'energy-consumption-potential': pl.Int64,
'co2-emiss-curr-per-floor-area': pl.Int64,
'co2-emissions-current': pl.Float64,
'co2-emissions-potential': pl.Float64,
'lighting-cost-current': pl.Int64,
'lighting-cost-potential': pl.Int64,
'heating-cost-current': pl.Int64,
'heating-cost-potential': pl.Int64,
'hot-water-cost-current': pl.Int64,
'hot-water-cost-potential': pl.Int64,
'total-floor-area': pl.Float64,
'number-habitable-rooms': pl.Int64,
'number-heated-rooms': pl.Int64,
'photo-supply': pl.Float64,
'uprn': pl.Int64,
'building-reference-number': pl.Int64}
#%%
def ingest_dom_certs_csv(la_list: list, cols_schema: dict) -> pl.DataFrame:
"""
Loop through all csv files in the epc_csv folder and ingest them into a single DF. Use an optimised polars query to ingest a subset of columns and do some transformations to create a single large DF of EPC data
"""
all_lazyframes = []
# rename columns to replace hyphens with underscores
cols_select_list = list(cols_schema.keys())
new_cols_names = [col.replace('-', '_') for col in cols_select_list]
renamed_cols_dict = dict(zip(cols_select_list, new_cols_names))
for item in la_list:
file_path = f'data/epc_csv/epc_{item}.csv'
if os.path.exists(file_path):
# Optimised query which implements predicate pushdown for each file
# Polars optimises the query to make it fast and efficient
q = (
pl.scan_csv(file_path,
dtypes = cols_schema,
encoding='utf8-lossy', # or trips up with odd characters
ignore_errors=True
)
# grouping the data by uprn and selecting the last lodgement date
# to remove old duplicates
.select(pl.col(cols_select_list))
.with_columns(pl.col('lodgement-datetime').str.to_datetime(format='%Y-%m-%d %H:%M:%S', strict=False))
.sort(pl.col(['uprn', 'lodgement-datetime']))
.group_by('uprn').last()
.rename(renamed_cols_dict)
)
all_lazyframes.append(q)
# Concatenate list of lazyframes into one consolidated DF, then collect all at once - FAST
certs_df = pl.concat(pl.collect_all(all_lazyframes)) # faster than intermediate step
return certs_df
#%%
all_dom_epc_raw = ingest_dom_certs_csv(la_list, cols_schema_adjusted)
#%%
all_dom_epc_raw.write_parquet('data/all_domestic_epc.parquet')
#%%
all_dom_epc_raw = pl.read_parquet('data/all_domestic_epc.parquet')
all_dom_epc_raw.glimpse()
#%%
def wrangle_epc(certs_df: pl.DataFrame) -> pl.DataFrame:
"""
Creates date artefacts and changes data types
"""
wrangled_df = (
certs_df
.with_columns([pl.col('lodgement_datetime')
.dt.date()
.alias('date')])
.with_columns([pl.col('date').dt.year().alias('year'),
pl.col('date').dt.month().cast(pl.Int16).alias('month'),
pl.col('date').cast(pl.Utf8)])
# .rename(certs_df_names)
.filter(~pl.col('uprn').is_duplicated()) # some nulls and duplicates (~134) so remove
.drop('lodgement_datetime'))
return wrangled_df
# %%
wrangled_certs = wrangle_epc(all_dom_epc_raw)
#%%
wrangled_certs.glimpse()
#%%
lep_codes = (ca_la_df
.filter(pl.col('ladnm').is_in(['Bristol, City of', 'South Gloucestershire', 'North Somerset', 'Bath and North East Somerset']))
.select('ladcd')
).to_series()
#%%
#check expected number of certificates
lep_epc_dom_df = (wrangled_certs
.filter(pl.col('local_authority').is_in(lep_codes))
)
#%%
del wrangled_certs, all_dom_epc_raw
#%%
lep_epc_dom_df.write_parquet('data/lep_epc_domestic.parquet')
#%%
lep_epc_dom_df = pl.read_parquet('data/lep_epc_domestic.parquet')
#%%
sample_certs = lep_epc_dom_df.sample(n = 10e3)
#%%
def clean_tenure(expr: pl.Expr, new_colname: str) -> pl.Expr:
''''
Function for cleaning the tenure column
The column is piped into this function
In a .with_columns context
'''
return (pl.when(expr.str.contains('wner'))
.then(pl.lit('Owner occupied'))
.when(expr.str.contains('rivate'))
.then(pl.lit('Private rented'))
.when(expr.str.contains('ocial'))
.then(pl.lit('Social rented'))
.otherwise(pl.lit('Unknown'))
.alias(new_colname))
#%%
# test df to capture all permutations of the construction age band
conbands = (sample_certs
.select(pl.col('construction_age_band')).unique()
)
#%%
#%%
def make_n_construction_age(df: pl.DataFrame, new_colname: str) -> pl.DataFrame:
'''
Take a dataframe and create a new column with the nominal construction date
Creating temporary columns and then dropping them
Return the dataframe with the new column
'''
return (df
.with_columns(pl.col('construction_age_band')
.str.extract_all(r'[0-9]')
.list.join('')
.alias('age_char'))
.with_columns(pl.col('age_char').str.len_chars().gt(4).alias('_8_chars'))
.with_columns(pl.when((pl.col('_8_chars').ne(True)) & (pl.col('age_char') == "1900"))
.then(pl.lit(1899))
.otherwise(pl.col('age_char').str.slice(0, 4))
.alias('lower')
.cast(pl.Int16, strict=False)
)
.with_columns(pl.when(pl.col('_8_chars'))
.then(pl.col('age_char').str.slice(4, 8))
.otherwise(None)
.alias('upper')
.cast(pl.Int16, strict=False)
)
.with_columns(pl.when(pl.col('upper').is_not_null())
.then(((pl.col('upper') - pl.col('lower')) // 2) + pl.col('lower'))
.when(pl.col('lower').is_nan())
.then(None)
.otherwise(pl.col('lower'))
.alias(new_colname))
.drop('age_int', '_8_chars', 'age_char', 'lower', 'upper')
)
#%%
test_out = sample_certs.pipe(make_n_construction_age, 'construction_age')
test_out.glimpse()
#%%
t_certs = (sample_certs
.with_columns(
# pipe the pl.col('tenure') expression into the clean_tenure function
pl.col('tenure').pipe(clean_tenure, 'tenure_clean')
)
)