[docs]classPuertoRico(CountryTestBase):location="Puerto Rico"source_url="https://healthdata.gov/api/views/j8mb-icvb/rows.csv"source_url_ref="https://healthdata.gov/dataset/COVID-19-Diagnostic-Laboratory-Testing-PCR-Testing/j8mb-icvb"source_label="Department of Health & Human Services"units="tests performed"rename_columns={"date":"Date"}
[docs]defpipe_metrics(self,df:pd.DataFrame)->pd.DataFrame:# daily change in positive testspos=(df[(df["overall_outcome"]=="Positive")&(df["state_name"]=="Puerto Rico")].groupby("Date",as_index=False).agg(**{"Daily change in positive total":("new_results_reported",sum)})).sort_values("Date")# daily change in total testsdf=(df[df["state_name"]=="Puerto Rico"].groupby(["Date"],as_index=False).agg(**{"Daily change in cumulative total":("new_results_reported",sum)})).sort_values("Date")# generate positive ratedf["Positive rate"]=((pos["Daily change in positive total"].rolling(7).mean())/(df["Daily change in cumulative total"].rolling(7).mean())).round(3)df["Positive rate"][df["Date"]<"2020-03-24"]=pd.NAdf=df[df["Daily change in cumulative total"]!=0]returndf