Skip to content

Commit e1f96f9

Browse files
committed
Fix date_col docstring, add None option to date_col, add test for that option
1 parent 0befb53 commit e1f96f9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

_delphi_utils_python/delphi_utils/geomap.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ def replace_geocode(
375375
new_code: {'fips', 'zip', 'state_code', 'state_id', 'state_name', 'hrr', 'msa',
376376
'hhs_region_number'}
377377
Specifies the geocode type of the data in new_col.
378+
date_col: str or None, default "date"
379+
Specify which column contains the date values. Used for value aggregation.
380+
If None, then the aggregation is done only on geo_id.
378381
data_cols: list, default None
379382
A list of data column names to aggregate when doing a weighted coding. If set to
380383
None, then all the columns are used except for date_col and new_col.
@@ -403,7 +406,11 @@ def replace_geocode(
403406
# Multiply and aggregate (this automatically zeros NAs)
404407
df[data_cols] = df[data_cols].multiply(df["weight"], axis=0)
405408
df.drop("weight", axis=1, inplace=True)
406-
df = df.groupby([date_col, new_col]).sum().reset_index()
409+
410+
if not date_col is None:
411+
df = df.groupby([date_col, new_col]).sum().reset_index()
412+
else:
413+
df = df.groupby([new_col]).sum().reset_index()
407414
return df
408415

409416
def add_population_column(self, data, geocode_type, geocode_col=None):

_delphi_utils_python/tests/test_geomap.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,14 @@ def test_add_geocode(self):
439439
assert not gmpr.add_geocode(self.fips_data_3, "fips", "hrr").isna().any().any()
440440
assert gmpr.add_geocode(self.fips_data_3, "fips", "hrr", dropna=False).isna().any().any()
441441

442-
TestGeoMapper().test_add_geocode()
442+
# fips -> zip (date_col=None chech)
443+
new_data = gmpr.replace_geocode(self.fips_data_5.drop(columns=["date"]), "fips", "hrr", date_col=None)
444+
assert new_data.equals(
445+
pd.DataFrame().from_dict(
446+
{
447+
'hrr': {0: '1', 1: '183', 2: '184', 3: '382', 4: '7'},
448+
'count': {0: 1.772347174163783, 1: 7157.392403522299, 2: 2863.607596477701, 3: 1.0, 4: 0.22765282583621685},
449+
'total': {0: 3.544694348327566, 1: 71424.64801363471, 2: 28576.35198636529, 3: 1.0, 4: 0.4553056516724337}
450+
}
451+
)
452+
)

0 commit comments

Comments
 (0)