Skip to content

Commit

Permalink
fixed zip endpoint for nyc (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
hurshd0 committed Apr 20, 2020
1 parent 96e39bd commit 53fa1e0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 87 deletions.
14 changes: 8 additions & 6 deletions api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from api.utils import read_country_data
from api.utils import read_county_stats
from api.utils import read_states
from api.utils import read_county_stats_zip_ny
from api.config import DataReadingError

# Starts the FastAPI Router to be used by the FastAPI app.
Expand Down Expand Up @@ -451,18 +450,21 @@ def post_zip(zip_code: ZIPInput) -> JSONResponse:
try:
zip_info = zipcodes.matching(zip_code.zip_code)[0]
except Exception as ex:
_logger.warning(f"Endpoint: /zip --- POST --- {ex}")
message = f"ZIP code {zip_code.zip_code} not found in US Zipcode database."
_logger.warning(f"Endpoint: /zip --- POST --- {message}")
raise HTTPException(status_code=422,
detail=f"[Error] get '/zip' API: {ex}")
detail=f"[Error] POST '/zip' {message}")

try:
county = zip_info['county'].rsplit(' ', 1)[0]
state = zip_info['state']
_logger.info(f"State: {state}, County: {county}")
if state == "NY":
print('NY')
data = read_county_stats_zip_ny(zip_code.zip_code)
nyc_counties = ["Bronx", "Kings", "Queens", "Richmond"]
if county in nyc_counties:
county = "New York"
data = read_county_stats(state, county)[0]
else:
print("not NY")
data = read_county_stats(state, county)[0]
json_data = {"success": True, "message": data}
del data
Expand Down
1 change: 0 additions & 1 deletion api/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
from .county_mongo import StateMongo
from .county import read_county_stats
from .state import read_states
from .zip import read_county_stats_zip_ny
19 changes: 9 additions & 10 deletions api/utils/county.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def read_county_stats(state: str, county: str) -> Dict:

try:
df = pd.read_csv(app_config.COUNTY_URL)
deaths = pd.read_csv(app_config.STATE_DEATH)
#deaths = pd.read_csv(app_config.STATE_DEATH)
except:
raise DataReadingError(
f"Data reading error State: {state}, and County: {county}."
Expand All @@ -32,18 +32,17 @@ def read_county_stats(state: str, county: str) -> Dict:
df.columns = map(str.lower, df.columns)
df.columns = df.columns.str.replace(" ", "_")

# used data source 2 for new death number
deaths = deaths[deaths['Province_State'] == reverse_states_map[state]]
deaths = deaths[deaths['Admin2'] == county]
# 4/15/20: force cast into int before diff as pd sometimes read as
# float and throws nan.
deaths = deaths.iloc[:, 12:].astype('int32').\
diff(axis=1).iloc[:, -1].values[0]
# # used data source 2 for new death number
# deaths = deaths[deaths['Province_State'] == reverse_states_map[state]]
# deaths = deaths[deaths['Admin2'] == county]
# # 4/15/20: force cast into int before diff as pd sometimes read as
# # float and throws nan.
# deaths = deaths.iloc[:, 12:].astype('int32').\
# diff(axis=1).iloc[:, -1].values[0]

df = df[df["state_name"] == reverse_states_map[state]]
# df = df.query(f"county_name == '{county}'")
df = df[df["county_name"] == county]
df.new_death.iloc[0] = deaths
# df.new_death.iloc[0] = deaths
df = pd.DataFrame.to_dict(df, orient="records")
if len(df) == 0:
raise DataValidationError("county.py len(df) == 0")
Expand Down
70 changes: 0 additions & 70 deletions api/utils/zip.py

This file was deleted.

0 comments on commit 53fa1e0

Please sign in to comment.