So I am extremely new to Python, or probably more accurately should say that I never use it and am trying to execute someone else's code to get some figures, but I keep on experiencing an error.
A very pared-down version of the code that I am trying to run is below. Please note that create_and_run_model is a more complex function, but I have shortened it for reproduciblity. I can't seem to get either the loop to run, or even an individual, state-level model to run either.
import pandas as pd
## Load State Information
url = 'https://covidtracking.com/api/v1/states/daily.csv'
states = pd.read_csv(url,
parse_dates=['date'],
index_col=['state', 'date']).sort_index()
# Note: GU/AS/VI do not have enough data for this model to run
# Note: PR had -384 change recently in total count so unable to model
states = states.drop(['MP', 'GU', 'AS', 'PR', 'VI'])
def create_and_run_model(name, state):
confirmed = state.positive.diff().dropna()
# Loop that is in the original code
models = {}
for state, grp in states.groupby('state'):
print(state)
if state in models:
print(f'Skipping {state}, already in cache')
continue
models[state] = create_and_run_model(grp.droplevel(0))
model_ny = create_and_run_model(states, "NY")
When I try to run the loop, I am getting thrown an error message stating: "create_and_run_model() missing 1 required positional argument: 'state'"
When I try to run the individual model, I am getting an error message stating: "'str' object has no attribute 'positive'"
I have zero idea what I'm doing wrong. Any help would be greatly appreciated,
create_and_run_model(states, "NY")so the argumentstateis"NY"for that call, which is a string. Then you dostate.positive.diff().dropna()hence the error... maybe you meantname.positive.diff().dropna(). And when you callcreate_and_run_model(grp.droplevel(0))you are not passing thestateargument at all...