<jupyter_start><jupyter_text>COVID-19 Cases by Zip Code<jupyter_code># these are the Python packages being used for data collection and analysis
import datetime
import requests
import pandas as pd

# this is the URL that supplies the COVID data by zip code
URL = "https://services.arcgis.com/njFNhDsUCentVYJW/arcgis/rest/services/MDCOVID19_MASTER_ZIP_CODE_CASES/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"

r = requests.get(URL)

# this will shut down the cell operation if there was an HTTP issue
r.raise_for_status()

# this collects the relevant part of the response object and converts it
# into something we can turn into a dataframe (table)
json_list = []
for item in r.json()['features']:
  json_list.append(item['attributes'])

# this formats the dataframe to have zip code columns and date rows
df = pd.DataFrame(json_list)
df = df.set_index('ZIP_CODE')
df = df.transpose()
df = df.drop('OBJECTID')

# this function converts the odd date notation from the raw data into datetimes
def todate(s):
  date = pd.to_datetime(s.replace("_", "-").strip("Ftotal"))
  return date
df.index = df.index.map(todate)

# this ensures the dates are ordered properly
df = df.sort_index()

# this drop zip codes with fewer than ten observations
# df = df.dropna(axis=1, thresh=10)
# it's commented out because we're not currently doing mass visualizations

# this checks whether today's data is included in the dataframe
if df.index[-1].date() == datetime.datetime.now(tz=datetime.timezone(datetime.timedelta(hours=-4))).date():
  print("Fresh data present, good to go")
else:
  print("Today's data is NOT present")
# to cut off the earliest days from the chart and only look at the most recent n,
# set this variable to your desired value of n
# to see all the data from the beginning, set it to 0
truncate = 0

# these are the zip codes used for the visualization
# if you want to add more, make sure they're comma-separated and enclosed in quotes
codes = ['21204', '21286', '21212', '21209', '21208', '21093', ]

df[codes].iloc[-truncate:].plot.line(title="Cumulative Cases by Zip Code",
                                     figsize=(15,9), grid=True);
# to look at new cases over the past n days,
# set this variable to your desired value of n
past_days = 7

# to cut off the earliest days from the chart and only look at the most recent n,
# set this variable to your desired value of n
# to see all the data from the beginning, set it to 0
truncate2 = 0

# these are the zip codes used for the visualization
# if you want to add more, make sure they're comma-separated and enclosed in quotes
codes2 = ['21204', '21286', '21212', '21209', '21208', '21093']

# this generates a new dataframe based on cumulative case count dataframe
# the value for a given (zip code, date) is equal to the number of new cases
# in that zip code in the past n days
df2 = df.diff(periods=past_days) / past_days

df2[codes2].iloc[-truncate2:].plot.line(title=f"Average number of new cases per day in the past {past_days} days",
                                        figsize=(15,9), grid=True);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09580262736302467, 'fraction_numerical': 0.03011855174623518, 'mean_word_length': 4.194675540765391, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '544301', 'license': 'MIT', 'n_tokens_mistral': 1025, 'n_tokens_neox': 937, 'n_words': 401, 'path': 'COVIDbyZip.ipynb', 'repo_name': 'srfinley/MD-covid-viewer', 'size': '3121'}