-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_milb_pbp.py
1193 lines (1030 loc) · 40.4 KB
/
get_milb_pbp.py
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import json
import os
import platform
import random
import time
import warnings
from datetime import datetime
from urllib.request import urlopen
import numpy as np
import pandas as pd
from tqdm import tqdm
from get_milb_schedule import load_milb_schedule
warnings.filterwarnings("ignore", category=FutureWarning)
def get_milb_game_pbp(game_id: int, cache_data=False, cache_dir=""):
"""
Retrieves and parses the play-by-play (PBP) data from a valid
MiLB game ID.
Parameters
----------
`game_id`: (int, mandatory):
The MiLB game ID you want PBP data from.
`cache_data`: (bool, optional) = `False`:
Optional boolean flag.
If set to `True`, data downloaded by this function will be cached to
a folder named `./milb/`.
This folder will either be located in the user's home directory,
or in a existing directory
specified by the optional argument `cache_dir`.
`cache_dir`: (str, optional) = `""`:
Optional string. If not set to `""` or `None`,
this will be the directory used to cache data
if `cache_data` is set to `True`.
This directory must exist prior to running this function!
Returns
----------
A pandas `DataFrame` object containing PBP data from
the MiLB game ID.
"""
has_lineups = False
if cache_data is True \
and (cache_dir == "" or cache_dir is None):
home_dir = os.path.expanduser("~")
try:
os.mkdir(f"{home_dir}/.milb/")
except Exception:
pass
try:
os.mkdir(f"{home_dir}/.milb/pbp/")
os.mkdir(f"{home_dir}/.milb/lineups/")
except Exception:
pass
elif cache_data is True \
and (cache_dir != "" or cache_dir is not None):
try:
os.mkdir(f"{cache_dir}/.milb/")
except Exception:
pass
try:
os.mkdir(f"{cache_dir}/.milb/pbp/")
os.mkdir(f"{cache_dir}/.milb/lineups/")
except Exception:
pass
game_df = pd.DataFrame()
play_df = pd.DataFrame()
lineups_url = "https://statsapi.mlb.com/api/v1/schedule" +\
f"?gamePk={game_id}&language=en&hydrate=story,xrefId," +\
"lineups,broadcasts(all),probablePitcher(note),game(tickets)" + \
"&useLatestGames=true&fields=dates,games,teams,probablePitcher," + \
"note,id,dates,games,broadcasts,type,name,homeAway,isNational," +\
"dates,games,game,tickets,ticketType,ticketLinks,dates,games," +\
"lineups,homePlayers,awayPlayers,useName,lastName," +\
"primaryPosition,abbreviation,dates,games," +\
"xrefIds,xrefId,xrefType,story"
game_url = f"https://statsapi.mlb.com/api/v1.1/game/{game_id}/feed/live?"
# Baseball Positions
##########################################################################
# 1 = Pitcher
# 2 = Catcher
# 3 = 1B
# 4 = 2B
# 5 = 3B
# 6 = SS
# 7 = LF
# 8 = CF
# 9 = RF
# away_fielders = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# home_fielders = [1, 2, 3, 4, 5, 6, 7, 8, 9]
away_fielders = [None, None, None, None, None, None, None, None, None]
home_fielders = [None, None, None, None, None, None, None, None, None]
away_score = 0
home_score = 0
if cache_data is True and (cache_dir == "" or cache_dir is None):
# Cached files, default directory
try:
with open(f"{home_dir}/.milb/lineups/{game_id}.json", "r") as f:
json_string = f.read()
json_data = json.loads(json_string)
has_lineups = True
del json_string
except Exception:
response = urlopen(lineups_url)
time.sleep(1)
if response.code == 200:
pass
elif response.code == 403:
raise ConnectionRefusedError(
"The MiLB API is actively refusing your connection." +
"\nHTTP Error Code:\t403"
)
else:
raise ConnectionError(
"Could not establish a connection to the MiLB API." +
f"\nHTTP Error Code:\t{response.code}"
)
json_data = json.loads(response.read())
try:
with open(
f"{home_dir}/.milb/lineups/{game_id}.json", "w+"
) as f:
f.write(json.dumps(json_data, indent=2))
has_lineups = True
except Exception:
has_lineups = False
elif cache_data is True and (cache_dir != "" or cache_dir is not None):
# Cached files, custom directory
try:
with open(f"{cache_dir}/.milb/lineups/{game_id}.json", "r") as f:
json_string = f.read()
json_data = json.loads(json_string)
del json_string
has_lineups = True
except Exception:
response = urlopen(lineups_url)
time.sleep(1)
if response.code == 200:
pass
elif response.code == 403:
raise ConnectionRefusedError(
"The MiLB API is actively refusing your connection." +
"\nHTTP Error Code:\t403"
)
else:
raise ConnectionError(
"Could not establish a connection to the MiLB API." +
f"\nHTTP Error Code:\t{response.code}"
)
try:
json_data = json.loads(response.read())
with open(
f"{cache_dir}/.milb/lineups/{game_id}.json", "w+"
) as f:
f.write(json.dumps(json_data, indent=2))
has_lineups = True
except Exception:
has_lineups = False
else:
# No cached files used.
response = urlopen(lineups_url)
time.sleep(1)
try:
if response.code == 200:
pass
elif response.code == 403:
raise ConnectionRefusedError(
"The MiLB API is actively refusing your connection." +
"\nHTTP Error Code:\t403"
)
else:
raise ConnectionError(
"Could not establish a connection to the MiLB API." +
f"\nHTTP Error Code:\t{response.code}"
)
json_data = json.loads(response.read())
has_lineups = True
except Exception:
has_lineups = False
if has_lineups is True:
try:
json_data = json_data["dates"][0]["games"][0]["lineups"]
for i in json_data["awayPlayers"]:
player_id = i["id"]
player_pos = i["primaryPosition"]["abbreviation"]
match player_pos:
case "DH":
# The designated hitter is not a fielder.
# Thus, we can skip this player.
continue
case "P":
away_fielders[0] = player_id
case "C":
away_fielders[1] = player_id
case "1B":
away_fielders[2] = player_id
case "2B":
away_fielders[3] = player_id
case "3B":
away_fielders[4] = player_id
case "SS":
away_fielders[5] = player_id
case "LF":
away_fielders[6] = player_id
case "CF":
away_fielders[7] = player_id
case "RF":
away_fielders[8] = player_id
case _:
raise ValueError(
"Unhandled starting player position:" +
f"\n\t{player_pos}"
)
for i in json_data["homePlayers"]:
player_id = i["id"]
player_pos = i["primaryPosition"]["abbreviation"]
match player_pos:
case "DH":
# The designated hitter is not a fielder.
# Thus, we can skip this player.
continue
case "P":
away_fielders[0] = player_id
case "C":
home_fielders[1] = player_id
case "1B":
home_fielders[2] = player_id
case "2B":
home_fielders[3] = player_id
case "3B":
home_fielders[4] = player_id
case "SS":
home_fielders[5] = player_id
case "LF":
home_fielders[6] = player_id
case "CF":
home_fielders[7] = player_id
case "RF":
home_fielders[8] = player_id
case _:
raise ValueError(
"Unhandled starting player position:" +
f"\n\t{player_pos}"
)
except Exception:
print(f"Lineups data not found for {game_id}")
else:
print(f"Lineups data not found for {game_id}")
if cache_data is True and (cache_dir == "" or cache_dir is None):
# Cached files, default directory
try:
with open(f"{home_dir}/.milb/pbp/{game_id}.json", "r") as f:
json_string = f.read()
json_data = json.loads(json_string)
except Exception:
response = urlopen(game_url)
time.sleep(1)
if response.code == 200:
pass
elif response.code == 403:
raise ConnectionRefusedError(
"The MiLB API is actively refusing your connection." +
"\nHTTP Error Code:\t403"
)
else:
raise ConnectionError(
"Could not establish a connection to the MiLB API." +
f"\nHTTP Error Code:\t{response.code}"
)
json_data = json.loads(response.read())
with open(f"{home_dir}/.milb/pbp/{game_id}.json", "w+") as f:
f.write(json.dumps(json_data, indent=2))
elif cache_data is True and (cache_dir != "" or cache_dir is not None):
try:
with open(f"{cache_dir}/.milb/pbp/{game_id}.json", "r") as f:
json_string = f.read()
json_data = json.loads(json_string)
except Exception:
response = urlopen(game_url)
time.sleep(1)
if response.code == 200:
pass
elif response.code == 403:
raise ConnectionRefusedError(
"The MiLB API is actively refusing your connection." +
"\nHTTP Error Code:\t403"
)
else:
raise ConnectionError(
"Could not establish a connection to the MiLB API." +
f"\nHTTP Error Code:\t{response.code}"
)
json_data = json.loads(response.read())
with open(f"{cache_dir}/.milb/pbp/{game_id}.json", "w+") as f:
f.write(json.dumps(json_data, indent=2))
else:
# No cached files used.
response = urlopen(game_url)
time.sleep(1)
if response.code == 200:
pass
elif response.code == 403:
raise ConnectionRefusedError(
"The MiLB API is actively refusing your connection." +
"\nHTTP Error Code:\t403"
)
else:
raise ConnectionError(
"Could not establish a connection to the MiLB API." +
f"\nHTTP Error Code:\t{response.code}"
)
json_data = json.loads(response.read())
# json_data = json.loads(response.read())
if len(json_data) == 0:
print(f"\nCould not get PBP data for game ID {game_id}")
return pd.DataFrame()
game_type = json_data["gameData"]["game"]["type"]
game_date = json_data["gameData"]["datetime"]["officialDate"]
game_date = datetime.strptime(game_date, "%Y-%m-%d")
game_date_str = game_date.strftime("%Y-%m-%d")
league_id = json_data["gameData"]["teams"]["away"]["league"]["id"]
league_name = json_data["gameData"]["teams"]["away"]["league"]["name"]
league_level_id = json_data["gameData"]["teams"]["away"]["sport"]["id"]
league_level_name = json_data["gameData"]["teams"]["away"]["sport"]["name"]
try:
away_team_org_id = json_data[
"gameData"]["teams"]["away"]["parentOrgId"]
except Exception:
away_team_org_id = None
try:
away_team_org_name = json_data[
"gameData"]["teams"]["away"]["parentOrgName"]
except Exception:
away_team_org_name = None
try:
away_team_abv = json_data["gameData"]["teams"]["away"]["abbreviation"]
except Exception:
away_team_abv = None
try:
home_team_org_id = json_data[
"gameData"]["teams"]["home"]["parentOrgId"]
except Exception:
home_team_org_id = None
try:
home_team_org_name = json_data[
"gameData"]["teams"]["home"]["parentOrgName"]
except Exception:
home_team_org_name = None
try:
home_team_abv = json_data["gameData"]["teams"]["home"]["abbreviation"]
except Exception:
home_team_abv = None
for i in tqdm(json_data["liveData"]["plays"]["allPlays"]):
at_bat_num = i["atBatIndex"]
player_name = i["matchup"]["pitcher"]["fullName"]
batter_id = i["matchup"]["batter"]["id"]
batter_side = i["matchup"]["batSide"]["code"]
pitcher_id = i["matchup"]["pitcher"]["id"]
pitcher_hand = i["matchup"]["pitchHand"]["code"]
try:
event = i["result"]["event"]
except Exception:
event = None
try:
sequence_description = i["result"]["description"]
except Exception:
sequence_description = None
inning = i["about"]["inning"]
top_bot = i["about"]["halfInning"]
if top_bot.lower() == "top":
top_bot = "Top"
elif top_bot.lower() == "bottom":
top_bot = "Bot"
else:
raise IndexError(f"Unhandled baseball inning state:\t{top_bot}")
post_away_score = i["result"]["awayScore"]
post_home_score = i["result"]["homeScore"]
try:
on_1b = i["matchup"]["postOnFirst"]["id"]
except Exception:
on_1b = None
try:
on_2b = i["matchup"]["postOnSecond"]["id"]
except Exception:
on_2b = None
try:
on_3b = i["matchup"]["postOnThird"]["id"]
except Exception:
on_3b = None
if top_bot == "Top": # Home pitching
bat_score = away_score
fld_score = home_score
post_bat_score = post_away_score
post_fld_score = post_home_score
elif top_bot == "Bot": # Away pitching
bat_score = home_score
fld_score = away_score
post_bat_score = post_home_score
post_fld_score = post_away_score
else:
raise ValueError(f"Unhandled inning state:\n\t{top_bot}")
for j in i["playEvents"]:
try:
play_start_datetime = datetime.strptime(
j["startTime"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
except Exception:
play_start_datetime = None
try:
play_end_datetime = datetime.strptime(
j["endTime"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
except Exception:
play_end_datetime = None
is_pitch = j["isPitch"]
try:
event_type = j["details"]["eventType"]
except Exception:
event_type = None
try:
play_description = j["details"]["description"]
except Exception:
play_description = None
try:
play_code = j["details"]["code"]
except Exception:
play_code = None
if event_type == "pitching_substitution":
# This should be handled normally within this API's structure,
# but this code is here if for any reason,
# the MiLB API spits out a play/sequence
# where a pitcher get's injured during an at bat,
# and absolutely must be subbed out.
pitcher_id = j["player"]["id"]
elif play_code == "AC":
# Automatic Strike because of a pitch clock violation
# committed by the batter.
pass
elif event_type == "defensive_substitution" and top_bot == "Top":
subbed_position = int(j["position"]["code"])
if subbed_position == 10:
pass
else:
new_player_id = j["player"]["id"]
home_fielders[(subbed_position - 1)] = new_player_id
del new_player_id, subbed_position
elif event_type == "defensive_substitution" and top_bot == "Bot":
subbed_position = int(j["position"]["code"])
if subbed_position == 10:
pass
else:
new_player_id = j["player"]["id"]
away_fielders[(subbed_position - 1)] = new_player_id
del new_player_id, subbed_position
elif event_type == "defensive_switch" and top_bot == "Top":
subbed_position = int(j["position"]["code"])
if subbed_position == 10:
pass
else:
new_player_id = j["player"]["id"]
home_fielders[(subbed_position - 1)] = new_player_id
del new_player_id, subbed_position
elif event_type == "defensive_switch" and top_bot == "Bot":
subbed_position = int(j["position"]["code"])
if subbed_position == 10:
pass
else:
new_player_id = j["player"]["id"]
away_fielders[(subbed_position - 1)] = new_player_id
del new_player_id, subbed_position
elif event_type == "offensive_substitution" and top_bot == "Top":
batter_id = j["player"]["id"]
elif event_type == "offensive_substitution":
batter_id = j["player"]["id"]
elif event_type == "batter_timeout":
pass
elif event_type == "wild_pitch":
pass
elif play_description == "Pitcher Step Off":
pass
elif play_description == "Pickoff Attempt 1B":
pass
elif play_description == "Pickoff Attempt 2B":
pass
elif play_description == "Pickoff Attempt 3B":
pass
elif event_type == "stolen_base_1b":
pass
elif event_type == "stolen_base_2b":
pass
elif event_type == "stolen_base_3b":
pass
elif event_type == "mound_visit":
pass
elif event_type == "game_advisory":
# These plays can be skipped for our purposes.
# This just means that either a delay (of any kind)
# happened, or the status of the game is being changed
# (typically from "awaiting for the start of the game"
# to "game's started now").
pass
elif is_pitch is False:
pass
else:
# Fielders
if top_bot == "Top": # Home team pitching
fielder_2 = home_fielders[1]
fielder_3 = home_fielders[2]
fielder_4 = home_fielders[3]
fielder_5 = home_fielders[4]
fielder_6 = home_fielders[5]
fielder_7 = home_fielders[6]
fielder_8 = home_fielders[7]
fielder_9 = home_fielders[8]
elif top_bot == "Bot": # Away team pitching
fielder_2 = away_fielders[1]
fielder_3 = away_fielders[2]
fielder_4 = away_fielders[3]
fielder_5 = away_fielders[4]
fielder_6 = away_fielders[5]
fielder_7 = away_fielders[6]
fielder_8 = away_fielders[7]
fielder_9 = away_fielders[8]
else:
raise ValueError(f"Unhandled inning state:\n\t{top_bot}")
try:
pitch_num = j["pitchNumber"]
except Exception:
pitch_num = None
play_balls = j["count"]["balls"]
play_strikes = j["count"]["strikes"]
play_outs = j["count"]["outs"]
try:
pitch_type = j["details"]["type"]["code"]
except Exception:
pitch_type = None
try:
pitch_name = j["details"]["type"]["description"]
except Exception:
pitch_name = None
try:
release_speed = j["pitchData"]["startSpeed"]
except Exception:
release_speed = None
try:
release_pos_x = j["pitchData"]["coordinates"]["x0"]
except Exception:
release_pos_x = None
try:
release_pos_y = j["pitchData"]["coordinates"]["y0"]
except Exception:
release_pos_y = None
try:
release_pos_z = j["pitchData"]["coordinates"]["z0"]
except Exception:
release_pos_z = None
try:
plate_x = j["pitchData"]["coordinates"]["pX"]
except Exception:
plate_x = None
try:
plate_z = j["pitchData"]["coordinates"]["pZ"]
except Exception:
plate_z = None
try:
pitch_pfx_x = j["pitchData"]["coordinates"]["pfxX"]
except Exception:
pitch_pfx_x = None
try:
pitch_pfx_z = j["pitchData"]["coordinates"]["pfxZ"]
except Exception:
pitch_pfx_z = None
try:
pitch_spin_dir = j["pitchData"]["breaks"]["spinDirection"]
except Exception:
pitch_spin_dir = None
try:
pitch_zone = j["pitchData"]["zone"]
except Exception:
pitch_zone = None
try:
pitch_vx0 = j["pitchData"]["coordinates"]["vX0"]
except Exception:
pitch_vx0 = None
try:
pitch_vy0 = j["pitchData"]["coordinates"]["vY0"]
except Exception:
pitch_vy0 = None
try:
pitch_vz0 = j["pitchData"]["coordinates"]["vZ0"]
except Exception:
pitch_vz0 = None
try:
pitch_ax = j["pitchData"]["coordinates"]["aX"]
except Exception:
pitch_ax = None
try:
pitch_ay = j["pitchData"]["coordinates"]["aY"]
except Exception:
pitch_ay = None
try:
pitch_az = j["pitchData"]["coordinates"]["aZ"]
except Exception:
pitch_az = None
try:
pitch_spin_rate = j["pitchData"]["breaks"]["spinRate"]
except Exception:
pitch_spin_rate = None
try:
pitch_extension = j["pitchData"]["extension"]
except Exception:
pitch_extension = None
strike_zone_top = j["pitchData"]["strikeZoneTop"]
strike_zone_bottom = j["pitchData"]["strikeZoneBottom"]
play_type = j["details"]["code"]
is_in_play = j["details"]["isInPlay"]
if is_in_play is True:
try:
hit_location = int(j["hitData"]["location"])
except Exception:
hit_location = None
try:
hit_trajectory = j["hitData"]["trajectory"]
except Exception:
hit_trajectory = None
try:
hit_distance = int(j["hitData"]["totalDistance"])
except Exception:
hit_distance = None
try:
hit_launch_speed = j["hitData"]["launchSpeed"]
except Exception:
hit_launch_speed = None
try:
hit_launch_angle = j["hitData"]["launchAngle"]
except Exception:
hit_launch_angle = None
try:
hit_x = j["hitData"]["coordinates"]["coordX"]
except Exception:
hit_x = None
try:
hit_y = j["hitData"]["coordinates"]["coordY"]
except Exception:
hit_y = None
else:
hit_location = None
hit_trajectory = None
hit_distance = None
hit_launch_speed = None
hit_launch_angle = None
hit_x = None
hit_y = None
play_df = pd.DataFrame(
{
"play_start_datetime": play_start_datetime,
"play_end_datetime": play_end_datetime,
"pitch_type": pitch_type,
"pitch_name": pitch_name,
"game_date": game_date_str,
"release_speed": release_speed,
"release_pos_x": release_pos_x,
"release_pos_y": release_pos_y,
"release_pos_z": release_pos_z,
"player_name": player_name,
"batter": batter_id,
"pitcher": pitcher_id,
"events": event_type,
"description": sequence_description,
"spin_dir": pitch_spin_dir,
"spin_rate_deprecated": "",
"break_angle_deprecated": "",
"break_length_deprecated": "",
"zone": pitch_zone,
"des": sequence_description,
"game_type": game_type,
"stand": batter_side,
"p_throws": pitcher_hand,
"home_team": home_team_abv,
"away_team": away_team_abv,
"type": play_type,
"hit_location": hit_location,
"bb_type": hit_trajectory,
"balls": play_balls,
"strikes": play_strikes,
"pfx_x": pitch_pfx_x,
"pfx_z": pitch_pfx_z,
"plate_x": plate_x,
"plate_z": plate_z,
"on_3b": on_3b,
"on_2b": on_2b,
"on_1b": on_1b,
"outs_when_up": play_outs,
"inning": inning,
"inning_top_bot": top_bot,
"hc_x": hit_x,
"hc_y": hit_y,
"tfs_deprecated": "",
"tfs_zulu_deprecated": "",
"umpire": "",
"sv_id": "",
"vx0": pitch_vx0,
"vy0": pitch_vy0,
"vz0": pitch_vz0,
"ax": pitch_ax,
"ay": pitch_ay,
"az": pitch_az,
"sz_top": strike_zone_top,
"sz_bot": strike_zone_bottom,
"hit_distance_sc": hit_distance,
"launch_speed": hit_launch_speed,
"launch_angle": hit_launch_angle,
"effective_speed": "",
"release_spin_rate": pitch_spin_rate,
"release_extension": pitch_extension,
"game_pk": game_id,
"pitcher_1": pitcher_id,
"fielder_2": fielder_2,
"fielder_3": fielder_3,
"fielder_4": fielder_4,
"fielder_5": fielder_5,
"fielder_6": fielder_6,
"fielder_7": fielder_7,
"fielder_8": fielder_8,
"fielder_9": fielder_9,
"estimated_ba_using_speedangle": "",
"estimated_woba_using_speedangle": "",
"woba_value": "",
"woba_denom": "",
"babip_value": "",
"iso_value": "",
"launch_speed_angle": "",
"at_bat_number": at_bat_num,
"pitch_number": pitch_num,
"home_score": home_score,
"away_score": away_score,
"bat_score": bat_score,
"fld_score": fld_score,
"post_away_score": post_away_score,
"post_home_score": post_home_score,
"post_bat_score": post_bat_score,
"post_fld_score": post_fld_score,
"if_fielding_alignment": "",
"of_fielding_alignment": "",
"spin_axis": pitch_spin_dir,
"delta_home_win_exp": "",
"delta_run_exp": "",
"game_month": game_date.month,
"game_day": game_date.day,
"game_year": game_date.year,
"league_id": league_id,
"league_name": league_name,
"league_level_id": league_level_id,
"league_level_name": league_level_name,
"away_team_org_id": away_team_org_id,
"away_team_org_name": away_team_org_name,
"home_team_org_id": home_team_org_id,
"home_team_org_name": home_team_org_name,
},
index=[0],
)
del (
pitch_type,
pitch_name,
release_speed,
release_pos_x,
release_pos_y,
release_pos_z,
pitch_zone,
hit_location,
hit_trajectory,
hit_launch_angle,
is_in_play,
plate_x,
plate_z,
pitch_pfx_x,
pitch_pfx_z,
pitch_spin_dir,
play_type,
hit_x,
hit_y,
pitch_vx0,
pitch_vy0,
pitch_vz0,
pitch_ax,
pitch_az,
pitch_ay,
strike_zone_top,
strike_zone_bottom,
hit_distance,
pitch_spin_rate,
pitch_extension,
pitch_num,
play_start_datetime,
play_end_datetime,
play_balls,
play_strikes,
play_outs,
fielder_2,
fielder_3,
fielder_4,
fielder_5,
fielder_6,
fielder_7,
fielder_8,
fielder_9,
)
game_df.fillna(value=np.nan, inplace=True)
game_df = pd.concat([game_df, play_df], ignore_index=True)
del is_pitch
del (
player_name,
batter_id,
pitcher_id,
sequence_description,
inning,
top_bot,
at_bat_num,
batter_side,
pitcher_hand,
event,
on_1b,
on_2b,
on_3b,
)
home_score = post_home_score
away_score = post_away_score
del post_home_score, post_away_score
return game_df
def get_month_milb_pbp(
season: int,
month: int,
level="AAA",
cache_data=False,
cache_dir="",
save=True
):
""" """
game_df = pd.DataFrame()
pbp_df = pd.DataFrame()
sched_df = pd.DataFrame()
if (
(level.lower() == "aaa")
or (level.lower() == "triple-a")
or (level.lower() == "triple a")
):
sched_df = load_milb_schedule(season, "AAA")
elif (
(level.lower() == "aa")
or (level.lower() == "double-a")
or (level.lower() == "double a")
):
if season == 2010:
sched_df = pd.read_csv(
"https://github.com/armstjc/milb-data-repository/" +
f"releases/download/schedule/{season}_aa_schedule.csv"
)
else:
sched_df = load_milb_schedule(season, "AA")