rairo commited on
Commit
1d818d3
·
verified ·
1 Parent(s): cc55fb7

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +20 -15
src/streamlit_app.py CHANGED
@@ -74,7 +74,8 @@ def get_player_index_brscraper():
74
  def get_player_career_stats_brscraper(player_name):
75
  """
76
  Uses BRScraper to get a player's career stats.
77
- Detects and renames the season column, applies mapping and numeric conversion.
 
78
  """
79
  if not BRSCRAPER_AVAILABLE:
80
  return pd.DataFrame()
@@ -84,18 +85,22 @@ def get_player_career_stats_brscraper(player_name):
84
  if df.empty:
85
  return df
86
 
87
- # 1) Detect whatever BRScraper calls its season column, rename to 'Season'
88
- season_renamed = False
89
  for col in df.columns:
90
- if 'season' in col.lower() or 'year' in col.lower():
91
- df = df.rename(columns={col: 'Season'})
92
- season_renamed = True
93
  break
94
- if not season_renamed:
95
- st.error(f"Could not find a season column for {player_name}.")
96
- return pd.DataFrame()
97
-
98
- # 2) Standardize column names
 
 
 
 
99
  mapping = {
100
  'G':'GP','GS':'GS','MP':'MIN',
101
  'FG%':'FG_PCT','3P%':'FG3_PCT','FT%':'FT_PCT',
@@ -108,16 +113,16 @@ def get_player_career_stats_brscraper(player_name):
108
  }
109
  df = df.rename(columns={o:n for o,n in mapping.items() if o in df.columns})
110
 
111
- # 3) Clean up season formatting
112
- df['Season'] = df['Season'].astype(str).str.replace('-', '–')
113
 
114
- # 4) Convert all other columns to numeric where possible
115
  non_numeric = {'Season', 'TEAM_ABBREVIATION', 'LEAGUE_ID', 'POSITION'}
116
  for c in df.columns:
117
  if c not in non_numeric:
118
  df[c] = pd.to_numeric(df[c], errors='coerce')
119
 
120
- # 5) Tag with player name
121
  df['Player'] = player_name
122
 
123
  return df
 
74
  def get_player_career_stats_brscraper(player_name):
75
  """
76
  Uses BRScraper to get a player's career stats.
77
+ Detects and renames the season column (or falls back to first column),
78
+ applies mapping and numeric conversion.
79
  """
80
  if not BRSCRAPER_AVAILABLE:
81
  return pd.DataFrame()
 
85
  if df.empty:
86
  return df
87
 
88
+ # 1) Try to autodetect a season column
89
+ season_col = None
90
  for col in df.columns:
91
+ low = col.lower().strip()
92
+ if 'season' in low or 'year' in low:
93
+ season_col = col
94
  break
95
+
96
+ # 2) Fallback: if nothing detected, just take the first column
97
+ if season_col is None:
98
+ season_col = df.columns[0]
99
+
100
+ # 3) Rename whatever that column is to exactly 'Season'
101
+ df = df.rename(columns={season_col: 'Season'})
102
+
103
+ # 4) Standard column mapping
104
  mapping = {
105
  'G':'GP','GS':'GS','MP':'MIN',
106
  'FG%':'FG_PCT','3P%':'FG3_PCT','FT%':'FT_PCT',
 
113
  }
114
  df = df.rename(columns={o:n for o,n in mapping.items() if o in df.columns})
115
 
116
+ # 5) Clean and format the Season column
117
+ df['Season'] = df['Season'].astype(str).str.replace('-', '–').str.strip()
118
 
119
+ # 6) Convert everything else numeric
120
  non_numeric = {'Season', 'TEAM_ABBREVIATION', 'LEAGUE_ID', 'POSITION'}
121
  for c in df.columns:
122
  if c not in non_numeric:
123
  df[c] = pd.to_numeric(df[c], errors='coerce')
124
 
125
+ # 7) Tag with Player
126
  df['Player'] = player_name
127
 
128
  return df