python-mlb-statsapi
python-mlb-statsapi copied to clipboard
ERROR: GameInfo.___init___() missing 2 required positional arguments: 'attendance' and 'gamedurationminutes'
@KCNilssen @Mattsface Looks like another GameInfo.init() adjustment needs to be made. Positional arguments attendance & gamedurationminutes, errors.
Call works for all but 2 gamePK's(745114 & 746083).
Input mlb_games.csv
Output mlb_games_ready.csv
Same code block as previous issue:
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"
Code #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)