Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processing game_pk information: PlayDetails.__init__() got an unexpected keyword argument 'violation' #204

Closed
mustseetv314 opened this issue Feb 19, 2024 · 11 comments

Comments

@mustseetv314
Copy link

mustseetv314 commented Feb 19, 2024

Note: I'm using the latest version of:
python-mlb-statsapi 0.5.16
MLB-StatsAPI 1.7.1

This function broke after the topperformers class update 2 weeks ago:

For some game_pk's the information returns as expected, but for a few(errors below) I'm receiving a PlayDetails.init() error, and I can't find that class in the repository:
PlayDetails.init() got an unexpected keyword argument 'violation'

Code:
def get_game_info(game_pk):
mlb = mlbstatsapi.Mlb()
try:
game = mlb.get_game(game_pk)
venue_id = game.gamedata.venue.id
venue_name = game.gamedata.venue.name
venue_lat = game.gamedata.venue.location.defaultcoordinates.latitude
venue_long = game.gamedata.venue.location.defaultcoordinates.longitude
venue_elevation = game.gamedata.venue.location.elevation
venue_surface = game.gamedata.venue.fieldinfo.turftype
return venue_id, venue_name, venue_lat, venue_long, venue_elevation, venue_surface
except Exception as e:
print(f"Error processing game_pk {game_pk}: {e}")
return "None", "None", "None", "None", "None", "None"

ERROR(For example processing 2023-06-03 game_pk's):
717885
Error processing game_pk 717885: PlayDetails.init() got an unexpected keyword argument 'violation'

@KCNilssen
Copy link
Collaborator

Thanks for the heads up! Looks like there is another update to the mlb api and we will need to add that field to game details

@mustseetv314
Copy link
Author

Thanks for the heads up! Looks like there is another update to the mlb api and we will need to add that field to game details

All good, appreciate it. Use this all the time, so kudos to ya'll. Great stuff!

@mustseetv314
Copy link
Author

Thanks for the heads up! Looks like there is another update to the mlb api and we will need to add that field to game details

Hey KC! Any ETA on when ya'll will make this code fix? Thanks!

@mustseetv314
Copy link
Author

@KCNilssen any ETA for the fix here? Thanks!

@Mattsface
Copy link
Collaborator

I'm sorry I'll go ahead and fix this now

@Mattsface
Copy link
Collaborator

Can you share your code?

I have no issues with gamepk 717885 @mustseetv314

@mustseetv314
Copy link
Author

sure @Mattsface ill run it again and post code

@mustseetv314
Copy link
Author

@Mattsface @KCNilssen

Code snipet plus 2 .csv attached:
Input = mlb_games.csv
output = mlb_games_ready.csv
--As you can see, it grabs the venue name, lat, long, elevation, surface for some...but errors with for some...PlayDetails.init()
image

mlb_games.csv
mlb_games_ready.csv

pip list
image

#Add Game Venue and Location Detail
csv_file = 'mlb_games.csv'
df = pd.read_csv(csv_file)

Initialize new columns

df['venue_id'] = None
df['venue_name'] = None
df['venue_latitude'] = None
df['venue_longitude'] = None
df['venue_elevation'] = None
df['venue_surface'] = None

for index, row in df.iterrows():
game_pk = row['GamePk']
print(game_pk)
try:
venue_id, venue_name, venue_lat, venue_long, venue_elevation, venue_surface = get_game_info(game_pk)
df.at[index, 'venue_id'] = venue_id
df.at[index, 'venue_name'] = venue_name
df.at[index, 'venue_latitude'] = venue_lat
df.at[index, 'venue_longitude'] = venue_long
df.at[index, 'venue_elevation'] = venue_elevation
df.at[index, 'venue_surface'] = venue_surface
except Exception as e:
print(f"Error processing game_pk {game_pk}: {e}")

new_column_order = [
'Date', 'Time', 'GamePk', 'Away Team', 'Home Team', 'venue_id', 'venue_name', 'venue_latitude', 'venue_longitude', 'venue_elevation', 'venue_surface']

Check if all columns are included in new_column_order

assert set(new_column_order) == set(df.columns), "New column order does not match DataFrame columns"

Reindex the DataFrame with the new column order

df = df[new_column_order]

Save the updated DataFrame back to a CSV

updated_csv_file = 'mlb_games_ready.csv'
df.to_csv(updated_csv_file, index=False)

@mustseetv314
Copy link
Author

Function:

#Function to get weather and venue for a given game_pk
def get_game_info(game_pk):
mlb = mlbstatsapi.Mlb()
try:
game = mlb.get_game(game_pk)
venue_id = game.gamedata.venue.id
venue_name = game.gamedata.venue.name
venue_lat = game.gamedata.venue.location.defaultcoordinates.latitude
venue_long = game.gamedata.venue.location.defaultcoordinates.longitude
venue_elevation = game.gamedata.venue.location.elevation
venue_surface = game.gamedata.venue.fieldinfo.turftype
return venue_id, venue_name, venue_lat, venue_long, venue_elevation, venue_surface
except Exception as e:
print(f"Error processing game_pk {game_pk}: {e}")
return "None", "None", "None", "None", "None", "None"

@KCNilssen
Copy link
Collaborator

@mustseetv314 Your issue should be resolved now, let us know If you have any more issues

@mustseetv314
Copy link
Author

@KCNilssen @Mattsface looks good now gents, appreciate it!

Upgraded to: python-mlb-statsapi 0.5.17 and errors are now gone.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants