-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_pitching_data.py
More file actions
67 lines (55 loc) · 1.69 KB
/
get_pitching_data.py
File metadata and controls
67 lines (55 loc) · 1.69 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
"""
MLB Pitching Data
Author: Patrick Mejia
Date: 6-13-2025
"""
import requests
import polars as pl
def get_pitching_data(year: int) -> pl.DataFrame:
"""
Gets the pitching data by year.
Args:
year (int): The year of the rankings.
Returns:
pl.DataFrame: A Polars DataFrame of pitching stats.
"""
try:
url = (
f"https://statsapi.mlb.com/api/v1/stats"
f"?stats=season&group=pitching&gameType=R&season={year}"
)
response = requests.get(url)
response.raise_for_status()
json_data = response.json()
records = []
for player_stat in json_data["stats"][0]["splits"]:
player = player_stat["player"]
stat = player_stat["stat"]
record = {
"Name": f"{player['firstName']} {player['lastName']}",
"Year": year,
**stat
}
records.append(record)
df = pl.DataFrame(records)
except Exception as e:
print(f"Error fetching pitching data for {year}: {e}")
return pl.DataFrame()
print(df)
df.write_csv(f"Stats/Pitching/mlb_pitching_data_{year}.csv")
return df
def main():
total_df = []
for year in range(2020, 2025):
print(f"{year} Pitching Stats")
df = get_pitching_data(year)
if df.height > 0:
total_df.append(df)
if total_df:
combined_df = pl.concat(total_df, how="vertical")
combined_df.write_csv("Stats/Pitching/full_mlb_pitching_data.csv")
print("Combined CSV saved as: Stats/full_mlb_pitching_data.csv")
else:
print("No pitching data found.")
if __name__ == "__main__":
main()