def get_pa_count_pfx(df): # PA = AB + BB + HBP + SH + SF + Times Reached on Defensive Interference pa_extras = ['Walk', 'Sac Fly', 'Hit By Pitch', 'Intent Walk', 'Sac Bunt', 'Sac Fly', 'Catcher Interference', 'Fan interference', 'Batter Interference', 'Sac Fly DP', 'Sacrifice Bunt DP'] pa = (Baseball.get_atbats_count_pfx(df) + len(df[df['event'].isin(pa_extras)].groupby(['gameday_link','num']).first())) return pa
def get_pa_count_pfx(df): """ Given a pitchab dataframe return the number of official plate appearances PA = AB + BB + HBP + SH + SF + Times Reached on Defensive Interference -> Calculate by first counting at-bats and then counting additional events that count toward a plate appearance """ pa_extras = ['Walk', 'Sac Fly', 'Hit By Pitch', 'Intent Walk', 'Sac Bunt', 'Sac Fly', 'Catcher Interference', 'Fan interference', 'Batter Interference', 'Sac Fly DP', 'Sacrifice Bunt DP'] pa = (Baseball.get_atbats_count_pfx(df) + len(df[df['event'].isin(pa_extras)].groupby(['gameday_link','num']).first())) return pa
def get_pa_for_obp(df): # At Bats + Walks + Hit by Pitch + Sacrifice Flies obp_pa_extras = ['Walk', 'Sac Fly', 'Hit By Pitch', 'Intent Walk', 'Sac Fly', 'Catcher Interference', 'Fan interference', 'Batter Interference', 'Sac Fly DP', 'Sacrifice Bunt DP'] obp_pa = (Baseball.get_atbats_count_pfx(df) + len(df[df['event'].isin(obp_pa_extras)].groupby(['gameday_link','num']).first())) return obp_pa
def get_slg_pfx(df): """ Calculate slugging from a pitchab dataframe SLG = (Total bases) / (At Bats) """ tb = Baseball.get_tb(df) ab = Baseball.get_atbats_count_pfx(df) return(tb/ab)