From 0a3659866d2eaf209d63fc9941c3af045eacd5e6 Mon Sep 17 00:00:00 2001 From: akern40 Date: Mon, 18 Apr 2022 07:40:46 -0400 Subject: [PATCH] Bug fix for `statcast_batter` (#256) * Bug fix for `statcast_batter` `statcast_batter` asserts that its parameters are not `None`, but does not reassign the parameters after the call to `sanitize_input`, leading to failure when `end_dt` is left as `None`. This commit reassigns the inputs from `sanitize_input`. `player_id` is not reassigned to avoid typing errors, since `sanitize_input` turns it into a string but `split_request` still expects an integer. * Adds tests for statcast_batter input Co-authored-by: Adam Kern --- pybaseball/statcast_batter.py | 2 +- .../pybaseball/data/statcast_batter_data.csv | 7 +++ tests/pybaseball/data/statcast_batter_raw.csv | 7 +++ tests/pybaseball/test_statcast_batter.py | 47 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/pybaseball/data/statcast_batter_data.csv create mode 100644 tests/pybaseball/data/statcast_batter_raw.csv create mode 100644 tests/pybaseball/test_statcast_batter.py diff --git a/pybaseball/statcast_batter.py b/pybaseball/statcast_batter.py index d1c6f06c..547201cf 100644 --- a/pybaseball/statcast_batter.py +++ b/pybaseball/statcast_batter.py @@ -17,7 +17,7 @@ def statcast_batter(start_dt: Optional[str] = None, end_dt: Optional[str] = None end_dt : YYYY-MM-DD : the final date for which you want data player_id : INT : the player's MLBAM ID. Find this by calling pybaseball.playerid_lookup(last_name, first_name), finding the correct player, and selecting their key_mlbam. """ - sanitize_input(start_dt, end_dt, player_id) + start_dt, end_dt, _ = sanitize_input(start_dt, end_dt, player_id) # sanitize_input will guarantee these are not None assert start_dt diff --git a/tests/pybaseball/data/statcast_batter_data.csv b/tests/pybaseball/data/statcast_batter_data.csv new file mode 100644 index 00000000..d76e975b --- /dev/null +++ b/tests/pybaseball/data/statcast_batter_data.csv @@ -0,0 +1,7 @@ +,pitch_type,game_date,release_speed,release_pos_x,release_pos_z,player_name,batter,pitcher,events,description,spin_dir,spin_rate_deprecated,break_angle_deprecated,break_length_deprecated,zone,des,game_type,stand,p_throws,home_team,away_team,type,hit_location,bb_type,balls,strikes,game_year,pfx_x,pfx_z,plate_x,plate_z,on_3b,on_2b,on_1b,outs_when_up,inning,inning_topbot,hc_x,hc_y,tfs_deprecated,tfs_zulu_deprecated,fielder_2,umpire,sv_id,vx0,vy0,vz0,ax,ay,az,sz_top,sz_bot,hit_distance_sc,launch_speed,launch_angle,effective_speed,release_spin_rate,release_extension,game_pk,pitcher.1,fielder_2.1,fielder_3,fielder_4,fielder_5,fielder_6,fielder_7,fielder_8,fielder_9,release_pos_y,estimated_ba_using_speedangle,estimated_woba_using_speedangle,woba_value,woba_denom,babip_value,iso_value,launch_speed_angle,at_bat_number,pitch_number,pitch_name,home_score,away_score,bat_score,fld_score,post_away_score,post_home_score,post_bat_score,post_fld_score,if_fielding_alignment,of_fielding_alignment,spin_axis,delta_home_win_exp,delta_run_exp +0,SI,2014-09-28,93.2,-0.6,5.71,"Jeter, Derek",116539,453329,single,hit_into_play,,,,,12,"Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.",R,R,R,BOS,NYY,X,5.0,ground_ball,1,2,2014,-0.97,0.82,1.07,2.68,400085.0,,,1,3,Top,112.45,180.72,,,424325,,140928_142509,6.58,-135.58,-3.65,-11.36,28.11,-23.95,3.52,1.67,,,,,,,382963,453329,424325,501800,605141,594555,457789,518490,628329,537953,50,,,0.9,,1.0,0.0,,18,4,Sinker,0,2,2,0,3,0,3,0,,,,-0.044,0.69 +1,FC,2014-09-28,90.2,-0.95,5.46,"Jeter, Derek",116539,453329,,foul,,,,,14,"Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.",R,R,R,BOS,NYY,S,,,1,1,2014,0.56,0.99,0.46,1.51,400085.0,,,1,3,Top,,,,,424325,,140928_142431,1.99,-131.28,-6.01,8.82,23.82,-22.4,3.52,1.67,,,,,,,382963,453329,424325,501800,605141,594555,457789,518490,628329,537953,50,,,,,,,,18,3,Cutter,0,2,2,0,2,0,2,0,,,,0.0,-0.106 +2,FF,2014-09-28,94.0,-0.75,5.51,"Jeter, Derek",116539,453329,,ball,,,,,14,"Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.",R,R,R,BOS,NYY,B,,,0,1,2014,-0.48,1.27,1.1,1.92,400085.0,,,1,3,Top,,,,,424325,,140928_142409,5.9,-136.63,-6.45,-4.88,26.67,-17.84,3.79,1.67,,,,,,,382963,453329,424325,501800,605141,594555,457789,518490,628329,537953,50,,,,,,,,18,2,4-Seam Fastball,0,2,2,0,2,0,2,0,,,,0.0,0.02 +3,FF,2014-09-28,94.3,-0.83,5.56,"Jeter, Derek",116539,453329,,called_strike,,,,,14,"Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.",R,R,R,BOS,NYY,S,,,0,0,2014,-0.38,1.42,0.83,1.8,400085.0,,,1,3,Top,,,,,424325,,140928_142343,5.15,-137.03,-7.22,-3.47,30.11,-15.94,3.62,1.67,,,,,,,382963,453329,424325,501800,605141,594555,457789,518490,628329,537953,50,,,,,,,,18,1,4-Seam Fastball,0,2,2,0,2,0,2,0,,,,0.0,-0.069 +4,FF,2014-09-28,92.9,-1.22,5.67,"Jeter, Derek",116539,453329,field_out,hit_into_play,,,,,5,Derek Jeter lines out to shortstop Jemile Weeks.,R,R,R,BOS,NYY,X,6.0,line_drive,0,1,2014,-0.51,1.39,-0.05,2.83,,,,1,1,Top,110.44,155.62,,,424325,,140928_134324,4.08,-135.2,-4.5,-5.12,27.6,-16.59,3.52,1.67,,,,,,,382963,453329,424325,501800,605141,594555,457789,518490,628329,537953,50,,,0.0,,0.0,0.0,,2,2,4-Seam Fastball,0,0,0,0,0,0,0,0,,,,0.015,-0.127 +5,FF,2014-09-28,93.3,-0.98,5.63,"Jeter, Derek",116539,453329,,swinging_strike,,,,,6,Derek Jeter lines out to shortstop Jemile Weeks.,R,R,R,BOS,NYY,S,,,0,0,2014,-0.45,1.48,0.73,2.46,,,,1,1,Top,,,,,424325,,140928_134303,5.39,-135.71,-5.58,-4.31,30.84,-15.45,3.52,1.67,,,,,,,382963,453329,424325,501800,605141,594555,457789,518490,628329,537953,50,,,,,,,,2,1,4-Seam Fastball,0,0,0,0,0,0,0,0,,,,0.0,-0.021 diff --git a/tests/pybaseball/data/statcast_batter_raw.csv b/tests/pybaseball/data/statcast_batter_raw.csv new file mode 100644 index 00000000..126aa735 --- /dev/null +++ b/tests/pybaseball/data/statcast_batter_raw.csv @@ -0,0 +1,7 @@ +"pitch_type","game_date","release_speed","release_pos_x","release_pos_z","player_name","batter","pitcher","events","description","spin_dir","spin_rate_deprecated","break_angle_deprecated","break_length_deprecated","zone","des","game_type","stand","p_throws","home_team","away_team","type","hit_location","bb_type","balls","strikes","game_year","pfx_x","pfx_z","plate_x","plate_z","on_3b","on_2b","on_1b","outs_when_up","inning","inning_topbot","hc_x","hc_y","tfs_deprecated","tfs_zulu_deprecated","fielder_2","umpire","sv_id","vx0","vy0","vz0","ax","ay","az","sz_top","sz_bot","hit_distance_sc","launch_speed","launch_angle","effective_speed","release_spin_rate","release_extension","game_pk","pitcher","fielder_2","fielder_3","fielder_4","fielder_5","fielder_6","fielder_7","fielder_8","fielder_9","release_pos_y","estimated_ba_using_speedangle","estimated_woba_using_speedangle","woba_value","woba_denom","babip_value","iso_value","launch_speed_angle","at_bat_number","pitch_number","pitch_name","home_score","away_score","bat_score","fld_score","post_away_score","post_home_score","post_bat_score","post_fld_score","if_fielding_alignment","of_fielding_alignment","spin_axis","delta_home_win_exp","delta_run_exp" +"SI","2014-09-28","93.2","-0.6","5.71","Jeter, Derek",116539,453329,"single","hit_into_play",,,,,"12","Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.","R","R","R","BOS","NYY","X","5","ground_ball","1","2",2014,"-0.97","0.82","1.07","2.68","400085",,,"1","3","Top",112.45,180.72,,,"424325",,"140928_142509",6.58,-135.58,-3.65,-11.36,28.11,-23.95,"3.52","1.67",,,,,,,382963,453329,"424325","501800","605141","594555","457789","518490","628329","537953","50",,,"0.9",,"1","0",,"18","4","Sinker","0","2","2","0","3","0","3","0",,,,"-0.044","0.69" +"FC","2014-09-28","90.2","-0.95","5.46","Jeter, Derek",116539,453329,,"foul",,,,,"14","Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.","R","R","R","BOS","NYY","S",,,"1","1",2014,"0.56","0.99","0.46","1.51","400085",,,"1","3","Top",,,,,"424325",,"140928_142431",1.99,-131.28,-6.01,8.82,23.82,-22.4,"3.52","1.67",,,,,,,382963,453329,"424325","501800","605141","594555","457789","518490","628329","537953","50",,,,,,,,"18","3","Cutter","0","2","2","0","2","0","2","0",,,,"0","-0.106" +"FF","2014-09-28","94.0","-0.75","5.51","Jeter, Derek",116539,453329,,"ball",,,,,"14","Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.","R","R","R","BOS","NYY","B",,,"0","1",2014,"-0.48","1.27","1.10","1.92","400085",,,"1","3","Top",,,,,"424325",,"140928_142409",5.9,-136.63,-6.45,-4.88,26.67,-17.84,"3.79","1.67",,,,,,,382963,453329,"424325","501800","605141","594555","457789","518490","628329","537953","50",,,,,,,,"18","2","4-Seam Fastball","0","2","2","0","2","0","2","0",,,,"0","0.02" +"FF","2014-09-28","94.3","-0.83","5.56","Jeter, Derek",116539,453329,,"called_strike",,,,,"14","Derek Jeter singles on a ground ball to shortstop Jemile Weeks, deflected by third baseman Garin Cecchini. Ichiro Suzuki scores.","R","R","R","BOS","NYY","S",,,"0","0",2014,"-0.38","1.42","0.83","1.80","400085",,,"1","3","Top",,,,,"424325",,"140928_142343",5.15,-137.03,-7.22,-3.47,30.11,-15.94,"3.62","1.67",,,,,,,382963,453329,"424325","501800","605141","594555","457789","518490","628329","537953","50",,,,,,,,"18","1","4-Seam Fastball","0","2","2","0","2","0","2","0",,,,"0","-0.069" +"FF","2014-09-28","92.9","-1.22","5.67","Jeter, Derek",116539,453329,"field_out","hit_into_play",,,,,"5","Derek Jeter lines out to shortstop Jemile Weeks.","R","R","R","BOS","NYY","X","6","line_drive","0","1",2014,"-0.51","1.39","-0.05","2.83",,,,"1","1","Top",110.44,155.62,,,"424325",,"140928_134324",4.08,-135.2,-4.5,-5.12,27.6,-16.59,"3.52","1.67",,,,,,,382963,453329,"424325","501800","605141","594555","457789","518490","628329","537953","50",,,"0",,"0","0",,"2","2","4-Seam Fastball","0","0","0","0","0","0","0","0",,,,"0.015","-0.127" +"FF","2014-09-28","93.3","-0.98","5.63","Jeter, Derek",116539,453329,,"swinging_strike",,,,,"6","Derek Jeter lines out to shortstop Jemile Weeks.","R","R","R","BOS","NYY","S",,,"0","0",2014,"-0.45","1.48","0.73","2.46",,,,"1","1","Top",,,,,"424325",,"140928_134303",5.39,-135.71,-5.58,-4.31,30.84,-15.45,"3.52","1.67",,,,,,,382963,453329,"424325","501800","605141","594555","457789","518490","628329","537953","50",,,,,,,,"2","1","4-Seam Fastball","0","0","0","0","0","0","0","0",,,,"0","-0.021" \ No newline at end of file diff --git a/tests/pybaseball/test_statcast_batter.py b/tests/pybaseball/test_statcast_batter.py new file mode 100644 index 00000000..e9739ba5 --- /dev/null +++ b/tests/pybaseball/test_statcast_batter.py @@ -0,0 +1,47 @@ +from typing import Callable + +import pandas as pd +import pytest +from tests.pybaseball.conftest import GetDataFrameCallable + +from pybaseball.statcast_batter import statcast_batter + + +@pytest.fixture(name="single_day_raw") +def _single_day_raw(get_data_file_contents: Callable[[str], str]) -> str: + return get_data_file_contents("statcast_batter_raw.csv") + + +@pytest.fixture(name="single_day") +def _single_day(get_data_file_dataframe: GetDataFrameCallable) -> pd.DataFrame: + return get_data_file_dataframe("statcast_batter_data.csv") + + +def test_statcast_batter_input_handling( + response_get_monkeypatch: Callable, + single_day_raw: str, + single_day: pd.DataFrame, +) -> None: + """ + Test whether `statcast_batter` correctly handles optional start and end dates. + + Parameters + ---------- + response_get_monkeypatch : Callable + The response monkeypatch function + single_day_raw : str + The raw csv result from the request + single_day : pd.DataFrame + The processed DataFrame expected + """ + pid = 116539 # Derek Jeter + dt = "2014-09-28" # Last day + + url_end = "&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&" + response_get_monkeypatch(single_day_raw, url_end) + + res = statcast_batter(start_dt=dt, player_id=pid) + pd.testing.assert_frame_equal(res, single_day, check_dtype=False) + + res = statcast_batter(end_dt=dt, player_id=pid) + pd.testing.assert_frame_equal(res, single_day, check_dtype=False)