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

[FotMob] Get correct team match stats using 'opponent_stats' parameter #541

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions soccerdata/fotmob.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,27 +409,23 @@ def read_team_match_stats( # noqa: C901

df_raw_stats = pd.DataFrame(selected_stats['stats'])
game_teams = [game.home_team, game.away_team]
if opponent_stats:
game_teams = [game.away_team, game.home_team]
for i, team in enumerate(game_teams):
df_team_stats = df_raw_stats.copy()
df_team_stats['stat'] = df_team_stats['stats'].apply(lambda x: x[i]) # noqa: B023
df_team_stats["league"] = lkey
df_team_stats["season"] = skey
df_team_stats["game"] = gkey
df_team_stats['team'] = team
if not opponent_stats:
df_team_stats = df_team_stats[df_team_stats.team.isin(teams_to_check)]
df_team_stats.set_index(['league', 'season', 'game', 'team'], inplace=True)
df_team_stats = df_team_stats[df_team_stats['type'] != 'title']
df_team_stats = df_team_stats.pivot(columns='title', values='stat').reset_index()
df_team_stats.columns.name = None
stats.append(df_team_stats)

df = pd.concat(stats, axis=0)
df = (
df[df.team.isin(teams_to_check)]
.set_index(['league', 'season', 'game', 'team'])
.sort_index()
)
df = df.set_index(['league', 'season', 'game', 'team']).sort_index()
# Split percentage values
pct_cols = [col for col in df.columns if df[col].astype(str).str.contains('%').any()]
for col in pct_cols:
Expand Down
Loading