Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions usafacts/delphi_usafacts/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ def geo_map(df: pd.DataFrame, geo_res: str, map_df: pd.DataFrame, sensor: str):
df = df.append(unassigned_counties)
geo_mapper = GeoMapper()
df = geo_mapper.add_geocode(df, "fips", "state_id", new_col="geo_id")

# Zero out the state FIPS population to avoid double counting.
df = df.set_index("fips")
state_fips_codes = {str(x).zfill(2) + "000" for x in range(1, 73)}
subset_state_fips_codes = set(df.index.values) & state_fips_codes
df.loc[subset_state_fips_codes, "population"] = 0
df = df.reset_index()
elif geo_res in ("msa", "hrr"):
# Map "missing" secondary FIPS to those that are in our canonical set
for fips, fips_list in SECONDARY_FIPS:
Expand Down
10 changes: 5 additions & 5 deletions usafacts/tests/test_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ def test_state(self):
"""Tests that values are correctly aggregated at the state level."""
df = pd.DataFrame(
{
"fips": ["04001", "04003", "04009", "25023"],
"timestamp": ["2020-02-15", "2020-02-15", "2020-02-15", "2020-02-15"],
"new_counts": [10, 15, 2, 13],
"cumulative_counts": [100, 20, 45, 60],
"population": [100, 2100, 300, 25],
"fips": ["04001", "04003", "04009", "25023", "25000"],
"timestamp": ["2020-02-15", "2020-02-15", "2020-02-15", "2020-02-15", "2020-02-15"],
"new_counts": [10, 15, 2, 13, 0],
"cumulative_counts": [100, 20, 45, 60, 0],
Comment on lines +81 to +82
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these always assumed to be 0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but it was the minimum change to the tests that failed in main and succeeded with the fix.

"population": [100, 2100, 300, 25, 25],
}
)

Expand Down