Skip to content

Commit 776b24b

Browse files
committed
county to state and hrr
1 parent 32e044b commit 776b24b

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

doctor_visits/delphi_doctor_visits/geo_maps.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ def county_to_state(self, data):
7171
converters={"fips": GeoMaps.convert_fips},
7272
)
7373
state_map.drop_duplicates(inplace=True)
74-
data = data.merge(
75-
state_map, how="left", left_on="PatCountyFIPS", right_on="fips"
76-
)
77-
data.dropna(inplace=True)
78-
data.drop(columns=["PatCountyFIPS", "fips"], inplace=True)
74+
data = self.gmpr.add_geocode(data,
75+
"fips",
76+
"state_id",
77+
from_col="PatCountyFIPS")
78+
data.drop(columns="PatCountyFIPS", inplace=True)
7979
data = data.groupby(["ServiceDate", "state_id"]).sum().reset_index()
8080

8181
return data.groupby("state_id"), "state_id"
@@ -109,15 +109,15 @@ def county_to_hrr(self, data):
109109

110110
hrr_map = hrr_map.melt(["fips"], var_name="hrr", value_name="wpop")
111111
hrr_map = hrr_map[hrr_map["wpop"] > 0]
112-
113-
data = data.merge(hrr_map, how="left", left_on="PatCountyFIPS", right_on="fips")
114-
## drops rows with no matching HRR, which should not be many
115-
data.dropna(inplace=True)
116-
data.drop(columns=["PatCountyFIPS", "fips"], inplace=True)
112+
data = self.gmpr.add_geocode(data,
113+
"fips",
114+
"hrr",
115+
from_col="PatCountyFIPS")
116+
data.drop(columns="PatCountyFIPS", inplace=True)
117117

118118
## do a weighted sum by the wpop column to get each HRR's contribution
119119
tmp = data.groupby(["ServiceDate", "hrr"])
120-
wtsum = lambda g: g["wpop"].values @ g[Config.COUNT_COLS]
120+
wtsum = lambda g: g["weight"].values @ g[Config.COUNT_COLS]
121121
data = tmp.apply(wtsum).reset_index()
122122

123123
return data.groupby("hrr"), "hrr"

doctor_visits/tests/test_geomap.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ def test_county_to_state(self):
3232

3333
out, name = GM.county_to_state(DATA)
3434
assert name == "state_id"
35-
assert set(out.groups.keys()) == set(["AL"])
35+
assert set(out.groups.keys()) == {"al"}
3636

3737
def test_county_to_hrr(self):
3838

3939
out, name = GM.county_to_hrr(DATA)
4040
assert name == "hrr"
41-
assert set(out.groups.keys()) == set(["1", "134", "144", "2", "6", "7", "9"])
41+
assert set(out.groups.keys()) == {"1", "134", "144", "146", "2", "5", "6", "7", "9"}
4242

4343
def test_county_to_megacounty(self):
4444

4545
out, name = GM.county_to_megacounty(DATA, 10, 10)
4646
assert name == "PatCountyFIPS"
47-
assert set(out.groups.keys()) == set(
48-
[
47+
assert set(out.groups.keys()) == {
4948
"01001",
5049
"01003",
5150
"01005",
@@ -55,5 +54,4 @@ def test_county_to_megacounty(self):
5554
"01013",
5655
"01015",
5756
"01017",
58-
]
59-
)
57+
}

0 commit comments

Comments
 (0)