Repository navigation
Expand file tree
/
Copy pathretrosheet.py
More file actions
454 lines (388 loc) · 20 KB
/
Copy pathretrosheet.py
File metadata and controls
454 lines (388 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""
Retrosheet data access layer.
Loads raw CSVs, filters to modern era, computes derived stats,
and exposes clean DataFrames for dashboard consumption.
"""
from __future__ import annotations
import datetime
from pathlib import Path
from typing import Optional
import pandas as pd
import streamlit as st
RAW_DIR = Path(__file__).parent / "data_files" / "retrosheet"
MODERN_START = 2020 # default cutoff year
# ---------------------------------------------------------------------------
# Team name lookup (Retrosheet 3-letter codes → full franchise names)
# ---------------------------------------------------------------------------
TEAM_NAMES: dict[str, str] = {
"ANA": "Angels", "ARI": "Diamondbacks", "ATH": "Athletics",
"ATL": "Braves", "BAL": "Orioles", "BOS": "Red Sox",
"CHA": "White Sox", "CHN": "Cubs", "CIN": "Reds",
"CLE": "Guardians", "COL": "Rockies", "DET": "Tigers",
"HOU": "Astros", "KCA": "Royals", "LAN": "Dodgers",
"MIA": "Marlins", "MIL": "Brewers", "MIN": "Twins",
"MON": "Expos", "NYA": "Yankees", "NYN": "Mets",
"OAK": "Athletics", "PHI": "Phillies", "PIT": "Pirates",
"SDN": "Padres", "SEA": "Mariners", "SFN": "Giants",
"SLN": "Cardinals", "TBA": "Rays", "TEX": "Rangers",
"TOR": "Blue Jays", "WAS": "Nationals", "WSN": "Nationals",
"FLO": "Marlins", "CAL": "Angels", "TBD": "Rays",
"MIL": "Brewers",
}
def _team_name(code: str) -> str:
"""Return full team name, falling back to the code if not mapped."""
return TEAM_NAMES.get(str(code).upper(), code)
# ---------------------------------------------------------------------------
# Internal helpers
# ---------------------------------------------------------------------------
def _parse_retrodate(series: pd.Series) -> pd.Series:
"""Convert YYYYMMDD integer dates to datetime."""
return pd.to_datetime(series.astype(str), format="%Y%m%d", errors="coerce")
def _extract_year(series: pd.Series) -> pd.Series:
return pd.to_numeric(series.astype(str).str[:4], errors="coerce")
def _read_with_supplement(primary: Path, supplement: Path) -> pd.DataFrame:
"""Load a parquet file, optionally concat a supplement if it exists."""
df = pd.read_parquet(primary)
if supplement.exists():
supp = pd.read_parquet(supplement)
# Cast category columns in the primary back to object so concat works.
cat_cols = df.select_dtypes("category").columns.tolist()
for col in cat_cols:
df[col] = df[col].astype(object)
df = pd.concat([df, supp], ignore_index=True)
return df
# ---------------------------------------------------------------------------
# Game Info
# ---------------------------------------------------------------------------
@st.cache_data(show_spinner=False)
def load_gameinfo(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
"""
Loads gameinfo.csv and returns one row per game with key fields.
Filters to regular-season games within [min_year, max_year].
"""
min_year = max(min_year, MODERN_START)
df = _read_with_supplement(RAW_DIR / "gameinfo.parquet", RAW_DIR / "gameinfo_current.parquet")
df["season"] = pd.to_numeric(df["season"], errors="coerce")
df = df[(df["season"] >= min_year) & (df["season"] <= max_year)]
df["date"] = _parse_retrodate(df["date"])
df["vruns"] = pd.to_numeric(df["vruns"], errors="coerce")
df["hruns"] = pd.to_numeric(df["hruns"], errors="coerce")
df["attendance"] = pd.to_numeric(df["attendance"], errors="coerce")
df["temp"] = pd.to_numeric(df["temp"], errors="coerce")
df["windspeed"] = pd.to_numeric(df["windspeed"], errors="coerce")
df["total_runs"] = df["vruns"] + df["hruns"]
df["run_diff"] = df["hruns"] - df["vruns"] # positive = home win margin
for col in ("visteam", "hometeam", "wteam", "lteam"):
if col in df.columns:
df[col] = df[col].map(_team_name)
return df.reset_index(drop=True)
# ---------------------------------------------------------------------------
# Team Stats (per game)
# ---------------------------------------------------------------------------
_TEAMSTATS_COLS = [
"gid", "team", "stattype",
"b_pa", "b_ab", "b_r", "b_h", "b_d", "b_t", "b_hr",
"b_rbi", "b_sh", "b_sf", "b_hbp", "b_w", "b_iw", "b_k",
"b_sb", "b_cs", "b_gdp",
"p_ipouts", "p_bfp", "p_h", "p_hr", "p_r", "p_er",
"p_w", "p_iw", "p_k", "p_hbp", "p_wp", "p_bk",
"d_po", "d_a", "d_e", "d_dp",
"date", "vishome", "opp", "win", "loss", "tie", "gametype",
]
def _teamstats_from_supplements() -> pd.DataFrame:
"""Aggregate batting_current + pitching_current into team-per-game rows.
Used when teamstats_current.parquet is absent so load_teamstats can
include the current season without a full retrosheet rebuild.
"""
bat_path = RAW_DIR / "batting_current.parquet"
if not bat_path.exists():
return pd.DataFrame()
bat = pd.read_parquet(bat_path)
group_keys = [c for c in ["gid", "team", "date", "vishome", "opp", "win", "loss"] if c in bat.columns]
agg_cols = {c: (c, "sum") for c in ["b_pa", "b_ab", "b_r", "b_h", "b_d", "b_t", "b_hr",
"b_rbi", "b_w", "b_k", "b_sb", "b_hbp", "b_sf"] if c in bat.columns}
bat_agg = bat.groupby(group_keys, as_index=False).agg(**agg_cols)
pit_path = RAW_DIR / "pitching_current.parquet"
if pit_path.exists():
pit = pd.read_parquet(pit_path)
pit_agg_cols = {c: (c, "sum") for c in ["p_ipouts", "p_h", "p_hr", "p_r", "p_er",
"p_w", "p_k", "p_hbp", "p_wp", "p_bk"] if c in pit.columns}
pit_agg = pit.groupby(["gid", "team"], as_index=False).agg(**pit_agg_cols)
result = bat_agg.merge(pit_agg, on=["gid", "team"], how="left")
else:
result = bat_agg
result["stattype"] = "game"
return result
@st.cache_data(show_spinner=False)
def load_teamstats(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
"""
Loads teamstats.csv (value rows only), filters to regular season,
and computes derived rate stats (BA, OBP, SLG, OPS, ERA, WHIP, etc.).
Returns one row per team per game.
"""
min_year = max(min_year, MODERN_START)
df = _read_with_supplement(RAW_DIR / "teamstats.parquet", RAW_DIR / "teamstats_current.parquet")
# If teamstats_current.parquet hasn't been built yet, fall back to aggregating
# from the individual batting/pitching current supplements.
if not (RAW_DIR / "teamstats_current.parquet").exists():
_cur = _teamstats_from_supplements()
if not _cur.empty:
df = pd.concat([df, _cur], ignore_index=True)
cols = [c for c in _TEAMSTATS_COLS if c in df.columns]
df["date"] = _parse_retrodate(df["date"])
df["season"] = _extract_year(df["date"].dt.strftime("%Y%m%d"))
df = df[(df["season"] >= min_year) & (df["season"] <= max_year)]
# Numeric coerce
num_cols = [c for c in cols if c not in ("gid", "team", "stattype", "vishome", "opp", "gametype", "date")]
for c in num_cols:
df[c] = pd.to_numeric(df[c], errors="coerce")
# ----- Derived batting stats -----
df["ip"] = df["p_ipouts"] / 3
df["singles"] = df["b_h"] - df["b_d"] - df["b_t"] - df["b_hr"]
# BA
df["ba"] = (df["b_h"] / df["b_ab"]).round(3)
# OBP = (H + BB + HBP) / (AB + BB + HBP + SF)
df["obp"] = (
(df["b_h"] + df["b_w"] + df["b_hbp"])
/ (df["b_ab"] + df["b_w"] + df["b_hbp"] + df["b_sf"].fillna(0))
).round(3)
# SLG = (1B + 2*2B + 3*3B + 4*HR) / AB
df["slg"] = (
(df["singles"] + 2 * df["b_d"] + 3 * df["b_t"] + 4 * df["b_hr"])
/ df["b_ab"]
).round(3)
df["ops"] = (df["obp"] + df["slg"]).round(3)
# ----- Derived pitching stats -----
ip_safe = df["ip"].where(df["ip"] > 0)
bb_safe = pd.to_numeric(df["p_w"], errors="coerce").where(pd.to_numeric(df["p_w"], errors="coerce") > 0)
df["era"] = (9 * df["p_er"] / ip_safe).round(2)
df["whip"] = ((df["p_h"] + df["p_w"]) / ip_safe).round(3)
df["k9"] = (9 * df["p_k"] / ip_safe).round(2)
df["bb9"] = (9 * df["p_w"] / ip_safe).round(2)
df["k_bb"] = (pd.to_numeric(df["p_k"], errors="coerce") / bb_safe).round(2)
df["hr9"] = (9 * df["p_hr"] / ip_safe).round(2)
df["team"] = df["team"].map(_team_name)
return df.reset_index(drop=True)
# ---------------------------------------------------------------------------
# Batting (individual)
# ---------------------------------------------------------------------------
_BAT_COLS = [
"gid", "id", "team", "stattype",
"b_pa", "b_ab", "b_r", "b_h", "b_d", "b_t", "b_hr",
"b_rbi", "b_w", "b_k", "b_sb", "b_hbp", "b_sf",
"date", "vishome", "opp", "win", "loss", "gametype",
]
@st.cache_data(show_spinner=False)
def load_batting(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
min_year = max(min_year, MODERN_START)
df = _read_with_supplement(RAW_DIR / "batting.parquet", RAW_DIR / "batting_current.parquet")
cols = [c for c in _BAT_COLS if c in df.columns]
df["date"] = _parse_retrodate(df["date"])
df["season"] = _extract_year(df["date"].dt.strftime("%Y%m%d"))
df = df[(df["season"] >= min_year) & (df["season"] <= max_year)]
num_cols = [c for c in cols if c not in ("gid", "id", "team", "stattype", "vishome", "opp", "gametype", "date")]
for c in num_cols:
df[c] = pd.to_numeric(df[c], errors="coerce")
df["team"] = df["team"].map(_team_name)
return df.reset_index(drop=True)
# ---------------------------------------------------------------------------
# Pitching (individual)
# ---------------------------------------------------------------------------
_PITCH_COLS = [
"gid", "id", "team", "stattype",
"p_ipouts", "p_bfp", "p_h", "p_hr", "p_r", "p_er",
"p_w", "p_iw", "p_k", "p_hbp", "p_wp", "p_bk",
"p_gs", "p_gf", "p_cg",
"wp", "lp", "save",
"date", "vishome", "opp", "win", "loss", "gametype",
]
@st.cache_data(show_spinner=False)
def load_pitching(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
min_year = max(min_year, MODERN_START)
df = _read_with_supplement(RAW_DIR / "pitching.parquet", RAW_DIR / "pitching_current.parquet")
cols = [c for c in _PITCH_COLS if c in df.columns]
df["date"] = _parse_retrodate(df["date"])
df["season"] = _extract_year(df["date"].dt.strftime("%Y%m%d"))
df = df[(df["season"] >= min_year) & (df["season"] <= max_year)]
num_cols = [c for c in cols if c not in ("gid", "id", "team", "stattype", "vishome", "opp", "gametype", "date", "wp", "lp", "save")]
for c in num_cols:
df[c] = pd.to_numeric(df[c], errors="coerce")
df["ip"] = df["p_ipouts"] / 3
ip_safe = df["ip"].where(df["ip"] > 0)
df["era"] = (9 * df["p_er"] / ip_safe).round(2)
df["whip"] = ((df["p_h"] + df["p_w"]) / ip_safe).round(3)
df["k9"] = (9 * df["p_k"] / ip_safe).round(2)
df["team"] = df["team"].map(_team_name)
return df.reset_index(drop=True)
# ---------------------------------------------------------------------------
# Player registry
# ---------------------------------------------------------------------------
@st.cache_data(show_spinner=False)
def load_players(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
min_year = max(min_year, MODERN_START)
df = _read_with_supplement(RAW_DIR / "allplayers.parquet", RAW_DIR / "allplayers_current.parquet")
df["season"] = pd.to_numeric(df["season"], errors="coerce")
df = df[(df["season"] >= min_year) & (df["season"] <= max_year)]
df["full_name"] = df["first"] + " " + df["last"]
return df.reset_index(drop=True)
# ---------------------------------------------------------------------------
# Season aggregations (cached)
# ---------------------------------------------------------------------------
@st.cache_data(show_spinner=False)
def season_team_batting(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
"""Aggregate team batting stats by season."""
df = load_teamstats(min_year, max_year)
agg = df.groupby(["season", "team"]).agg(
G=("b_ab", "count"),
PA=("b_pa", "sum"),
AB=("b_ab", "sum"),
R=("b_r", "sum"),
H=("b_h", "sum"),
doubles=("b_d", "sum"),
triples=("b_t", "sum"),
HR=("b_hr", "sum"),
RBI=("b_rbi", "sum"),
BB=("b_w", "sum"),
K=("b_k", "sum"),
SB=("b_sb", "sum"),
).reset_index()
agg["singles"] = agg["H"] - agg["doubles"] - agg["triples"] - agg["HR"]
agg["BA"] = (agg["H"] / agg["AB"]).round(3)
agg["SLG"] = ((agg["singles"] + 2 * agg["doubles"] + 3 * agg["triples"] + 4 * agg["HR"]) / agg["AB"]).round(3)
agg["OPS"] = agg["SLG"] # simplified without OBP denominator at aggregate level
return agg
@st.cache_data(show_spinner=False)
def season_team_pitching(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
"""Aggregate team pitching stats by season."""
df = load_teamstats(min_year, max_year)
agg = df.groupby(["season", "team"]).agg(
G=("p_ipouts", "count"),
IPouts=("p_ipouts", "sum"),
HA=("p_h", "sum"),
HRA=("p_hr", "sum"),
RA=("p_r", "sum"),
ER=("p_er", "sum"),
BB=("p_w", "sum"),
SO=("p_k", "sum"),
WP=("p_wp", "sum"),
).reset_index()
agg["IP"] = (agg["IPouts"] / 3).round(1)
ip_s = agg["IP"].where(agg["IP"] > 0)
agg["ERA"] = (9 * agg["ER"] / ip_s).round(2)
agg["WHIP"] = ((agg["HA"] + agg["BB"]) / ip_s).round(3)
agg["K9"] = (9 * agg["SO"] / ip_s).round(2)
agg["BB9"] = (9 * agg["BB"] / ip_s).round(2)
agg["HR9"] = (9 * agg["HRA"] / ip_s).round(2)
return agg
@st.cache_data(show_spinner=False)
def season_standings(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
"""W/L standings per team per season with run differential (vectorized)."""
gi = load_gameinfo(min_year, max_year)
# Visitor perspective
vis = gi[["season", "visteam", "vruns", "hruns", "wteam", "lteam"]].copy()
vis.columns = ["season", "team", "RS", "RA", "wteam", "lteam"]
vis["W"] = (vis["team"] == vis["wteam"]).astype(int)
vis["L"] = (vis["team"] == vis["lteam"]).astype(int)
vis["home"] = 0
# Home perspective
hom = gi[["season", "hometeam", "hruns", "vruns", "wteam", "lteam"]].copy()
hom.columns = ["season", "team", "RS", "RA", "wteam", "lteam"]
hom["W"] = (hom["team"] == hom["wteam"]).astype(int)
hom["L"] = (hom["team"] == hom["lteam"]).astype(int)
hom["home"] = 1
df = pd.concat([vis, hom], ignore_index=True)
overall = df.groupby(["season", "team"]).agg(
W=("W", "sum"), L=("L", "sum"),
RS=("RS", "sum"), RA=("RA", "sum"),
).reset_index()
home_df = df[df["home"] == 1].groupby(["season", "team"]).agg(
Home_W=("W", "sum"), Home_L=("L", "sum"),
).reset_index()
away_df = df[df["home"] == 0].groupby(["season", "team"]).agg(
Away_W=("W", "sum"), Away_L=("L", "sum"),
).reset_index()
agg = overall.merge(home_df, on=["season", "team"]).merge(away_df, on=["season", "team"])
agg["G"] = agg["W"] + agg["L"]
agg["WPct"] = (agg["W"] / agg["G"]).round(3)
agg["RD"] = agg["RS"] - agg["RA"]
agg["RD_per_G"] = (agg["RD"] / agg["G"]).round(2)
agg["RS_per_G"] = (agg["RS"] / agg["G"]).round(2)
agg["RA_per_G"] = (agg["RA"] / agg["G"]).round(2)
agg["PythWPct"] = (agg["RS"] ** 2 / (agg["RS"] ** 2 + agg["RA"] ** 2)).round(3)
return agg.sort_values(["season", "WPct"], ascending=[True, False]).reset_index(drop=True)
@st.cache_data(show_spinner=False)
def head_to_head(team_a: str, team_b: str, min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
"""All games between two franchises in the date window."""
gi = load_gameinfo(min_year, max_year)
mask = (
((gi["visteam"] == team_a) & (gi["hometeam"] == team_b))
| ((gi["visteam"] == team_b) & (gi["hometeam"] == team_a))
)
df = gi[mask].copy()
df["a_runs"] = df.apply(lambda r: r["vruns"] if r["visteam"] == team_a else r["hruns"], axis=1)
df["b_runs"] = df.apply(lambda r: r["vruns"] if r["visteam"] == team_b else r["hruns"], axis=1)
df["a_win"] = (df["wteam"] == team_a).astype(int)
return df.sort_values("date").reset_index(drop=True)
@st.cache_data(show_spinner=False)
def rolling_team_form(team: str, window: int = 10, min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> pd.DataFrame:
"""Rolling N-game averages for a single team (runs scored/allowed, win rate)."""
gi = load_gameinfo(min_year, max_year)
vis = gi[gi["visteam"] == team][["date", "season", "vruns", "hruns", "wteam"]].copy()
vis.rename(columns={"vruns": "RS", "hruns": "RA"}, inplace=True)
hom = gi[gi["hometeam"] == team][["date", "season", "hruns", "vruns", "wteam"]].copy()
hom.rename(columns={"hruns": "RS", "vruns": "RA"}, inplace=True)
combined = pd.concat([vis, hom]).sort_values("date").reset_index(drop=True)
combined["W"] = (combined["wteam"] == team).astype(int)
for col in ("RS", "RA", "W"):
combined[f"roll_{col}_{window}"] = combined[col].rolling(window, min_periods=1).mean().round(3)
combined["roll_RD"] = combined[f"roll_RS_{window}"] - combined[f"roll_RA_{window}"]
return combined
@st.cache_data(show_spinner=False)
def season_batting_leaders(min_year: int = MODERN_START, max_year: int = datetime.date.today().year, min_pa: int = 300) -> pd.DataFrame:
"""Individual batter season totals, filtered to qualified hitters."""
bat = load_batting(min_year, max_year)
agg = bat.groupby(["season", "id", "team"], observed=False).agg(
PA=("b_pa", "sum"), AB=("b_ab", "sum"),
R=("b_r", "sum"), H=("b_h", "sum"),
doubles=("b_d", "sum"), triples=("b_t", "sum"),
HR=("b_hr", "sum"), RBI=("b_rbi", "sum"),
BB=("b_w", "sum"), K=("b_k", "sum"), SB=("b_sb", "sum"),
).reset_index()
agg = agg[agg["PA"] >= min_pa]
agg["singles"] = agg["H"] - agg["doubles"] - agg["triples"] - agg["HR"]
agg["BA"] = (agg["H"] / agg["AB"]).round(3)
agg["SLG"] = ((agg["singles"] + 2*agg["doubles"] + 3*agg["triples"] + 4*agg["HR"]) / agg["AB"]).round(3)
players = load_players(min_year, max_year)[["id", "full_name", "season"]].drop_duplicates()
agg = agg.merge(players, on=["id", "season"], how="left")
agg["full_name"] = agg["full_name"].fillna(agg["id"])
return agg.sort_values(["season", "BA"], ascending=[True, False]).reset_index(drop=True)
@st.cache_data(show_spinner=False)
def season_pitching_leaders(min_year: int = MODERN_START, max_year: int = datetime.date.today().year, min_ip: float = 100.0) -> pd.DataFrame:
"""Individual pitcher season totals, filtered to qualified starters."""
pit = load_pitching(min_year, max_year)
agg = pit.groupby(["season", "id", "team"], observed=False).agg(
IPouts=("p_ipouts", "sum"),
H=("p_h", "sum"), HR=("p_hr", "sum"),
R=("p_r", "sum"), ER=("p_er", "sum"),
BB=("p_w", "sum"), SO=("p_k", "sum"),
HBP=("p_hbp", "sum"), WP=("p_wp", "sum"),
GS=("p_gs", "sum"),
).reset_index()
agg["IP"] = (agg["IPouts"] / 3).round(1)
agg = agg[agg["IP"] >= min_ip]
ip_s = agg["IP"].where(agg["IP"] > 0)
bb_s = agg["BB"].where(agg["BB"] > 0)
agg["ERA"] = (9 * agg["ER"] / ip_s).round(2)
agg["WHIP"] = ((agg["H"] + agg["BB"]) / ip_s).round(3)
agg["K9"] = (9 * agg["SO"] / ip_s).round(2)
agg["BB9"] = (9 * agg["BB"] / ip_s).round(2)
agg["K_BB"] = (agg["SO"] / bb_s).round(2)
players = load_players(min_year, max_year)[["id", "full_name", "season"]].drop_duplicates()
agg = agg.merge(players, on=["id", "season"], how="left")
agg["full_name"] = agg["full_name"].fillna(agg["id"])
return agg.sort_values(["season", "ERA"]).reset_index(drop=True)
@st.cache_data(show_spinner=False)
def team_list(min_year: int = MODERN_START, max_year: int = datetime.date.today().year) -> list[str]:
gi = load_gameinfo(min_year, max_year)
teams = sorted(set(gi["visteam"].dropna()) | set(gi["hometeam"].dropna()))
return teams