0

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,

1
  • 1
    you call create_and_run_model(states, "NY") so the argument state is "NY" for that call, which is a string. Then you do state.positive.diff().dropna() hence the error... maybe you meant name.positive.diff().dropna(). And when you call create_and_run_model(grp.droplevel(0)) you are not passing the state argument at all... Commented May 21, 2020 at 15:25

2 Answers 2

2

I think the order of the parameters doesn't match

When the function is declared create_and_run_model(name, state) name is fiirst and state is second

When the function is called model_ny = create_and_run_model(states, "NY") state seems to be first and name second

Edited as per comment:

You are passing the said dataframe "NY" as a string though. That's why calling positive on it also fails. Also name doesn't seem to be used. If you just want to call the dataframe that contains ONLY "NY" then you can filter it something like NY = states[['NY']] or use states[['NY']] as an argument instead of "NY".

And get rid of name as it is not used and if you don't intend to use it

Sign up to request clarification or add additional context in comments.

2 Comments

I thought about that but I'm not sure if that's it. My interpretation is that in 'create_and_run_model', name = the data frame containing the state information ,and 'state' specifies which state to look at
You are passing the said dataframe "NY" as a string though. That's why calling positive on it also fails. Also name doesn't seem to be used. If you just want to call the dataframe that contains ONLY "NY" then you can filter it something like NY = states[['NY']] or use states[['NY']] as an argument instead of "NY"
0

The function create_and_run_model accepts two positional arguments name and state, but never makes any use of name. So maybe you should just remove the first argument from the function definition like this:

def create_and_run_model(state):
    confirmed = state.positive.diff().dropna()

And than don't use it in create_and_run_model function calls:

model_ny = create_and_run_model(states.loc['NY'])

states.loc[''] will allow you to select specific state from the dataframe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.