patrickramos commited on
Commit
3de1790
·
1 Parent(s): 8467744

Account for pitches without batted balls

Browse files
Files changed (1) hide show
  1. stats.py +10 -1
stats.py CHANGED
@@ -274,6 +274,15 @@ def compute_pitch_stats(data, player_type, pitch_class_type, min_pitches=1, pitc
274
  {'batted_ball_direction': 'Pull', 'proportion': None},
275
  {'batted_ball_direction': 'Cent', 'proportion': None},
276
  {'batted_ball_direction': 'Oppo', 'proportion': None}
 
 
 
 
 
 
 
 
 
277
  ]))
278
  )
279
  .explode('batType')
@@ -294,7 +303,7 @@ def compute_pitch_stats(data, player_type, pitch_class_type, min_pitches=1, pitc
294
  # (pl.col('F') + pl.col('P') + pl.col('L')).alias('AIR%')
295
 
296
  )
297
- .drop('G', 'F', 'B', 'P', 'L', 'Pull', 'Cent', 'Oppo', 'null')
298
  .with_columns(
299
  # (pl.when(pl.col('qualified')).then(pl.col(stat)).rank(descending=((stat in ['FB%', 'LD%', 'OFFB%', 'AIR%', 'Ball%', 'Behind%'] or 'Contact%' in stat)))/pl.when(pl.col('qualified')).then(pl.col(stat)).count()).alias(f'{stat}_pctl')
300
  # for stat in ['Avg KPH', 'Max KPH', 'Avg MPH', 'Max MPH', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'Strike%', 'Ball%', 'F-Str%', 'PAR%', 'GB%', 'FB%', 'LD%', 'OFFB%', 'IFFB%', 'AIR%', 'Zone%', 'Behind%']
 
274
  {'batted_ball_direction': 'Pull', 'proportion': None},
275
  {'batted_ball_direction': 'Cent', 'proportion': None},
276
  {'batted_ball_direction': 'Oppo', 'proportion': None}
277
+ ])),
278
+ pl.when(pl.col('batType').list.len() > 0)
279
+ .then('batType')
280
+ .otherwise(pl.lit([
281
+ {'batType': 'G', 'proportion': None},
282
+ {'batType': 'B', 'proportion': None},
283
+ {'batType': 'P', 'proportion': None},
284
+ {'batType': 'F', 'proportion': None},
285
+ {'batType': 'L', 'proportion': None},
286
  ]))
287
  )
288
  .explode('batType')
 
303
  # (pl.col('F') + pl.col('P') + pl.col('L')).alias('AIR%')
304
 
305
  )
306
+ .drop('G', 'F', 'B', 'P', 'L', 'Pull', 'Cent', 'Oppo')
307
  .with_columns(
308
  # (pl.when(pl.col('qualified')).then(pl.col(stat)).rank(descending=((stat in ['FB%', 'LD%', 'OFFB%', 'AIR%', 'Ball%', 'Behind%'] or 'Contact%' in stat)))/pl.when(pl.col('qualified')).then(pl.col(stat)).count()).alias(f'{stat}_pctl')
309
  # for stat in ['Avg KPH', 'Max KPH', 'Avg MPH', 'Max MPH', 'Swing%', 'Z-Swing%', 'Chase%', 'Contact%', 'Z-Contact%', 'O-Contact%', 'SwStr%', 'Whiff%', 'CSW%', 'Strike%', 'Ball%', 'F-Str%', 'PAR%', 'GB%', 'FB%', 'LD%', 'OFFB%', 'IFFB%', 'AIR%', 'Zone%', 'Behind%']