Skip to content

Commit bf34663

Browse files
committed
Add megacounty map
1 parent 776b24b commit bf34663

File tree

2 files changed

+10
-40
lines changed

2 files changed

+10
-40
lines changed

doctor_visits/delphi_doctor_visits/geo_maps.py

+8-34
Original file line numberDiff line numberDiff line change
@@ -165,37 +165,11 @@ def county_to_megacounty(self, data, threshold_visits, threshold_len):
165165
recent_visits_df, how="left", on=["ServiceDate", "PatCountyFIPS"]
166166
)
167167

168-
# mark date-fips points to exclude if we see less than threshold visits that day
169-
data["ToExclude"] = data["RecentVisits"] < threshold_visits
170-
171-
# now to convert to megacounties
172-
state_map = pd.read_csv(
173-
join(self.geo_filepath, "02_20_uszips.csv"),
174-
usecols=["fips", "state_id"],
175-
dtype={"state_id": str},
176-
converters={"fips": GeoMaps.convert_fips},
177-
)
178-
state_map.drop_duplicates(inplace=True)
179-
data = data.merge(
180-
state_map, how="left", left_on="PatCountyFIPS", right_on="fips"
181-
)
182-
# drops rows with no matches, which should not be many
183-
data.dropna(inplace=True)
184-
data.drop(columns=["fips"], inplace=True)
185-
data["StateFIPS"] = data["PatCountyFIPS"].apply(
186-
lambda f: str((int(f) // 1000) * 1000).zfill(5)
187-
)
188-
189-
megacounty_df = (
190-
data[data["ToExclude"]]
191-
.groupby(["ServiceDate", "StateFIPS"])
192-
.sum()
193-
.reset_index()
194-
)
195-
megacounty_df["ToExclude"] = False
196-
megacounty_df.rename(columns={"StateFIPS": "PatCountyFIPS"}, inplace=True)
197-
198-
result = pd.concat([data, megacounty_df])
199-
result.drop(columns=["StateFIPS", "state_id"], inplace=True)
200-
201-
return result.groupby("PatCountyFIPS"), "PatCountyFIPS"
168+
data = self.gmpr.fips_to_megacounty(data,
169+
threshold_visits,
170+
threshold_len,
171+
fips_col="PatCountyFIPS",
172+
thr_col="RecentVisits",
173+
date_col="ServiceDate")
174+
data.rename({"megafips": "PatCountyFIPS"}, axis=1, inplace=True)
175+
return data.groupby("PatCountyFIPS"), "PatCountyFIPS"

doctor_visits/tests/test_geomap.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@ def test_county_to_hrr(self):
4242

4343
def test_county_to_megacounty(self):
4444

45-
out, name = GM.county_to_megacounty(DATA, 10, 10)
45+
out, name = GM.county_to_megacounty(DATA, 100000, 10)
4646
assert name == "PatCountyFIPS"
4747
assert set(out.groups.keys()) == {
4848
"01001",
49-
"01003",
5049
"01005",
51-
"01007",
5250
"01009",
53-
"01011",
54-
"01013",
5551
"01015",
56-
"01017",
52+
"01000"
5753
}

0 commit comments

Comments
 (0)