/
pull_soi_proprietorship.py
399 lines (360 loc) · 13.3 KB
/
pull_soi_proprietorship.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"""
SOI Proprietorship Data (pull_soi_partner.py):
------------------------------------------------------------------------
Module that handles reading in the soi proprietorship data. Because no
fixed asset and land data is available for sole props, the depreciation
deduction is used along with the partner data. The ratio of land and
fixed assets to the depreciation deduction for partners is used to
impute the data for sole props. The sole prop inventory and farm data
is also loaded in.
"""
import re
import numpy as np
import pandas as pd
from ccc.utils import to_str
import pull_soi_partner as prt
from data_paths import get_paths
globals().update(get_paths())
_DDCT_FILE_FCTR = 10**3
def load_proprietorship_data(entity_dfs):
"""
Using the partnership and sole prop data, the capital stock data is
imputed.
Args:
entity_dfs (dictioinary): Dictionary of DataFrames of SOI data
by industry
Returns:
data (dictionary): Dictionary with a DataFrame of SOI sole prop
capital stock data organized by industry
"""
# Opening data on depreciable fixed assets, inventories, and land
# for non-farm sole props
nonfarm_df = format_dataframe(
pd.read_excel(_NFARM_PATH, skiprows=2, skipfooter=8)
)
# Cuts off the repeated columns so only the data for all sole props
# remains
nonfarm_df = nonfarm_df.T.groupby(sort=False, level=0).first().T
# Fixing the index labels of the new dataframe
nonfarm_df.reset_index(inplace=True, drop=True)
# Keep only variables of interest
nonfarm_df = nonfarm_df[["Industry", "Depreciation deduction [1,2]"]]
nonfarm_df["Industry"] = nonfarm_df["Industry"].apply(
lambda x: re.sub(r"[\s+]", "", x)
)
nonfarm_df.rename(
columns={
"Industry": "Item",
"Depreciation deduction [1,2]": "Depreciation",
},
inplace=True,
)
# Opens the nonfarm inventory data
nonfarm_inv = prt.format_excel(
pd.read_excel(_NFARM_INV, skiprows=1, skipfooter=8)
)
# Cuts off the repeated columns so only the data for all sole props remains
nonfarm_inv = nonfarm_inv.T.groupby(sort=False, level=0).first().T
nonfarm_inv.columns = [to_str(c) for c in nonfarm_inv.columns]
# Fixing the index labels of the new dataframe
nonfarm_inv.reset_index(inplace=True, drop=True)
# Keep only variables of interest
nonfarm_inv = nonfarm_inv[
["Net income status, item", "Inventory, end of year"]
]
nonfarm_inv["Net income status, item"] = nonfarm_inv[
"Net income status, item"
].str.strip()
nonfarm_inv.rename(
columns={
"Net income status, item": "Item",
"Inventory, end of year": "Inventories",
},
inplace=True,
)
nonfarm_inv["Item"] = nonfarm_inv["Item"].apply(
lambda x: re.sub(r"[\s+]", "", x)
)
# merge together two sole prop data sources
# have to manually fix a couple names to be compatible
nonfarm_df.loc[
nonfarm_df["Item"]
== "Otherambulatoryhealthcareservices(includingambulanceservices,bloodandorganbanks)",
"Item",
] = "Otherambulatoryhealthcareservices(includingambulanceservices,blood,organbanks)"
nonfarm_df.loc[
nonfarm_df["Item"]
== "Officesofrealestateagents,brokers,propertymanagers,andappraisers",
"Item",
] = "Officesofrealestateagents,brokers,propertymanagersandappraisers"
nonfarm_df.loc[
nonfarm_df["Item"]
== "OOtherautorepairandmaintenance(includingoilchange,lubrication,andcarwashes)",
"Item",
] = "Otherautorepairandmaintenance(includingoilchange,lube,andcarwashes)"
nonfarm_df = nonfarm_df.merge(
nonfarm_inv, how="inner", on=["Item"], copy=True
)
# read in crosswalk for these data
xwalk = pd.read_csv(_DETAIL_SOLE_PROP_CROSS_PATH)
# keep only codes that help to identify complete industries
xwalk = xwalk[xwalk["complete"] == 1]
xwalk = xwalk[["Industry", "INDY_CD"]]
xwalk["Industry"] = xwalk["Industry"].apply(
lambda x: re.sub(r"[\s+]", "", x)
)
# merge industry codes to sole prop data
nonfarm_df = nonfarm_df.merge(
xwalk, how="inner", left_on=["Item"], right_on=["Industry"], copy=True
)
nonfarm_df.drop(["Item", "Industry"], axis=1, inplace=True)
# Sums together the repeated codes into one industry
nonfarm_df = nonfarm_df.groupby("INDY_CD", sort=False).sum()
nonfarm_df.reset_index(inplace=True)
# add some rows for industry codes not in the sole prop data because
# they are zero
df = pd.DataFrame(columns=("INDY_CD", "Depreciation", "Inventories"))
missing_code_list = [
312,
517,
519,
524140,
524142,
524143,
524156,
524159,
55,
521,
525,
531115,
]
for i in range(len(missing_code_list)):
df.loc[i] = [int(missing_code_list[i]), 0.0, 0.0]
nonfarm_df = (
nonfarm_df.append(df, sort=True, ignore_index=True)
.copy()
.reset_index()
)
# attribute over a minor industry only idenfified in w/ other minor
# ind in SOI
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531115, "Depreciation"] = (
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531135, "Depreciation"].values
* 0.5
)
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531115, "Inventories"] = (
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531135, "Inventories"].values
* 0.5
)
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531135, "Depreciation"] = (
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531135, "Depreciation"].values
* 0.5
)
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531135, "Inventories"] = (
nonfarm_df.loc[nonfarm_df["INDY_CD"] == 531135, "Inventories"].values
* 0.5
)
# create ratios for minor industry assets using corporate data
# read in crosswalk for bea and soi industry codes
soi_bea_ind_codes = pd.read_csv(
_SOI_BEA_CROSS, dtype={"bea_ind_code": str}
)
soi_bea_ind_codes.drop("notes", axis=1, inplace=True)
# drop one repeated minor ind code in crosswalk
soi_bea_ind_codes.drop_duplicates(subset=["minor_code_alt"], inplace=True)
# merge codes to sole prop data
# likely better way to do this...
nonfarm_sector = nonfarm_df[
(nonfarm_df["INDY_CD"] > 9) & (nonfarm_df["INDY_CD"] < 100)
]
nonfarm_major = nonfarm_df[
(nonfarm_df["INDY_CD"] > 99) & (nonfarm_df["INDY_CD"] < 1000)
]
nonfarm_minor = nonfarm_df[
(nonfarm_df["INDY_CD"] > 99999) & (nonfarm_df["INDY_CD"] < 1000000)
]
sector_df = nonfarm_sector.merge(
soi_bea_ind_codes,
how="inner",
left_on=["INDY_CD"],
right_on=["sector_code"],
copy=True,
indicator=True,
)
major_df = nonfarm_major.merge(
soi_bea_ind_codes,
how="inner",
left_on=["INDY_CD"],
right_on=["major_code"],
copy=True,
indicator=True,
)
minor_df = nonfarm_minor.merge(
soi_bea_ind_codes,
how="inner",
left_on=["INDY_CD"],
right_on=["minor_code"],
copy=True,
indicator=True,
)
nonfarm_data = (
sector_df.append([major_df, minor_df], sort=True, ignore_index=True)
.copy()
.reset_index()
)
nonfarm_data.drop(
["bea_inv_name", "bea_code", "_merge"], axis=1, inplace=True
)
# merge codes to total part data
# inner join means that we keep only rows that match in both datasets
# this should keep only unique soi minor industries
columns = ["Inventories", "Depreciation"]
part_data = entity_dfs["part_data"][
["minor_code_alt", "part_type"] + columns + ["Land", "Fixed Assets"]
].copy()
# sum at industry-partner type level
part_data = part_data.groupby(["minor_code_alt"]).sum().reset_index()
part2 = part_data[["minor_code_alt"] + columns].copy()
partner = part2.merge(
soi_bea_ind_codes,
how="inner",
on=["minor_code_alt"],
suffixes=("_x", "_y"),
copy=True,
)
for var in columns:
partner[var + "_ratio"] = partner.groupby(["major_code"])[var].apply(
lambda x: x / float(x.sum())
)
partner.drop(
["bea_inv_name", "bea_code", "sector_code", "minor_code"] + columns,
axis=1,
inplace=True,
)
# merge these ratios to the sole prop data
nonfarm = nonfarm_data.merge(
partner,
how="right",
on=["minor_code_alt"],
suffixes=("_x", "_y"),
copy=True,
indicator=True,
)
# filling in missing values. This works ok now but need to be
# careful as the ratio value could cause problems
nonfarm["Inventories"].fillna(value=0.0, axis=0, inplace=True)
nonfarm["Inventories_ratio"].fillna(value=1.0, axis=0, inplace=True)
# allocate capital based on ratios
for var in columns:
nonfarm.loc[nonfarm["INDY_CD"] > 99999, var + "_ratio"] = 1.0
nonfarm[var] = nonfarm[var] * nonfarm[var + "_ratio"]
nonfarm.drop(list(x + "_ratio" for x in columns), axis=1, inplace=True)
nonfarm.drop(
[
"index",
"sector_code",
"major_code_x",
"minor_code",
"major_code_y",
"_merge",
],
axis=1,
inplace=True,
)
# data here totals out for allocable industries (so doesn't hit
# SOI totals for all industries bc some not allocated to an industry)
# merge in partner data to get ratios need to impute FA's and land
part_ratios = part_data[
["minor_code_alt", "Fixed Assets", "Depreciation", "Land"]
].copy()
part_ratios["FA_ratio"] = (
part_ratios["Fixed Assets"] / part_ratios["Depreciation"]
)
part_ratios["Land_ratio"] = (
part_ratios["Land"] / part_ratios["Fixed Assets"]
)
part_ratios = part_ratios[["minor_code_alt", "FA_ratio", "Land_ratio"]]
nonfarm = nonfarm.merge(
part_ratios,
how="inner",
on=["minor_code_alt"],
suffixes=("_x", "_y"),
copy=True,
indicator=False,
)
# need to find ratio of assets from BEA to SOI
bea_ratio = 1.0
nonfarm["Fixed Assets"] = (
nonfarm["FA_ratio"] * nonfarm["Depreciation"] * bea_ratio
)
nonfarm["Land"] = nonfarm["Land_ratio"] * nonfarm["Fixed Assets"]
nonfarm.drop(["Land_ratio", "FA_ratio"], axis=1, inplace=True)
# Calculates the FA and Land for Farm sole proprietorships.
# Note: we should update so read in raw Census of Agriculture
# What about inventories for farm sole props? Worry about??
farm_df = pd.read_csv(_FARM_IN_PATH)
asst_land = farm_df["R_p"][0] + farm_df["Q_p"][0]
land_ratio = np.array(
(
part_data.loc[part_data["minor_code_alt"] == 111, "Land"]
/ (
part_data.loc[
part_data["minor_code_alt"] == 111, "Fixed Assets"
]
+ part_data.loc[part_data["minor_code_alt"] == 111, "Land"]
)
)
)
part_land = land_ratio * asst_land
sp_farm_land = farm_df["A_sp"][0] * part_land / farm_df["A_p"][0]
sp_farm_assts = farm_df["R_sp"][0] + farm_df["Q_sp"][0] - sp_farm_land
sp_farm_cstock = np.array([sp_farm_assts, 0, sp_farm_land])
# Adds farm data to industry 111
nonfarm.loc[nonfarm["INDY_CD"] == 111, "Fixed Assets"] += sp_farm_cstock[0]
nonfarm.loc[nonfarm["INDY_CD"] == 111, "Land"] += sp_farm_cstock[2]
# Creates the dictionary of sector : dataframe that is returned and
# used to update entity_dfs
data = {"sole_prop_data": nonfarm}
return data
def format_columns(nonfarm_df):
"""
Removes extra characters from the columns of the dataframe
Args:
nonfarm_df (DataFrame): nonfarm sole prop capital stock data
Returns:
nonfarm_df (DataFrame): formatted nonfarm sole prop capital
stock data
"""
columns = nonfarm_df.columns.tolist()
for i in range(0, len(columns)):
column = columns[i]
if ".1" in column:
column = column[:-2]
if "\n" in column:
column = column.replace("\n", " ").replace("\r", "")
column = column.rstrip()
columns[i] = column
nonfarm_df.columns = columns
return nonfarm_df
def format_dataframe(nonfarm_df):
"""
Fixes the column headers, drops the unnecessary rows, inserts the
SOI codes and multiplies by a factor of a thousand
Args:
nonfarm_df (DataFrame): nonfarm sole prop capital stock data
Returns:
nonfarm_df (DataFrame): formatted nonfarm sole prop capital
stock data
"""
# Creates a list from the first row of the dataframe
columns = nonfarm_df.iloc[0].tolist()
# Replaces the first item in the list with a new label
columns[0] = "Industry"
# Sets the values of the columns on the dataframes
nonfarm_df.columns = list(to_str(x).replace("\n", " ") for x in columns)
# Drops the first couple of rows and last row in the dataframe
nonfarm_df.dropna(inplace=True)
# Multiplies each value in the dataframe by a factor of 1000
nonfarm_df.iloc[:, 1:] = nonfarm_df.iloc[:, 1:] * _DDCT_FILE_FCTR
# Resets the index values to a normal range after some have been dropped
nonfarm_df.reset_index(inplace=True, drop=True)
return nonfarm_df