@@ -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 )
0 commit comments