rairo commited on
Commit
e7a2819
·
verified ·
1 Parent(s): 397b75c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +22 -4
src/streamlit_app.py CHANGED
@@ -126,11 +126,24 @@ def get_player_career_stats_brscraper(player_name, seasons_to_check=10):
126
  def get_team_season_stats_brscraper(year):
127
  if not BRSCRAPER_AVAILABLE:
128
  return pd.DataFrame()
 
129
  try:
130
  df = nba.get_stats(year, info='per_game', rename=False)
131
- teams = df[df['Player'].isna() & (df['Rk'].astype(str).str.lower()!='rk')].copy()
 
 
 
 
 
 
 
 
 
 
132
  if teams.empty:
133
  return pd.DataFrame()
 
 
134
  mapping = {
135
  'G':'GP','MP':'MIN','FG%':'FG_PCT','3P%':'FG3_PCT','FT%':'FT_PCT',
136
  'TRB':'REB','AST':'AST','STL':'STL','BLK':'BLK','TOV':'TO',
@@ -139,13 +152,18 @@ def get_team_season_stats_brscraper(year):
139
  '2P':'FGM2','2PA':'FGA2','2P%':'FG2_PCT','eFG%':'EFG_PCT',
140
  'FT':'FTM','FTA':'FTA','ORB':'OREB','DRB':'DREB','Tm':'Team'
141
  }
142
- teams = teams.rename(columns={o:n for o,n in mapping.items() if o in teams.columns})
143
- teams['Team'] = teams['Team'].astype(str).str.replace('*','',regex=False).str.strip()
144
- non_num = {'Team','RANK'}
 
 
 
145
  for col in teams.columns:
146
  if col not in non_num:
147
  teams[col] = pd.to_numeric(teams[col], errors='coerce')
 
148
  return teams
 
149
  except Exception as e:
150
  st.error(f"Error fetching team stats for {year}: {e}")
151
  return pd.DataFrame()
 
126
  def get_team_season_stats_brscraper(year):
127
  if not BRSCRAPER_AVAILABLE:
128
  return pd.DataFrame()
129
+
130
  try:
131
  df = nba.get_stats(year, info='per_game', rename=False)
132
+
133
+ # Ensure 'Rk' and 'Player' columns exist
134
+ if 'Rk' not in df.columns or 'Player' not in df.columns:
135
+ st.warning(f"'Rk' or 'Player' columns missing for {year}.")
136
+ return pd.DataFrame()
137
+
138
+ # Drop rows that are header repeats (e.g. 'Rk' == 'Rk' or 'rk')
139
+ df = df[df['Rk'].apply(lambda x: str(x).lower() != 'rk')]
140
+
141
+ # Keep only rows where Player is NaN (which correspond to team totals)
142
+ teams = df[df['Player'].isna()].copy()
143
  if teams.empty:
144
  return pd.DataFrame()
145
+
146
+ # Rename & clean columns
147
  mapping = {
148
  'G':'GP','MP':'MIN','FG%':'FG_PCT','3P%':'FG3_PCT','FT%':'FT_PCT',
149
  'TRB':'REB','AST':'AST','STL':'STL','BLK':'BLK','TOV':'TO',
 
152
  '2P':'FGM2','2PA':'FGA2','2P%':'FG2_PCT','eFG%':'EFG_PCT',
153
  'FT':'FTM','FTA':'FTA','ORB':'OREB','DRB':'DREB','Tm':'Team'
154
  }
155
+ teams = teams.rename(columns={o: n for o, n in mapping.items() if o in teams.columns})
156
+
157
+ if 'Team' in teams.columns:
158
+ teams['Team'] = teams['Team'].astype(str).str.replace('*', '', regex=False).str.strip()
159
+
160
+ non_num = {'Team', 'RANK'}
161
  for col in teams.columns:
162
  if col not in non_num:
163
  teams[col] = pd.to_numeric(teams[col], errors='coerce')
164
+
165
  return teams
166
+
167
  except Exception as e:
168
  st.error(f"Error fetching team stats for {year}: {e}")
169
  return pd.DataFrame()