Skip to content

Commit 7aa680a

Browse files
authored
Merge pull request #400 from cmu-delphi/fix_geomapper_popcol
Fix geomapper add_population_column function
2 parents af0b3fb + c1fff63 commit 7aa680a

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

_delphi_utils_python/delphi_utils/geomap.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,17 @@ def replace_geocode(
392392
df = df.groupby([date_col, new_col]).sum().reset_index()
393393
return df
394394

395-
def add_population_column(self, geocode_type, data=None, geocode_col=None, dropna=True):
395+
def add_population_column(self, data, geocode_type, geocode_col=None, dropna=True):
396396
"""
397397
Appends a population column to a dataframe, based on the FIPS or ZIP code. If no
398398
dataframe is provided, the full crosswalk from geocode to population is returned.
399399
400400
Parameters
401401
---------
402-
geocode_type: {"fips", "zip"}
403-
The type of the geocode contained in geocode_col.
404402
data: pd.DataFrame
405403
The dataframe with a FIPS code column.
404+
geocode_type: {"fips", "zip"}
405+
The type of the geocode contained in geocode_col.
406406
geocode_col: str, default None
407407
The name of the column containing the geocodes. If None, uses the geocode_type
408408
as the name.
@@ -413,6 +413,7 @@ def add_population_column(self, geocode_type, data=None, geocode_col=None, dropn
413413
A dataframe with a population column appended.
414414
"""
415415
geocode_col = geocode_type if geocode_col is None else geocode_col
416+
data = data.copy()
416417

417418
if geocode_type not in ["fips", "zip"]:
418419
raise ValueError(
@@ -422,15 +423,12 @@ def add_population_column(self, geocode_type, data=None, geocode_col=None, dropn
422423

423424
pop_df = self._load_crosswalk(from_code=geocode_type, to_code="pop")
424425

425-
if data is None:
426-
return pop_df.rename(columns={"pop": "population"})
427-
428426
if not is_string_dtype(data[geocode_col]):
429427
data[geocode_col] = data[geocode_col].astype(str).str.zfill(5)
430428

431-
merge_type = "left" if dropna else "inner"
429+
merge_type = "inner" if dropna else "left"
432430
data_with_pop = (
433-
data.copy()
431+
data
434432
.merge(pop_df, left_on=geocode_col, right_on=geocode_type, how=merge_type)
435433
.rename(columns={"pop": "population"})
436434
)

_delphi_utils_python/tests/test_geomap.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,12 @@ def test_zip_to_state_id(self):
277277

278278
def test_add_population_column(self):
279279
gmpr = GeoMapper()
280-
new_data = gmpr.add_population_column("fips", self.fips_data_3)
280+
new_data = gmpr.add_population_column(self.fips_data_3, "fips")
281281
assert new_data["population"].sum() == 274963
282-
new_data = gmpr.add_population_column("zip", self.zip_data)
282+
new_data = gmpr.add_population_column(self.zip_data, "zip")
283283
assert new_data["population"].sum() == 274902
284284
with pytest.raises(ValueError):
285-
new_data = gmpr.add_population_column("hrr", self.zip_data)
286-
pop_df = gmpr.add_population_column("fips")
287-
assert pop_df.shape == (3274, 2)
285+
new_data = gmpr.add_population_column(self.zip_data, "hrr")
288286

289287
def test_add_geocode(self):
290288
gmpr = GeoMapper()

jhu/delphi_jhu/pull.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def pull_jhu_data(base_url: str, metric: str, gmpr: GeoMapper) -> pd.DataFrame:
101101
)
102102

103103
# Merge in population, set population as NAN for fake fips
104-
df = gmpr.add_population_column("fips", df)
104+
df = gmpr.add_population_column(df, "fips")
105105

106106
df = create_diffs_column(df)
107107

0 commit comments

Comments
 (0)