-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_inyear_data.py
383 lines (339 loc) · 11.8 KB
/
process_inyear_data.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# %%
# !/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Purpose
Read in an in-year release of OSCAR data and process it so it can be added
to our aggregated OSCAR data
Inputs
- csv: 'OSCAR_in_year_dataset_June_2023.csv'
- pkl: 'oscar_2021_2022_annual.pkl'
Outputs
- pkl: 'oscar_2022_2023_inyear_june_2023.pkl'
- xlsx: 'oscar_2022_2023_inyear_june_2023.xlsx'
Parameters
- column_renamings: Dictionary of column names used in the in-year data,
and the renaming we want to apply
- columns_to_drop: List of columns we want to drop from the in-year data
- collated_data_columns: List of columns in the collated OSCAR data
Notes
- We exclude non-budget, non-voted spend, as per previous Whitehall
Monitor analysis
'''
import os
import pandas as pd
from pandas.io.formats import excel
# %%
# DEFINE PARAMETERS
column_renamings = {
'Economic Budget Code': 'PESA_ECONOMIC_BUDGET_CODE',
'PESA Economic Group Code': 'PESA_ECONOMIC_GROUP_CODE',
'Control Budget Code': 'CONTROL_BUDGET_L0_LONG_NAME',
'Control Budget Detail Code': 'CONTROL_BUDGET_L1_LONG_NAME',
'Economic Category Long Name': 'ECONOMIC_CATEGORY_LONG_NAME',
'Economic Category Code': 'ECONOMIC_CATEGORY_CODE',
'Amount': 'AMOUNT',
'Segment Department Long Name': 'DEPARTMENT_GROUP_LONG_NAME',
'Organisation Code': 'ORGANISATION_CODE',
'Organisation': 'ORGANISATION_LONG_NAME',
'Year': 'Financial_Year',
}
columns_to_drop = [
'Quarter',
'Month'
]
collated_data_columns = [
'PESA_ECONOMIC_BUDGET_CODE',
'PESA_ECONOMIC_GROUP_CODE',
'CONTROL_BUDGET_L0_LONG_NAME',
'CONTROL_BUDGET_L1_LONG_NAME',
'ECONOMIC_CATEGORY_LONG_NAME',
'ECONOMIC_CATEGORY_CODE',
'ACCOUNTING_AUTHORITY_L0_CODE',
'ECONOMIC_GROUP_LONG_NAME',
'INCOME_CATEGORY_SHORT_NAME',
'CHART_OF_ACCOUNTS_L5_LONG_NAME',
'BUDGETING_ORGANISATIONS_CODE',
'DEPARTMENT_GROUP_CODE',
'DEPARTMENT_GROUP_LONG_NAME',
'ORGANISATION_CODE',
'ORGANISATION_LONG_NAME',
'ORGANISATION_TYPE_L1_LONG_NAME',
'ORGANISATION_TYPE_L1_CODE',
'Financial_Year',
'Checked_Organisation_Name',
'IfG_Organisation_Type',
'IfG_Organisation_Status',
'AMOUNT',
'Added',
]
control_budget_l0_replacements = {
'TOTAL DEL': 'DEPARTMENTAL EXPENDITURE LIMITS',
'TOTAL AME': 'ANNUALLY MANAGED EXPENDITURE'
}
control_budget_l1_replacements = {
'DEL PROG': 'DEPARTMENTAL EXPENDITURE LIMITS PROGRAMME',
'DEL ADMIN': 'DEPARTMENTAL EXPENDITURE LIMITS ADMINISTRATION',
'DEPT AME': 'DEPARTMENTAL ANNUALLY MANAGED EXPENDITURE',
'NON-DEPT AME': 'NON-DEPARTMENTAL ANNUALLY MANAGED EXPENDITURE',
}
# %%
# READ IN IN-YEAR OSCAR DATA
file_path_inyear = (
'C:/Users/' + os.getlogin() + '/'
'Institute for Government/' +
'Data - General/' +
'Public finances/OSCAR/' +
'Source/In-year data'
)
file_name_inyear = 'OSCAR_in_year_dataset_June_2023.csv'
df_inyear = pd.read_csv(file_path_inyear + '/' + file_name_inyear, encoding='cp1252')
# %%
# RUN CHECKS ON DATA
# Check column names are as expected
assert set(df_inyear.columns) == set(
[
'Organisation Code',
'Organisation',
'Control Budget Code',
'Control Budget Detail Code',
'Segment Department Long Name',
'Sub Segment Code',
'Sub Segment Long Name',
'Economic Category Code',
'Economic Category Long Name',
'Economic Budget Code',
'PESA Economic Group Code',
'Version',
'Year',
'Quarter',
'Month',
'Amount',
]
), 'Column names not as expected'
# %%
# Check there's only one Version
assert len(df_inyear['Version'].unique()) == 1, 'More than one Version in in-year data'
# %%
# Check that for any one organisation, only one organisation code is used
assert df_inyear.groupby('Organisation')['Organisation Code'].nunique().max() == 1, \
'More than one organisation code for an organisation'
# %%
# EDIT DATA
# Rename columns
df_inyear = df_inyear.rename(columns=column_renamings)
# %%
# Drop columns we don't need
df_inyear = df_inyear.drop(columns=columns_to_drop)
# %%
# Exclude non-budget, non-voted spend
# NB: Note that these differ from what we get in the full year data - the values
# we get in the CONTROL_BUDGET_L0_LONG_NAME column differ and we don't
# have a ACCOUNTING_AUTHORITY_L0_CODE column here
df_inyear = df_inyear[
(df_inyear['CONTROL_BUDGET_L0_LONG_NAME'] != 'TOTAL NON-BUDGET') &
~(df_inyear['Sub Segment Long Name'].str.upper().str.contains('NON-VOTED')) &
~(df_inyear['Sub Segment Long Name'].str.upper().str.contains('NON VOTED'))
]
# %%
# Reorder columns
df_inyear = df_inyear[
[
'PESA_ECONOMIC_BUDGET_CODE',
'PESA_ECONOMIC_GROUP_CODE',
'CONTROL_BUDGET_L0_LONG_NAME',
'CONTROL_BUDGET_L1_LONG_NAME',
'ECONOMIC_CATEGORY_LONG_NAME',
'ECONOMIC_CATEGORY_CODE',
'AMOUNT',
'DEPARTMENT_GROUP_LONG_NAME',
'ORGANISATION_CODE',
'ORGANISATION_LONG_NAME',
'Financial_Year',
'Version',
'Sub Segment Code',
'Sub Segment Long Name'
]
]
# %%
# SUM SPENDING
df_inyear_total = df_inyear.groupby(
[
'PESA_ECONOMIC_GROUP_CODE',
'PESA_ECONOMIC_BUDGET_CODE',
'CONTROL_BUDGET_L0_LONG_NAME',
'CONTROL_BUDGET_L1_LONG_NAME',
'ECONOMIC_CATEGORY_LONG_NAME',
'ECONOMIC_CATEGORY_CODE',
'DEPARTMENT_GROUP_LONG_NAME',
'ORGANISATION_CODE',
'ORGANISATION_LONG_NAME',
'Financial_Year',
'Sub Segment Code',
'Sub Segment Long Name',
'Version',
]
).agg({'AMOUNT': 'sum'}).reset_index()
# %%
# READ IN PREVIOUS OSCAR DATA, FROM WHICH WE'LL PULL IN ORG DETAILS
file_path_previous = (
'C:/Users/' + os.getlogin() + '/'
'Institute for Government/' +
'Data - General/' +
'Public finances/OSCAR/' +
'Scripts/data'
)
file_name_previous = 'oscar_2021_2022_annual.pkl'
df_previous = pd.read_pickle(file_path_previous + '/' + file_name_previous)
# %%
# MERGE IN ORG DETAILS FROM PREVIOUS OSCAR DATA
# NB: merge() is used here rather than combine_first() as these columns don't
# already exist in the data
df_inyear_total_merged = df_inyear_total.merge(
df_previous[[
'ORGANISATION_LONG_NAME',
'ORGANISATION_CODE',
'IfG_Organisation_Type',
'IfG_Organisation_Status',
'Checked_Organisation_Name'
]].drop_duplicates(),
how='left',
on=['ORGANISATION_CODE'],
suffixes=(None, '_previous'),
).drop(columns=['ORGANISATION_LONG_NAME_previous'])
# %%
# CARRY OUT CHECKS ON MERGED DATA
# Check that df_inyear_total_merged has the same number of rows as df_inyear_total
assert len(df_inyear_total_merged) == len(df_inyear_total), \
'df_inyear_total_merged has a different number of rows to df_inyear_total'
# %%
# MERGE IN ORG DETAILS FROM MANUAL CLASSIFICATION
# Read in manual classifications
file_path_manual = (
'C:/Users/' + os.getlogin() + '/'
'Institute for Government/' +
'Data - General/' +
'Public finances/OSCAR/'
)
file_name_manual = 'IfG classification of bodies.xlsx'
df_manual = pd.read_excel(
file_path_manual + '/' + file_name_manual,
sheet_name='2023',
usecols=[
'New organisation name',
'Name to use',
'Organisation type',
'Organisation status',
]
)
df_manual = df_manual.rename(
columns={
'New organisation name': 'ORGANISATION_LONG_NAME',
'Name to use': 'Checked_Organisation_Name',
'Organisation type': 'IfG_Organisation_Type',
'Organisation status': 'IfG_Organisation_Status',
}
)
# %%
# Merge in manual classifications
# NB: combine_first() is used here rather than merge() as these columns already
# exist in the data, and rather than fillna() as this doesn't rely on
# us having a unique index
# NB: We can't use inplace=True, as combine_first() would return None, which we
# wouldn't be able to reset the index on
df_inyear_total_merged = df_inyear_total_merged.set_index('ORGANISATION_LONG_NAME').combine_first(
df_manual[[
'ORGANISATION_LONG_NAME',
'IfG_Organisation_Type',
'IfG_Organisation_Status',
'Checked_Organisation_Name'
]].drop_duplicates().set_index('ORGANISATION_LONG_NAME'),
).reset_index()
# %%
# Reset order of columns, which is lost by combine_first()
df_inyear_total_merged = df_inyear_total_merged[
[c for c in df_inyear_total.columns if c not in ['Version', 'AMOUNT']] + [
'IfG_Organisation_Type',
'IfG_Organisation_Status',
'Checked_Organisation_Name',
'Version',
'AMOUNT'
]
]
# %%
# Drop rows where AMOUNT is NaN
# NB: We need to do this as combine_first() will have created rows where
# an organisation exists in df_manual, even where it doesn't exist in
# df_inyear_total_merged
df_inyear_total_merged = df_inyear_total_merged[
df_inyear_total_merged['AMOUNT'].notna()
]
# %%
# CARRY OUT CHECKS ON MERGED DATA
# Check that df_inyear_total_merged has the same number of rows as df_inyear_total
assert len(df_inyear_total_merged) == len(df_inyear_total), \
'df_inyear_total_merged has a different number of rows to df_inyear_total'
# %%
# EDIT DATA
# Add a column to indicate when the data was added
df_inyear_total_merged['Added'] = pd.to_datetime('today').date()
# %%
# Bring CONTROL_BUDGET_L0_LONG_NAME, CONTROL_BUDGET_L1_LONG_NAME values in line with
# those in collated data
df_inyear_total_merged['CONTROL_BUDGET_L0_LONG_NAME'].replace(
control_budget_l0_replacements,
inplace=True
)
df_inyear_total_merged['CONTROL_BUDGET_L1_LONG_NAME'].replace(
control_budget_l1_replacements,
inplace=True
)
# %%
# CARRY OUT FINAL CHECKS ON DATA
# Check that no bodies have IfG_Organisation_Status, Checked_Organisation_Name or
# IfG_Organisation_Type featuring 'CHECK'
assert df_inyear_total_merged[
df_inyear_total_merged['Checked_Organisation_Name'].str.contains('CHECK')
].empty, 'Found records with Checked_Organisation_Name featuring the string "CHECK"'
assert df_inyear_total_merged[
df_inyear_total_merged['IfG_Organisation_Type'].str.contains('CHECK')
].empty, 'Found records with IfG_Organisation_Type featuring the string "CHECK"'
assert df_inyear_total_merged[
df_inyear_total_merged['IfG_Organisation_Status'].str.contains('CHECK')
].empty, 'Found records with IfG_Organisation_Status featuring the string "CHECK"'
# Check that no bodies have IfG_Organisation_Status, Checked_Organisation_Name or
# IfG_Organisation_Type of NaN
assert df_inyear_total_merged['Checked_Organisation_Name'].isna().sum() == 0, \
'NaN values in Checked_Organisation_Name column'
assert df_inyear_total_merged['IfG_Organisation_Status'].isna().sum() == 0, \
'NaN values in IfG_Organisation_Status column'
assert df_inyear_total_merged['IfG_Organisation_Type'].isna().sum() == 0, \
'NaN values in IfG_Organisation_Type column'
# %%
# SAVE DATA TO PICKLE
file_path_pickle_output = (
'C:/Users/' + os.getlogin() + '/'
'Institute for Government/' +
'Data - General/' +
'Public finances/OSCAR/' +
'Scripts/data'
)
file_name_pickle_output = 'oscar_2022_2023_inyear_june_2023.pkl'
df_inyear_total_merged.to_pickle(file_path_pickle_output + '/' + file_name_pickle_output)
# %%
# ADD EMPTY COLUMNS TO MATCH THOSE IN COLLATED DATA AND REORDER
df_inyear_total_merged = df_inyear_total_merged.reindex(columns=collated_data_columns)
df_inyear_total_merged = df_inyear_total_merged[collated_data_columns]
# %%
# SAVE DATA TO EXCEL
file_path_excel_output = (
'C:/Users/' + os.getlogin() + '/'
'Institute for Government/' +
'Data - General/' +
'Public finances/OSCAR/' +
'Scripts/temp'
)
file_name_excel_output = 'oscar_2022_2023_inyear_june_2023.xlsx'
excel.ExcelFormatter.header_style = None
df_inyear_total_merged.to_excel(file_path_excel_output + file_name_excel_output, index=False)
# %%