zero-sum-seattle / python-mlb-statsapi

Python Wrapper for the MLB's Official Stats API
MIT License
44 stars 11 forks source link

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

Closed mustseetv314 closed 6 months ago

mustseetv314 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 6 months ago

@KCNilssen any ETA for the fix here? Thanks!

Mattsface commented 6 months ago

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

Mattsface commented 6 months ago

Can you share your code?

I have no issues with gamepk 717885 @mustseetv314

mustseetv314 commented 6 months ago

sure @Mattsface ill run it again and post code

mustseetv314 commented 6 months ago

@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 commented 6 months ago

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 commented 6 months ago

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

mustseetv314 commented 6 months ago

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

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