Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Arm angle data and fixed Statcast Tests #458

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion docs/statcast_batter.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,25 @@ data = statcast_batter_pitch_arsenal(2019)

# get data for batters with a minimum of 100 plate appearances in 2019
data = statcast_batter_pitch_arsenal(2019, 100)
```

```
# Statcast Batter Bat Tracking
`statcast_batter_bat_tracking(year, minSwings=q)`

This functions retrives a player\'s bat tracking data in a given year.

## Arguments:
`year:` The year for which you wish to retrieve the bat tracking data. Format: YYYY.

`minSwings:` The minimum number of competitive swings for wach player. If a player falls below this threshold, they will be excluded from the results. If no value is specified, the default number of competitive swings is qualified.

## Examples of Valid Queries
```python
from pybaseball import statcast_batter_bat_tracking

# Get data for all qualified batters in 2024
data = statcast_batter_bat_tracking(2024)

# Get data for batters with a minimum of 250 swings in 2024
data = statcast_batter_bat_tracking(2024, 250)
```
40 changes: 40 additions & 0 deletions docs/statcast_pitcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,43 @@ data = statcast_pitcher_spin_dir_comp(2020, pitch_a="Sinker", pitch_b="Slider",
# get data for sinker / slider combos in 2020 using pitch codes and from the batter's POV
data = statcast_pitcher_spin_dir_comp(2020, pitch_a="SIFT", pitch_b="SL", pitcher_pov=False)
```
# Statcast Pitcher Bat Tracking
`statcast_pitcher_bat_tracking(year, minSwings=q)`

This function retrives the bat tracking data against for pitchers.

## Arguments
`year:` The year for which you wish to retrieve bat tracking data. Format: YYYY

`minSwings:` The minimum number of swings batters have taken against a pitcher. If a pitcher falls belows the threshold, they will be excluded from the results. The default value is qualified.

## Examples of Valid Queries
```python
from pybaseball import statcast_pitcher_bat_tracking

# Get data for all qualified pitchers in 2024
data = statcast_pitcher_bat_tracking(2024)

#Get data for all pitchers with a minimum of 250 swings against in 2024
data = statcast_pitcher_bat_tracking(2024, 250)
```
# Statcast Pitcher Arm Angle
`statcast_pitcher_arm_angle(year, minP=q)`

This function retrieves pitcher arm angle data.

## Arguments
`year:` The year for which you wish to retrieve arm angle data. Format: YYYY

`minP:` The minimum number of pitchers thrown. If a player falls below this threshold, they will be excluded from the results. The default value if no argument is passed is qualified.

## Examples of Valid Queries
```python
from pybaseball import statcast_pitcher_arm_angle

#Get data for all qualified pitchers in 2024
data = statcast_pitcher_arm_angle(2024)

#Get data for all pitchers with a minimum of 100 pitches thrown in 2024
data = statcast_pitcher_arm_angle(2024, 100)
```
1 change: 0 additions & 1 deletion pybaseball/datasources/bref.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ def get(self, url: str, **kwargs: Any) -> requests.Response:
self.last_request = datetime.datetime.now()

return self.session.get(url, **kwargs)

12 changes: 7 additions & 5 deletions pybaseball/statcast_batter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests

from . import cache
from .utils import sanitize_input, split_request, sanitize_statcast_columns
from .utils import sanitize_input, split_request, sanitize_statcast_columns, get_season


def statcast_batter(start_dt: Optional[str] = None, end_dt: Optional[str] = None, player_id: Optional[int] = None) -> pd.DataFrame:
Expand All @@ -25,7 +25,9 @@ def statcast_batter(start_dt: Optional[str] = None, end_dt: Optional[str] = None
assert end_dt
assert player_id

url = 'https://baseballsavant.mlb.com/statcast_search/csv?all=true&hfPT=&hfAB=&hfBBT=&hfPR=&hfZ=&stadium=&hfBBL=&hfNewZones=&hfGT=R%7CPO%7CS%7C=&hfSea=&hfSit=&player_type=batter&hfOuts=&opponent=&pitcher_throws=&batter_stands=&hfSA=&game_date_gt={}&game_date_lt={}&batters_lookup%5B%5D={}&team=&position=&hfRO=&home_road=&hfFlag=&metric_1=&hfInn=&min_pitches=0&min_results=0&group_by=name&sort_col=pitches&player_event_sort=h_launch_speed&sort_order=desc&min_abs=0&type=details&'
season = get_season(start_dt, end_dt) #Needs season for query to work

url = f'https://baseballsavant.mlb.com/statcast_search/csv?hfPT=&hfAB=&hfGT=R%7C&hfPR=&hfZ=&hfStadium=&hfBBL=&hfNewZones=&hfPull=&hfC=&hfSea={season}&hfSit=&player_type=pitcher&hfOuts=&hfOpponent=&pitcher_throws=&batter_stands=&hfSA=&game_date_gt={start_dt}&game_date_lt={end_dt}&hfMo=&hfTeam=&home_road=&hfRO=&position=&hfInfield=&hfOutfield=&hfInn=&hfBBT=&batters_lookup%5B%5D={player_id}&hfFlag=&metric_1=&group_by=name&min_pitches=0&min_results=0&min_pas=0&sort_col=pitches&player_event_sort=api_p_release_speed&sort_order=desc&type=details&all=true&minors=false'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original hfGT is &hfGT=R%7CPO%7CS%7C= which means it would include PO for Playoff data and S for Spring Training data.
Not sure whether should we keep it or not

df = split_request(start_dt, end_dt, player_id, url)
return df

Expand Down Expand Up @@ -56,7 +58,7 @@ def statcast_batter_expected_stats(year: int, minPA: Union[int, str] = "q") -> p
minPA: The minimum number of plate appearances for each player. If a player falls below this threshold,
they will be excluded from the results. If no value is specified, only qualified batters will be returned.
"""
url = f"https://baseballsavant.mlb.com/leaderboard/expected_statistics?type=batter&year={year}&position=&team=&min={minPA}&csv=true"
url = f"https://baseballsavant.mlb.com/leaderboard/expected_statistics?type=batter&year={year}&position=&team=&filterType=pa&min={minPA}&csv=true"
res = requests.get(url, timeout=None).content
data = pd.read_csv(io.StringIO(res.decode('utf-8')))
data = sanitize_statcast_columns(data)
Expand Down Expand Up @@ -98,8 +100,8 @@ def statcast_batter_bat_tracking(year: int, minSwings: Union[int,str] = "q" ) ->
Retrieves a player's bat tracking data for a given year.

ARGUMENTS
year: The year for which you which to retrieve the bat tracking data. Format: YYYY.
minSwings: The minimum number of competitive swings for wach player. If a player falls below this threshold,
year: The year for which you wish to retrieve the bat tracking data. Format: YYYY.
minSwings: The minimum number of competitive swings for each player. If a player falls below this threshold,
they will be excluded from the results. If no value is specified, the default number of competitive swings
is qualified.
"""
Expand Down
18 changes: 17 additions & 1 deletion pybaseball/statcast_pitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def statcast_pitcher_expected_stats(year: int, minPA: Union[int, str] = "q") ->
minPA: The minimum number of plate appearances against for each player. If a player falls below this threshold,
they will be excluded from the results. If no value is specified, only qualified pitchers will be returned.
"""
url = f"https://baseballsavant.mlb.com/leaderboard/expected_statistics?type=pitcher&year={year}&position=&team=&min={minPA}&csv=true"
url = f"https://baseballsavant.mlb.com/leaderboard/expected_statistics?type=pitcher&year={year}&position=&team=&filterType=pa&min={minPA}&csv=true"
res = requests.get(url, timeout=None).content
data = pd.read_csv(io.StringIO(res.decode('utf-8')))
data = sanitize_statcast_columns(data)
Expand Down Expand Up @@ -216,3 +216,19 @@ def statcast_pitcher_bat_tracking(year: int, minSwings: Union[int,str] = "q") ->
data = pd.read_csv(io.StringIO(res.decode('utf-8')))
data = sanitize_statcast_columns(data)
return data
@cache.df_cache()
def statcast_pitcher_arm_angle(year: int, minP: Union[int,str] = "q") -> pd.DataFrame:
"""
Retreives pitcher arm angle data.

ARGUMENTS
year: The year for which you wish to retrive arm angle data. Format: YYYY
minP: The minimum number of pitches thrown. If a player falls below this threshold, they will be excluded from
the results. The default value if no argument is passed is qualified.
"""
url = f"https://baseballsavant.mlb.com/leaderboard/pitcher-arm-angles?season={year}&team=&pitchHand=&min={minP}&sort=ascending&csv=true"
res = requests.get(url, timeout=None).content
data = pd.read_csv(io.StringIO(res.decode('utf-8')))
data = sanitize_statcast_columns(data)
return data

Loading
Loading