@@ -33,12 +33,12 @@ def pull_nchs_mortality_data(token: str, map_df: pd.DataFrame, test_mode: str):
3333 Dataframe as described above.
3434 """
3535 # Constants
36- KEEP_COLUMNS = ['covid_deaths' , 'total_deaths' ,
36+ keep_columns = ['covid_deaths' , 'total_deaths' ,
3737 'percent_of_expected_deaths' , 'pneumonia_deaths' ,
3838 'pneumonia_and_covid_deaths' , 'influenza_deaths' ,
3939 'pneumonia_influenza_or_covid_19_deaths' ]
40- TYPE_DICT = {key : float for key in KEEP_COLUMNS }
41- TYPE_DICT ["timestamp" ] = 'datetime64[ns]'
40+ type_dict = {key : float for key in keep_columns }
41+ type_dict ["timestamp" ] = 'datetime64[ns]'
4242
4343 if test_mode == "" :
4444 # Pull data from Socrata API
@@ -52,18 +52,18 @@ def pull_nchs_mortality_data(token: str, map_df: pd.DataFrame, test_mode: str):
5252 # Check missing start_week == end_week
5353 try :
5454 assert sum (df ["timestamp" ] != df ["end_week" ]) == 0
55- except AssertionError :
55+ except AssertionError as exc :
5656 raise ValueError (
5757 "end_week is not always the same as start_week, check the raw file"
58- )
58+ ) from exc
5959
6060 try :
61- df = df .astype (TYPE_DICT )
62- except KeyError :
61+ df = df .astype (type_dict )
62+ except KeyError as exc :
6363 raise ValueError ("Expected column(s) missed, The dataset "
64- "schema may have changed. Please investigate and "
65- "amend the code." )
66-
64+ "schema may have changed. Please investigate and "
65+ "amend the code." ) from exc
66+
6767 df = df [df ["state" ] != "United States" ]
6868 df .loc [df ["state" ] == "New York City" , "state" ] = "New York"
6969
@@ -91,7 +91,7 @@ def pull_nchs_mortality_data(token: str, map_df: pd.DataFrame, test_mode: str):
9191 )
9292
9393 # Add population info
94- KEEP_COLUMNS .extend (["timestamp" , "geo_id" , "population" ])
95- df = df .merge (map_df , on = "state" )[KEEP_COLUMNS ]
96-
94+ keep_columns .extend (["timestamp" , "geo_id" , "population" ])
95+ df = df .merge (map_df , on = "state" )[keep_columns ]
96+
9797 return df
0 commit comments