Update functions/pitch_summary_functions.py
Browse files
functions/pitch_summary_functions.py
CHANGED
|
@@ -864,16 +864,16 @@ def plot_logo(pitcher_id: str, ax: plt.Axes,df_team: pl.DataFrame,df_players : p
|
|
| 864 |
return
|
| 865 |
|
| 866 |
splits = {
|
| 867 |
-
'
|
| 868 |
-
'
|
| 869 |
-
'
|
| 870 |
}
|
| 871 |
|
| 872 |
splits_title = {
|
| 873 |
|
| 874 |
-
'
|
| 875 |
-
'
|
| 876 |
-
'
|
| 877 |
|
| 878 |
}
|
| 879 |
|
|
@@ -910,6 +910,76 @@ def fangraphs_pitching_leaderboards(season: int,
|
|
| 910 |
df = pl.DataFrame(data=data['data'], infer_schema_length=1000)
|
| 911 |
return df
|
| 912 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 913 |
|
| 914 |
def fangraphs_table(df: pl.DataFrame,
|
| 915 |
ax: plt.Axes,
|
|
@@ -933,18 +1003,23 @@ def fangraphs_table(df: pl.DataFrame,
|
|
| 933 |
end_date = df['game_date'][-1]
|
| 934 |
|
| 935 |
# Fetch Fangraphs pitching leaderboards data
|
| 936 |
-
df_fangraphs = fangraphs_pitching_leaderboards(season=season,
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
|
| 941 |
-
df_fangraphs = df_fangraphs.with_columns(
|
| 942 |
-
|
| 943 |
|
| 944 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 945 |
|
| 946 |
# Select relevant columns for the table
|
| 947 |
-
plot_table = df_fangraphs.select(['IP', 'WHIP', 'ERA', 'TBF', 'FIP', 'K%', 'BB%', 'K-BB%'
|
| 948 |
|
| 949 |
# Format table values
|
| 950 |
plot_table_values = [format(plot_table[x][0], fangraphs_stats_dict[x]['format']) if plot_table[x][0] != '---' else '---' for x in plot_table.columns]
|
|
|
|
| 864 |
return
|
| 865 |
|
| 866 |
splits = {
|
| 867 |
+
'all':0,
|
| 868 |
+
'left':13,
|
| 869 |
+
'right':14,
|
| 870 |
}
|
| 871 |
|
| 872 |
splits_title = {
|
| 873 |
|
| 874 |
+
'all':'',
|
| 875 |
+
'left':' vs LHH',
|
| 876 |
+
'right':' vs RHH',
|
| 877 |
|
| 878 |
}
|
| 879 |
|
|
|
|
| 910 |
df = pl.DataFrame(data=data['data'], infer_schema_length=1000)
|
| 911 |
return df
|
| 912 |
|
| 913 |
+
def fangrpahs_splits_scrape(player_input: str, year_input: int, start_date: str, end_date: str, split: str):
|
| 914 |
+
### FANGRAPHS SPLITS SCRAPE ###
|
| 915 |
+
split_dict = {'all':[],
|
| 916 |
+
'left':['5'],
|
| 917 |
+
'right':['6']
|
| 918 |
+
}
|
| 919 |
+
import json
|
| 920 |
+
url = "https://www.fangraphs.com/api/leaders/splits/splits-leaders"
|
| 921 |
+
|
| 922 |
+
fg_id = str(fangraphs_pitching_leaderboards(year_input,
|
| 923 |
+
split='All',
|
| 924 |
+
start_date = f'{year_input}-01-01',
|
| 925 |
+
end_date = f'{year_input}-12-31').filter(pl.col('xMLBAMID')==player_input)['playerid'][0])
|
| 926 |
+
print('start_date',start_date)
|
| 927 |
+
print('start_date',end_date)
|
| 928 |
+
payload = {
|
| 929 |
+
"strPlayerId": str(fg_id),
|
| 930 |
+
"strSplitArr": split_dict[split],
|
| 931 |
+
"strGroup": "season",
|
| 932 |
+
"strPosition": "P",
|
| 933 |
+
"strType": "2",
|
| 934 |
+
"strStartDate": str(pd.to_datetime(start_date).strftime('%Y-%m-%d')),
|
| 935 |
+
"strEndDate": str(pd.to_datetime(end_date).strftime('%Y-%m-%d')),
|
| 936 |
+
"strSplitTeams": False,
|
| 937 |
+
"dctFilters": [],
|
| 938 |
+
"strStatType": "player",
|
| 939 |
+
"strAutoPt": False,
|
| 940 |
+
"arrPlayerId": [],
|
| 941 |
+
"strSplitArrPitch": [],
|
| 942 |
+
"arrWxTemperature": None,
|
| 943 |
+
"arrWxPressure": None,
|
| 944 |
+
"arrWxAirDensity": None,
|
| 945 |
+
"arrWxElevation": None,
|
| 946 |
+
"arrWxWindSpeed": None
|
| 947 |
+
}
|
| 948 |
+
json_payload = json.dumps(payload)
|
| 949 |
+
headers = {'Content-Type': 'application/json'}
|
| 950 |
+
response = requests.post(url, data=json_payload, headers=headers)
|
| 951 |
+
data_pull = response.json()['data'][0]
|
| 952 |
+
|
| 953 |
+
payload_advanced = {
|
| 954 |
+
"strPlayerId": str(fg_id),
|
| 955 |
+
"strSplitArr": split_dict[split],
|
| 956 |
+
"strGroup": "season",
|
| 957 |
+
"strPosition": "P",
|
| 958 |
+
"strType": "1",
|
| 959 |
+
"strStartDate": str(pd.to_datetime(start_date).strftime('%Y-%m-%d')),
|
| 960 |
+
"strEndDate": str(pd.to_datetime(end_date).strftime('%Y-%m-%d')),
|
| 961 |
+
"strSplitTeams": False,
|
| 962 |
+
"dctFilters": [],
|
| 963 |
+
"strStatType": "player",
|
| 964 |
+
"strAutoPt": False,
|
| 965 |
+
"arrPlayerId": [],
|
| 966 |
+
"strSplitArrPitch": [],
|
| 967 |
+
"arrWxTemperature": None,
|
| 968 |
+
"arrWxPressure": None,
|
| 969 |
+
"arrWxAirDensity": None,
|
| 970 |
+
"arrWxElevation": None,
|
| 971 |
+
"arrWxWindSpeed": None
|
| 972 |
+
}
|
| 973 |
+
|
| 974 |
+
json_payload_advanced = json.dumps(payload_advanced)
|
| 975 |
+
headers = {'Content-Type': 'application/json'}
|
| 976 |
+
response_advanced = requests.post(url, data=json_payload_advanced, headers=headers)
|
| 977 |
+
data_pull_advanced = response_advanced.json()['data'][0]
|
| 978 |
+
|
| 979 |
+
data_pull.update(data_pull_advanced)
|
| 980 |
+
df_pull = pl.DataFrame(data_pull)
|
| 981 |
+
return df_pull
|
| 982 |
+
|
| 983 |
|
| 984 |
def fangraphs_table(df: pl.DataFrame,
|
| 985 |
ax: plt.Axes,
|
|
|
|
| 1003 |
end_date = df['game_date'][-1]
|
| 1004 |
|
| 1005 |
# Fetch Fangraphs pitching leaderboards data
|
| 1006 |
+
# df_fangraphs = fangraphs_pitching_leaderboards(season=season,
|
| 1007 |
+
# split=split,
|
| 1008 |
+
# start_date=start_date,
|
| 1009 |
+
# end_date=end_date).filter(pl.col('xMLBAMID') == player_input)
|
| 1010 |
|
| 1011 |
+
# df_fangraphs = df_fangraphs.with_columns(
|
| 1012 |
+
# ((pl.col('Strikes')/pl.col('Pitches'))).alias('strikePercentage'),
|
| 1013 |
|
| 1014 |
+
# )
|
| 1015 |
+
df_fangraphs = fangrpahs_splits_scrape(player_input=player_input,
|
| 1016 |
+
year_input=season,
|
| 1017 |
+
start_date=start_date,
|
| 1018 |
+
end_date=start_date,
|
| 1019 |
+
split=split)
|
| 1020 |
|
| 1021 |
# Select relevant columns for the table
|
| 1022 |
+
plot_table = df_fangraphs.select(['IP', 'WHIP', 'ERA', 'TBF', 'FIP', 'K%', 'BB%', 'K-BB%'])
|
| 1023 |
|
| 1024 |
# Format table values
|
| 1025 |
plot_table_values = [format(plot_table[x][0], fangraphs_stats_dict[x]['format']) if plot_table[x][0] != '---' else '---' for x in plot_table.columns]
|