@@ -165,37 +165,11 @@ def county_to_megacounty(self, data, threshold_visits, threshold_len):
165
165
recent_visits_df , how = "left" , on = ["ServiceDate" , "PatCountyFIPS" ]
166
166
)
167
167
168
- # mark date-fips points to exclude if we see less than threshold visits that day
169
- data ["ToExclude" ] = data ["RecentVisits" ] < threshold_visits
170
-
171
- # now to convert to megacounties
172
- state_map = pd .read_csv (
173
- join (self .geo_filepath , "02_20_uszips.csv" ),
174
- usecols = ["fips" , "state_id" ],
175
- dtype = {"state_id" : str },
176
- converters = {"fips" : GeoMaps .convert_fips },
177
- )
178
- state_map .drop_duplicates (inplace = True )
179
- data = data .merge (
180
- state_map , how = "left" , left_on = "PatCountyFIPS" , right_on = "fips"
181
- )
182
- # drops rows with no matches, which should not be many
183
- data .dropna (inplace = True )
184
- data .drop (columns = ["fips" ], inplace = True )
185
- data ["StateFIPS" ] = data ["PatCountyFIPS" ].apply (
186
- lambda f : str ((int (f ) // 1000 ) * 1000 ).zfill (5 )
187
- )
188
-
189
- megacounty_df = (
190
- data [data ["ToExclude" ]]
191
- .groupby (["ServiceDate" , "StateFIPS" ])
192
- .sum ()
193
- .reset_index ()
194
- )
195
- megacounty_df ["ToExclude" ] = False
196
- megacounty_df .rename (columns = {"StateFIPS" : "PatCountyFIPS" }, inplace = True )
197
-
198
- result = pd .concat ([data , megacounty_df ])
199
- result .drop (columns = ["StateFIPS" , "state_id" ], inplace = True )
200
-
201
- return result .groupby ("PatCountyFIPS" ), "PatCountyFIPS"
168
+ data = self .gmpr .fips_to_megacounty (data ,
169
+ threshold_visits ,
170
+ threshold_len ,
171
+ fips_col = "PatCountyFIPS" ,
172
+ thr_col = "RecentVisits" ,
173
+ date_col = "ServiceDate" )
174
+ data .rename ({"megafips" : "PatCountyFIPS" }, axis = 1 , inplace = True )
175
+ return data .groupby ("PatCountyFIPS" ), "PatCountyFIPS"
0 commit comments