generated from UBICenter/analysis-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathACS_child_allowance_all_states.py
183 lines (172 loc) · 6.24 KB
/
ACS_child_allowance_all_states.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
import numpy as np
import pandas as pd
import microdf as mdf
from pandas.core.reshape.concat import concat
import requests
import plotly.graph_objects as go
import plotly.figure_factory as ff
url = "https://raw.githubusercontent.com/UBICenter/local-child-allowance/main/data/puma_upper_lower_leg_district.csv"
res = requests.get(url, allow_redirects=True)
with open("puma_upper_lower_leg_district.csv", "wb") as file:
file.write(res.content)
puma_upper_lower_leg_district = pd.read_csv("puma_upper_lower_leg_district.csv")
person2018 = pd.read_stata(
"spm_2018_pu.dta",
columns=[
"spm_id",
"puma",
"spm_povthreshold",
"spm_resources",
"age",
"spm_totval",
"st",
"wt",
],
)
person2018["year"] = 2018
person2017 = pd.read_stata(
"spm_2017_pu.dta",
columns=[
"spm_id",
"puma",
"spm_povthreshold",
"spm_resources",
"age",
"spm_totval",
"st",
"wt",
],
)
person2017["year"] = 2017
person2016 = pd.read_stata(
"spm_2016_pu.dta",
columns=[
"spm_id",
"puma",
"spm_povthreshold",
"spm_resources",
"age",
"spm_totval",
"st",
"wt",
],
)
person2016["year"] = 2016
years_frame = [person2018, person2017, person2016]
def pov(data):
base = mdf.poverty_rate(data, "spm_resources", "spm_povthreshold", "wt")
reform = mdf.poverty_rate(data, "new_resources", "spm_povthreshold", "wt")
deep_base = mdf.deep_poverty_rate(data, "spm_resources", "spm_povthreshold", "wt")
deep_reform = mdf.deep_poverty_rate(data, "new_resources", "spm_povthreshold", "wt")
return pd.Series(
{
"poverty_base": base,
"poverty_reform": reform,
"deep_poverty_base": deep_base,
"deep_poverty_reform": deep_reform,
}
)
def pct_chg(data):
return (data["poverty_base"] - data["poverty_reform"]) / data["poverty_base"]
def pct_chg_deep(data):
return (data["deep_poverty_base"] - data["deep_poverty_reform"]) / data[
"deep_poverty_base"
]
person = pd.concat(years_frame)
puma_upper_lower_leg_district.puma = puma_upper_lower_leg_district.puma.astype(int)
puma_upper_lower_leg_district.puma = puma_upper_lower_leg_district.puma.astype(
str
).str.zfill(5)
puma_upper_lower_leg_district["state_puma"] = (
puma_upper_lower_leg_district["state_fip"].astype(str)
+ puma_upper_lower_leg_district["puma"]
)
person.rename(columns={"puma": "state_puma"}, inplace=True)
# DC and collection error
person_clean = person[(person.st != 11) & (person.st != 99)]
person_clean["child"] = np.where(person_clean["age"] < 18, 1, 0)
spmu = (
person_clean.groupby(["spm_id", "spm_resources", "year"])[["child"]]
.sum()
.reset_index()
)
spmu.rename(columns={"child": "spmu_children"}, inplace=True)
ANNUAL_CHILD_ALLOWANCE = 1200
spmu["new_resources"] = spmu.spm_resources + ANNUAL_CHILD_ALLOWANCE * spmu.spmu_children
person_clean3 = person_clean.merge(
spmu[["spm_id", "new_resources", "year"]], on=["year", "spm_id"]
)
pov_by_state = person_clean3.groupby(["st"]).apply(pov).reset_index()
pov_by_state["pct_chg"] = pct_chg(pov_by_state)
pov_by_state["pct_chg_deep"] = pct_chg_deep(pov_by_state)
state_names = pd.read_csv("us_state_fips.csv")
state_names.columns
state_names.rename(columns={" st": "st", " stusps": "stusps",}, inplace=True)
person_clean3.rename(columns={"puma": "state_puma"}, inplace=True)
person_clean3_kids = person_clean3[(person_clean3.child == 1)]
person_clean3_CA = person_clean3[(person_clean3.st == 6)]
person_clean3_CA_kids = person_clean3_CA[(person_clean3.child == 1)]
person_clean3.state_puma = person_clean3.state_puma.astype(int)
person_clean3.state_puma = person_clean3.state_puma.astype(str)
puma_upper_lower_leg_district.state_puma = puma_upper_lower_leg_district.state_puma.astype(
int
)
person_merged = puma_upper_lower_leg_district.merge(person_clean3, on="state_puma")
person_merged.wt *= person_merged.share_of_puma_pop
person_merged_only_children = person_merged[(person_merged.child == 1)]
pov_by_lower_leg_district = (
person_merged.groupby(["st", "lower_leg_district"]).apply(pov).reset_index()
)
pov_by_lower_leg_district_children = (
person_merged_only_children.groupby(["st", "lower_leg_district"])
.apply(pov)
.reset_index()
)
pov_by_upper_leg_district = (
person_merged.groupby(["st", "upper_leg_district"]).apply(pov).reset_index()
)
pov_by_upper_leg_district_children = (
person_merged_only_children.groupby(["st", "upper_leg_district"])
.apply(pov)
.reset_index()
)
pov_by_lower_leg_district["pct_chg"] = pct_chg(pov_by_lower_leg_district)
pov_by_lower_leg_district["pct_chg_deep"] = pct_chg_deep(pov_by_lower_leg_district)
pov_by_lower_leg_district_children["pct_chg"] = pct_chg(
pov_by_lower_leg_district_children
)
pov_by_lower_leg_district_children["pct_chg_deep"] = pct_chg_deep(
pov_by_lower_leg_district_children
)
pov_by_upper_leg_district["pct_chg"] = pct_chg(pov_by_upper_leg_district)
pov_by_upper_leg_district["pct_chg_deep"] = pct_chg_deep(pov_by_upper_leg_district)
pov_by_upper_leg_district_children["pct_chg"] = pct_chg(
pov_by_upper_leg_district_children
)
pov_by_upper_leg_district_children["pct_chg_deep"] = pct_chg_deep(
pov_by_upper_leg_district_children
)
pov_by_lower_leg_district_children.columns = [
"child_" + i for i in pov_by_lower_leg_district_children.columns
]
pov_by_upper_leg_district_children.columns = [
"child_" + i for i in pov_by_upper_leg_district_children.columns
]
pov_by_lower_leg_district_children.rename(
columns={"child_lower_leg_district": "lower_leg_district", "child_st": "st"},
inplace=True,
)
pov_by_upper_leg_district_children.rename(
columns={"child_upper_leg_district": "upper_leg_district", "child_st": "st"},
inplace=True,
)
pov_by_lower_leg_district_children
pov_by_lower_leg_district
pov_all_lower_leg_district = pov_by_lower_leg_district.merge(
pov_by_lower_leg_district_children, on=["st", "lower_leg_district"]
)
pov_all_upper_leg_district = pov_by_upper_leg_district.merge(
pov_by_upper_leg_district_children, on=["st", "upper_leg_district"]
)
pov_all_lower_leg_district.to_csv(r"pov_all_lower_leg_district.csv")
pov_all_upper_leg_district.to_csv(r"pov_all_upper_leg_district.csv")