Skip to content

Commit 34a3585

Browse files
committed
Update add_population_column
* unnecessary int type casting of population * add dropna flag and default it to a left merge
1 parent 29c01e6 commit 34a3585

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

_delphi_utils_python/delphi_utils/geomap.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ 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):
395+
def add_population_column(self, geocode_type, data=None, 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.
@@ -428,12 +428,13 @@ def add_population_column(self, geocode_type, data=None, geocode_col=None):
428428
if not is_string_dtype(data[geocode_col]):
429429
data[geocode_col] = data[geocode_col].astype(str).str.zfill(5)
430430

431+
merge_type = "left" if dropna else "inner"
431432
data_with_pop = (
432433
data.copy()
433-
.merge(pop_df, left_on=geocode_col, right_on=geocode_type, how="inner")
434+
.merge(pop_df, left_on=geocode_col, right_on=geocode_type, how=merge_type)
434435
.rename(columns={"pop": "population"})
435436
)
436-
data_with_pop["population"] = data_with_pop["population"].astype(int)
437+
437438
return data_with_pop
438439

439440
@staticmethod

0 commit comments

Comments
 (0)