Skip to content

stats

Reference materials for chickenstats.evolving_hockey.stats.

For more detailed walk-throughs or examples, please consult the User Guide

prep_pbp

prep_pbp(
    pbp, shifts, columns="full", disable_progress_bar=False
)

Prepares a play-by-play dataframe using EvolvingHockey data, but with additional stats and information.

Columns keyword argument determines information returned. Used in later aggregation functions. Returns a DataFrame.

Parameters:

Name Type Description Default
pbp DataFrame

Pandas DataFrame of CSV file downloaded from play-by-play query tool at evolving-hockey.com

required
shifts DataFrame

Pandas DataFrame of CSV file downloaded from shifts query tool at evolving-hockey.com

required
columns str

Whether to return additional columns or more sparse play-by-play dataframe

'full'
disable_progress_bar bool

Whether to disable progress bar

False

Returns:

Name Type Description
season int

8-digit season code, e.g., 20192020

session str

Regular season or playoffs, e.g., R

game_id int

10-digit game identifier, e.g., 2019020684

game_date str

Date of game in Eastern time-zone, e.g., 2020-01-09

event_index int

Unique index number of event, in chronological order, e.g.,

game_period int

Game period, e.g., 3

game_seconds int

Game time elapsed in seconds, e.g., 3578

period_seconds int

Period time elapsed in seconds, e.g., 1178

clock_time str

Time shown on clock, e.g., 0:22

strength_state str

Strength state from the perspective of the event team, e.g., 5vE

score_state str

Score state from the perspective of the event team, e.g., 5v2

event_type str

Name of the event, e.g., GOAL

event_description str

Description of the event, e.g., NSH #35 RINNE(1), WRIST, DEF. ZONE, 185 FT.

event_detail str | None

Additional information about the event, e.g., Wrist

event_zone str | None

Zone location of event, e.g., DEF

event_team str | None

3-letter abbreviation of the team for the event, e.g., NSH

opp_team str | None

3-letter abbreviation of the opposing team for the event, e.g., CHI

is_home int | None

Dummy variable to signify whether event team is home team, e.g., 0

coords_x int | None

X coordinates of event, e.g., -96

coords_y int | None

Y coordinates of event, e.g., 11

event_player_1 str

Name of the first event player, e.g., PEKKA.RINNE

event_player_1_id str

Identifier that can be used to match with Evolving Hockey data, e.g., PEKKA.RINNE

event_player_1_pos str

Player's position for the game, may differ from primary position, e.g., G

event_player_2 str

Name of the second event player

event_player_2_id str

Identifier that can be used to match with Evolving Hockey data

event_player_2_pos str

Player's position for the game, may differ from primary position

event_player_3 str

Name of the third event player

event_player_3_id str

Identifier that can be used to match with Evolving Hockey data

event_player_3_pos str

Player's position for the game, may differ from primary position

event_length int

Length of time elapsed in seconds since previous event, e.g., 5

high_danger int

Whether shot event is from high-danger area, e.g., 0

danger int

Whether shot event is from danger area, exclusive of high-danger area, e.g., 0

pbp_distance float

Distance from opponent net, in feet, according to play-by-play description, e.g., 185.0

event_distance float

Distance from opponent net, in feet, e.g., 185.326738

event_angle float

Angle of opponent's net from puck, in degrees, e.g., 57.528808

opp_strength_state str

Strength state from the perspective of the opposing team, e.g., Ev5

opp_score_state str

Score state from the perspective of the opposing team, e.g., 2v5

event_on_f str

Names of the event team's forwards that are on the ice during the event, e.g., NICK BONINO, CALLE JARNKROK, MIKAEL GRANLUND

event_on_f_id str

EH IDs of the event team's forwards that are on the ice during the event, e.g., NICK.BONINO, CALLE.JARNKROK, MIKAEL.GRANLUND

event_on_d str

Names of the event team's defensemen that are on the ice during the event, e.g., MATTIAS EKHOLM, ROMAN JOSI

event_on_d_id str

EH IDs of the event team's defensemen that are on the ice during the event, e.g., MATTIAS.EKHOLM, ROMAN.JOSI

event_on_g str

Name of the goalie for the event team, e.g., PEKKA RINNE

event_on_g_id str

Identifier for the event team goalie that can be used to match with Evolving Hockey data, e.g., PEKKA.RINNE

event_on_1 str

Name of one the event team's players that are on the ice during the event, e.g., CALLE.JARNKROK

event_on_1_id str

EH ID of one the event team's players that are on the ice during the event, e.g., CALLE.JARNKROK

event_on_1_pos str

Position of one the event team's players that are on the ice during the event, e.g., C

event_on_2 str

Name of one the event team's players that are on the ice during the event, e.g., MATTIAS.EKHOLM

event_on_2_id str

EH ID of one the event team's players that are on the ice during the event, e.g., MATTIAS.EKHOLM

event_on_2_pos str

Position of one the event team's players that are on the ice during the event, e.g., D

event_on_3 str

Name of one the event team's players that are on the ice during the event, e.g., MIKAEL.GRANLUND

event_on_3_id str

EH ID of one the event team's players that are on the ice during the event, e.g., MIKAEL.GRANLUND

event_on_3_pos str

Position of one the event team's players that are on the ice during the event, e.g., C

event_on_4 str

Name of one the event team's players that are on the ice during the event, e.g., NICK.BONINO

event_on_4_id str

EH ID of one the event team's players that are on the ice during the event, e.g., NICK.BONINO

event_on_4_pos str

Position of one the event team's players that are on the ice during the event, e.g., C

event_on_5 str

Name of one the event team's players that are on the ice during the event, e.g., PEKKA.RINNE

event_on_5_id str

EH ID of one the event team's players that are on the ice during the event, e.g., PEKKA.RINNE

event_on_5_pos str

Position of one the event team's players that are on the ice during the event, e.g., G

event_on_6 str

Name of one the event team's players that are on the ice during the event, e.g., ROMAN.JOSI

event_on_6_id str

EH ID of one the event team's players that are on the ice during the event, e.g., ROMAN.JOSI

event_on_6_pos str

Position of one the event team's players that are on the ice during the event, e.g., D

event_on_7 str

Name of one the event team's players that are on the ice during the event, e.g., NaN

event_on_7_id str

EH ID of one the event team's players that are on the ice during the event, e.g., NaN

event_on_7_pos str

Position of one the event team's players that are on the ice during the event, e.g., NaN

opp_on_f str

Names of the opponent's forwards that are on the ice during the event, e.g., ALEX DEBRINCAT, JONATHAN TOEWS, KIRBY DACH, PATRICK KANE

opp_on_f_id str

EH IDs of the event team's forwards that are on the ice during the event, e.g., ALEX.DEBRINCAT, JONATHAN.TOEWS, KIRBY.DACH, PATRICK.KANE

opp_on_d str

Names of the opposing team's defensemen that are on the ice during the event, e.g., DUNCAN KEITH, ERIK GUSTAFSSON

opp_on_d_id str

EH IDs of the opposing team's defensemen that are on the ice during the event, e.g., DUNCAN.KEITH, ERIK.GUSTAFSSON2

opp_on_g str

Name of the opposing goalie for the event team, e.g., EMPTY NET

opp_on_g_id str

Identifier for the opposing goalie that can be used to match with Evolving Hockey data, e.g., EMPTY NET

opp_on_1 str

Name of one the opposing team's players that are on the ice during the event, e.g., ALEX.DEBRINCAT

opp_on_1_id str

EH ID of one the opposing team's players that are on the ice during the event, e.g., ALEX.DEBRINCAT

opp_on_1_pos str

Position of one the opposing team's players that are on the ice during the event, e.g., R

opp_on_2 str

Name of one the opposing team's players that are on the ice during the event, e.g., DUNCAN.KEITH

opp_on_2_id str

EH ID of one the opposing team's players that are on the ice during the event, e.g., DUNCAN.KEITH

opp_on_2_pos str

Position of one the opposing team's players that are on the ice during the event, e.g., D

opp_on_3 str

Name of one the opposing team's players that are on the ice during the event, e.g., ERIK.GUSTAFSSON2

opp_on_3_id str

EH ID of one the opposing team's players that are on the ice during the event, e.g., ERIK.GUSTAFSSON2

opp_on_3_pos str

Position of one the opposing team's players that are on the ice during the event, e.g., D

opp_on_4 str

Name of one the opposing team's players that are on the ice during the event, e.g., JONATHAN.TOEWS

opp_on_4_id str

EH ID of one the opposing team's players that are on the ice during the event, e.g., JONATHAN.TOEWS

opp_on_4_pos str

Position of one the opposing team's players that are on the ice during the event, e.g., C

opp_on_5 str

Name of one the opposing team's players that are on the ice during the event, e.g., KIRBY.DACH

opp_on_5_id str

EH ID of one the opposing team's players that are on the ice during the event, e.g., KIRBY.DACH

opp_on_5_pos str

Position of one the opposing team's players that are on the ice during the event, e.g., C

opp_on_6 str

Name of one the opposing team's players that are on the ice during the event, e.g., PATRICK.KANE

opp_on_6_id str

EH ID of one the opposing team's players that are on the ice during the event, e.g., PATRICK.KANE

opp_on_6_pos str

Position of one the opposing team's players that are on the ice during the event, e.g., R

opp_on_7 str

Name of one the opposing team's players that are on the ice during the event, e.g., NaN

opp_on_7_id str

EH ID of one the opposing team's players that are on the ice during the event, e.g., NaN

opp_on_7_pos str

Position of one the opposing team's players that are on the ice during the event, e.g., NaN

change int

Dummy variable to indicate whether event is a change, e.g., 0

zone_start str

Zone where the changed, e.g., OFF or OTF

num_on int | None

Number of players entering the ice, e.g., 6

num_off int | None

Number of players exiting the ice, e.g., 0

players_on str

Names of players on, in jersey order, e.g., FILIP FORSBERG, ALEX CARRIER, ROMAN JOSI, MIKAEL GRANLUND, JUUSE SAROS, MATT DUCHENE

players_on_id str

Evolving Hockey IDs of players on, in jersey order, e.g., FILIP.FORSBERG, ALEX.CARRIER, ROMAN.JOSI, MIKAEL.GRANLUND, JUUSE.SAROS, MATT.DUCHENE

players_on_pos str

Positions of players on, in jersey order, e.g., L, D, D, C, G, C

players_off str

Names of players off, in jersey order

players_off_id str

Evolving Hockey IDs of players off, in jersey order

players_off_pos str

Positions of players off, in jersey order

shot int

Dummy variable to indicate whether event is a shot, e.g., 1

shot_adj float

Score and venue-adjusted shot value, e.g., 0

goal int

Dummy variable to indicate whether event is a goal, e.g., 1

goal_adj float

Score and venue-adjusted shot value, e.g., 0

pred_goal float

Predicted goal value (xG), e.g., 0.482589

pred_goal_adj float

Score and venue-adjusted predicted goal (xG) value, e.g., 0

miss int

Dummy variable to indicate whether event is a missed shot, e.g., 0

block int

Dummy variable to indicate whether event is a block, e.g., 0

corsi int

Dummy variable to indicate whether event is a corsi event, e.g., 1

corsi_adj float

Score and venue-adjusted corsi value, e.g., 0

fenwick int

Dummy variable to indicate whether event is a fenwick event, e.g., 1

fenwick_adj float

Score and venue-adjusted fenwick value, e.g., 0

hd_shot int

Dummy variable to indicate whether event is a high-danger shot event, e.g., 0

hd_goal int

Dummy variable to indicate whether event is a high-danger goal event, e.g., 0

hd_miss int

Dummy variable to indicate whether event is a high-danger miss event, e.g., 0

hd_fenwick int

Dummy variable to indicate whether event is a high-danger fenwick event, e.g., 0

fac int

Dummy variable to indicate whether event is a faceoff, e.g., 0

hit int

Dummy variable to indicate whether event is a hit, e.g., 0

give int

Dummy variable to indicate whether event is a giveaway, e.g., 0

take int

Dummy variable to indicate whether event is a takeaway, e.g., 0

pen0 int

Dummy variable to indicate whether event is a penalty with no minutes, e.g., 0

pen2 int

Dummy variable to indicate whether event is a two-minute penalty, e.g., 0

pen4 int

Dummy variable to indicate whether event is a four-minute penalty, e.g., 0

pen5 int

Dummy variable to indicate whether event is a five-minute penalty, e.g., 0

pen10 int

Dummy variable to indicate whether event is a ten-minute penalty, e.g., 0

stop int

Dummy variable to indicate whether event is a stoppage, e.g., 0

ozf int

Dummy variable to indicate whether event is an offensive zone faceoff e.g., 0

nzf int

Dummy variable to indicate whether event is a neutral zone faceoff, e.g., 0

dzf int

Dummy variable to indicate whether event is a defensive zone faceoff, e.g., 0

ozs int

Dummy variable to indicate whether an event is an offensive zone change, e.g., 0

nzs int

Dummy variable to indicate whether an event is a neutral zone change, e.g., 0

dzs int

Dummy variable to indicate whether an event is an defensive zone change, e.g., 0

otf int

Dummy variable to indicate whether an event is an on-the-fly change, e.g., 0

Examples:

Play-by-play DataFrame

>>> shifts_raw = pd.read_csv("./raw_shifts.csv")
>>> pbp_raw = pd.read_csv("./raw_pbp.csv")
>>> pbp = prep_pbp(pbp_raw, shifts_raw)
Source code in src/chickenstats/evolving_hockey/stats.py
 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
def prep_pbp(
    pbp: pd.DataFrame | list[pd.DataFrame],
    shifts: pd.DataFrame | list[pd.DataFrame],
    columns: Literal["light", "full", "all"] = "full",
    disable_progress_bar: bool = False,
) -> pd.DataFrame:
    """Prepares a play-by-play dataframe using EvolvingHockey data, but with additional stats and information.

    Columns keyword argument determines information returned. Used in later aggregation
    functions. Returns a DataFrame.

    Parameters:
        pbp (pd.DataFrame):
            Pandas DataFrame of CSV file downloaded from play-by-play query tool at evolving-hockey.com
        shifts (pd.DataFrame):
            Pandas DataFrame of CSV file downloaded from shifts query tool at evolving-hockey.com
        columns (str):
            Whether to return additional columns or more sparse play-by-play dataframe
        disable_progress_bar (bool):
            Whether to disable progress bar

    Returns:
        season (int):
            8-digit season code, e.g., 20192020
        session (str):
            Regular season or playoffs, e.g., R
        game_id (int):
            10-digit game identifier, e.g., 2019020684
        game_date (str):
            Date of game in Eastern time-zone, e.g., 2020-01-09
        event_index (int):
            Unique index number of event, in chronological order, e.g.,
        game_period (int):
            Game period, e.g., 3
        game_seconds (int):
            Game time elapsed in seconds, e.g., 3578
        period_seconds (int):
            Period time elapsed in seconds, e.g., 1178
        clock_time (str):
            Time shown on clock, e.g., 0:22
        strength_state (str):
            Strength state from the perspective of the event team, e.g., 5vE
        score_state (str):
            Score state from the perspective of the event team, e.g., 5v2
        event_type (str):
            Name of the event, e.g., GOAL
        event_description (str):
            Description of the event, e.g., NSH #35 RINNE(1), WRIST, DEF. ZONE, 185 FT.
        event_detail (str | None):
            Additional information about the event, e.g., Wrist
        event_zone (str | None):
            Zone location of event, e.g., DEF
        event_team (str | None):
            3-letter abbreviation of the team for the event, e.g., NSH
        opp_team (str | None):
            3-letter abbreviation of the opposing team for the event, e.g., CHI
        is_home (int | None):
            Dummy variable to signify whether event team is home team, e.g., 0
        coords_x (int | None):
            X coordinates of event, e.g., -96
        coords_y (int | None):
            Y coordinates of event, e.g., 11
        event_player_1 (str):
            Name of the first event player, e.g., PEKKA.RINNE
        event_player_1_id (str):
            Identifier that can be used to match with Evolving Hockey data, e.g., PEKKA.RINNE
        event_player_1_pos (str):
            Player's position for the game, may differ from primary position, e.g., G
        event_player_2 (str):
            Name of the second event player
        event_player_2_id (str):
            Identifier that can be used to match with Evolving Hockey data
        event_player_2_pos (str):
            Player's position for the game, may differ from primary position
        event_player_3 (str):
            Name of the third event player
        event_player_3_id (str):
            Identifier that can be used to match with Evolving Hockey data
        event_player_3_pos (str):
            Player's position for the game, may differ from primary position
        event_length (int):
            Length of time elapsed in seconds since previous event, e.g., 5
        high_danger (int):
            Whether shot event is from high-danger area, e.g., 0
        danger (int):
            Whether shot event is from danger area,
            exclusive of high-danger area, e.g., 0
        pbp_distance (float):
            Distance from opponent net, in feet, according to play-by-play description, e.g., 185.0
        event_distance (float):
            Distance from opponent net, in feet, e.g., 185.326738
        event_angle (float):
            Angle of opponent's net from puck, in degrees, e.g., 57.528808
        opp_strength_state (str):
            Strength state from the perspective of the opposing team, e.g., Ev5
        opp_score_state (str):
            Score state from the perspective of the opposing team, e.g., 2v5
        event_on_f (str):
            Names of the event team's forwards that are on the ice during the event,
            e.g., NICK BONINO, CALLE JARNKROK, MIKAEL GRANLUND
        event_on_f_id (str):
            EH IDs of the event team's forwards that are on the ice during the event,
            e.g., NICK.BONINO, CALLE.JARNKROK, MIKAEL.GRANLUND
        event_on_d (str):
            Names of the event team's defensemen that are on the ice during the event,
            e.g., MATTIAS EKHOLM, ROMAN JOSI
        event_on_d_id (str):
            EH IDs of the event team's defensemen that are on the ice during the event,
            e.g., MATTIAS.EKHOLM, ROMAN.JOSI
        event_on_g (str):
            Name of the goalie for the event team, e.g., PEKKA RINNE
        event_on_g_id (str):
            Identifier for the event team goalie that can be used to match with Evolving Hockey data, e.g., PEKKA.RINNE
        event_on_1 (str):
            Name of one the event team's players that are on the ice during the event,
            e.g., CALLE.JARNKROK
        event_on_1_id (str):
            EH ID of one the event team's players that are on the ice during the event,
            e.g., CALLE.JARNKROK
        event_on_1_pos (str):
            Position of one the event team's players that are on the ice during the event,
            e.g., C
        event_on_2 (str):
            Name of one the event team's players that are on the ice during the event,
            e.g., MATTIAS.EKHOLM
        event_on_2_id (str):
            EH ID of one the event team's players that are on the ice during the event,
            e.g., MATTIAS.EKHOLM
        event_on_2_pos (str):
            Position of one the event team's players that are on the ice during the event,
            e.g., D
        event_on_3 (str):
            Name of one the event team's players that are on the ice during the event,
            e.g., MIKAEL.GRANLUND
        event_on_3_id (str):
            EH ID of one the event team's players that are on the ice during the event,
            e.g., MIKAEL.GRANLUND
        event_on_3_pos (str):
            Position of one the event team's players that are on the ice during the event,
            e.g., C
        event_on_4 (str):
            Name of one the event team's players that are on the ice during the event,
            e.g., NICK.BONINO
        event_on_4_id (str):
            EH ID of one the event team's players that are on the ice during the event,
            e.g., NICK.BONINO
        event_on_4_pos (str):
            Position of one the event team's players that are on the ice during the event,
            e.g., C
        event_on_5 (str):
            Name of one the event team's players that are on the ice during the event,
            e.g., PEKKA.RINNE
        event_on_5_id (str):
            EH ID of one the event team's players that are on the ice during the event,
            e.g., PEKKA.RINNE
        event_on_5_pos (str):
            Position of one the event team's players that are on the ice during the event,
            e.g., G
        event_on_6 (str):
            Name of one the event team's players that are on the ice during the event,
            e.g., ROMAN.JOSI
        event_on_6_id (str):
            EH ID of one the event team's players that are on the ice during the event,
            e.g., ROMAN.JOSI
        event_on_6_pos (str):
            Position of one the event team's players that are on the ice during the event,
            e.g., D
        event_on_7 (str):
            Name of one the event team's players that are on the ice during the event,
            e.g., NaN
        event_on_7_id (str):
            EH ID of one the event team's players that are on the ice during the event,
            e.g., NaN
        event_on_7_pos (str):
            Position of one the event team's players that are on the ice during the event,
            e.g., NaN
        opp_on_f (str):
            Names of the opponent's forwards that are on the ice during the event,
            e.g., ALEX DEBRINCAT, JONATHAN TOEWS, KIRBY DACH, PATRICK KANE
        opp_on_f_id (str):
            EH IDs of the event team's forwards that are on the ice during the event,
            e.g., ALEX.DEBRINCAT, JONATHAN.TOEWS, KIRBY.DACH, PATRICK.KANE
        opp_on_d (str):
            Names of the opposing team's defensemen that are on the ice during the event,
            e.g., DUNCAN KEITH, ERIK GUSTAFSSON
        opp_on_d_id (str):
            EH IDs of the opposing team's defensemen that are on the ice during the event,
            e.g., DUNCAN.KEITH, ERIK.GUSTAFSSON2
        opp_on_g (str):
            Name of the opposing goalie for the event team, e.g., EMPTY NET
        opp_on_g_id (str):
            Identifier for the opposing goalie that can be used to match with Evolving Hockey data, e.g., EMPTY NET
        opp_on_1 (str):
            Name of one the opposing team's players that are on the ice during the event,
            e.g., ALEX.DEBRINCAT
        opp_on_1_id (str):
            EH ID of one the opposing team's players that are on the ice during the event,
            e.g., ALEX.DEBRINCAT
        opp_on_1_pos (str):
            Position of one the opposing team's players that are on the ice during the event,
            e.g., R
        opp_on_2 (str):
            Name of one the opposing team's players that are on the ice during the event,
            e.g., DUNCAN.KEITH
        opp_on_2_id (str):
            EH ID of one the opposing team's players that are on the ice during the event,
            e.g., DUNCAN.KEITH
        opp_on_2_pos (str):
            Position of one the opposing team's players that are on the ice during the event,
            e.g., D
        opp_on_3 (str):
            Name of one the opposing team's players that are on the ice during the event,
            e.g., ERIK.GUSTAFSSON2
        opp_on_3_id (str):
            EH ID of one the opposing team's players that are on the ice during the event,
            e.g., ERIK.GUSTAFSSON2
        opp_on_3_pos (str):
            Position of one the opposing team's players that are on the ice during the event,
            e.g., D
        opp_on_4 (str):
            Name of one the opposing team's players that are on the ice during the event,
            e.g., JONATHAN.TOEWS
        opp_on_4_id (str):
            EH ID of one the opposing team's players that are on the ice during the event,
            e.g., JONATHAN.TOEWS
        opp_on_4_pos (str):
            Position of one the opposing team's players that are on the ice during the event,
            e.g., C
        opp_on_5 (str):
            Name of one the opposing team's players that are on the ice during the event,
            e.g., KIRBY.DACH
        opp_on_5_id (str):
            EH ID of one the opposing team's players that are on the ice during the event,
            e.g., KIRBY.DACH
        opp_on_5_pos (str):
            Position of one the opposing team's players that are on the ice during the event,
            e.g., C
        opp_on_6 (str):
            Name of one the opposing team's players that are on the ice during the event,
            e.g., PATRICK.KANE
        opp_on_6_id (str):
            EH ID of one the opposing team's players that are on the ice during the event,
            e.g., PATRICK.KANE
        opp_on_6_pos (str):
            Position of one the opposing team's players that are on the ice during the event,
            e.g., R
        opp_on_7 (str):
            Name of one the opposing team's players that are on the ice during the event,
            e.g., NaN
        opp_on_7_id (str):
            EH ID of one the opposing team's players that are on the ice during the event,
            e.g., NaN
        opp_on_7_pos (str):
            Position of one the opposing team's players that are on the ice during the event,
            e.g., NaN
        change (int):
            Dummy variable to indicate whether event is a change, e.g., 0
        zone_start (str):
            Zone where the changed, e.g., OFF or OTF
        num_on (int | None):
            Number of players entering the ice, e.g., 6
        num_off (int | None):
            Number of players exiting the ice, e.g., 0
        players_on (str):
            Names of players on, in jersey order,
            e.g., FILIP FORSBERG, ALEX CARRIER, ROMAN JOSI, MIKAEL GRANLUND, JUUSE SAROS, MATT DUCHENE
        players_on_id (str):
            Evolving Hockey IDs of players on, in jersey order,
            e.g., FILIP.FORSBERG, ALEX.CARRIER, ROMAN.JOSI, MIKAEL.GRANLUND, JUUSE.SAROS, MATT.DUCHENE
        players_on_pos (str):
            Positions of players on, in jersey order,
            e.g., L, D, D, C, G, C
        players_off (str):
            Names of players off, in jersey order
        players_off_id (str):
            Evolving Hockey IDs of players off, in jersey order
        players_off_pos (str):
            Positions of players off, in jersey order
        shot (int):
            Dummy variable to indicate whether event is a shot, e.g., 1
        shot_adj (float):
            Score and venue-adjusted shot value, e.g., 0
        goal (int):
            Dummy variable to indicate whether event is a goal, e.g., 1
        goal_adj (float):
            Score and venue-adjusted shot value, e.g., 0
        pred_goal (float):
            Predicted goal value (xG), e.g., 0.482589
        pred_goal_adj (float):
            Score and venue-adjusted predicted goal (xG) value, e.g., 0
        miss (int):
            Dummy variable to indicate whether event is a missed shot, e.g., 0
        block (int):
            Dummy variable to indicate whether event is a block, e.g., 0
        corsi (int):
            Dummy variable to indicate whether event is a corsi event, e.g., 1
        corsi_adj (float):
            Score and venue-adjusted corsi value, e.g., 0
        fenwick (int):
            Dummy variable to indicate whether event is a fenwick event, e.g., 1
        fenwick_adj (float):
             Score and venue-adjusted fenwick value, e.g., 0
        hd_shot (int):
            Dummy variable to indicate whether event is a high-danger shot event, e.g., 0
        hd_goal (int):
            Dummy variable to indicate whether event is a high-danger goal event, e.g., 0
        hd_miss (int):
            Dummy variable to indicate whether event is a high-danger miss event, e.g., 0
        hd_fenwick (int):
            Dummy variable to indicate whether event is a high-danger fenwick event, e.g., 0
        fac (int):
            Dummy variable to indicate whether event is a faceoff, e.g., 0
        hit (int):
            Dummy variable to indicate whether event is a hit, e.g., 0
        give (int):
            Dummy variable to indicate whether event is a giveaway, e.g., 0
        take (int):
            Dummy variable to indicate whether event is a takeaway, e.g., 0
        pen0 (int):
            Dummy variable to indicate whether event is a penalty with no minutes, e.g., 0
        pen2 (int):
            Dummy variable to indicate whether event is a two-minute penalty, e.g., 0
        pen4 (int):
            Dummy variable to indicate whether event is a four-minute penalty, e.g., 0
        pen5 (int):
            Dummy variable to indicate whether event is a five-minute penalty, e.g., 0
        pen10 (int):
            Dummy variable to indicate whether event is a ten-minute penalty, e.g., 0
        stop (int):
            Dummy variable to indicate whether event is a stoppage, e.g., 0
        ozf (int):
            Dummy variable to indicate whether event is an offensive zone faceoff e.g., 0
        nzf (int):
            Dummy variable to indicate whether event is a neutral zone faceoff, e.g., 0
        dzf (int):
            Dummy variable to indicate whether event is a defensive zone faceoff, e.g., 0
        ozs (int):
            Dummy variable to indicate whether an event is an offensive zone change, e.g., 0
        nzs (int):
            Dummy variable to indicate whether an event is a neutral zone change, e.g., 0
        dzs (int):
            Dummy variable to indicate whether an event is an defensive zone change, e.g., 0
        otf (int):
            Dummy variable to indicate whether an event is an on-the-fly change, e.g., 0

    Examples:
        Play-by-play DataFrame
        >>> shifts_raw = pd.read_csv("./raw_shifts.csv")
        >>> pbp_raw = pd.read_csv("./raw_pbp.csv")
        >>> pbp = prep_pbp(pbp_raw, shifts_raw)

    """
    with ChickenProgress(disable=disable_progress_bar) as progress:
        if isinstance(pbp, pd.DataFrame):
            progress_total = 1

            pbp = [pbp]

        elif isinstance(pbp, list):
            progress_total = len(pbp)

        if isinstance(shifts, pd.DataFrame):
            shifts = [shifts]

        if len(pbp) != len(shifts):
            raise Exception("Number of play-by-play and shift CSV files does not match")

        pbar_message = "Prepping play-by-play data..."

        csv_task = progress.add_task(pbar_message, total=progress_total)

        pbp_concat = []

        for idx, (pbp_raw, shifts_raw) in enumerate(zip(pbp, shifts)):
            rosters = munge_rosters(shifts_raw)

            pbp_clean = munge_pbp(pbp_raw)

            pbp_clean = add_positions(pbp_clean, rosters)

            cols = [
                "id",
                "season",
                "session",
                "game_id",
                "game_date",
                "event_index",
                "game_period",
                "game_seconds",
                "period_seconds",
                "clock_time",
                "strength_state",
                "score_state",
                "event_type",
                "event_description",
                "event_detail",
                "event_zone",
                "event_team",
                "opp_team",
                "is_home",
                "coords_x",
                "coords_y",
                "event_player_1",
                "event_player_1_id",
                "event_player_1_pos",
                "event_player_2",
                "event_player_2_id",
                "event_player_2_pos",
                "event_player_3",
                "event_player_3_id",
                "event_player_3_pos",
                "event_length",
                "high_danger",
                "danger",
                "pbp_distance",
                "event_distance",
                "event_angle",
                "event_on_f",
                "event_on_f_id",
                "event_on_d",
                "event_on_d_id",
                "event_on_g",
                "event_on_g_id",
                "opp_on_f",
                "opp_on_f_id",
                "opp_on_d",
                "opp_on_d_id",
                "opp_on_g",
                "opp_on_g_id",
                "change",
                "zone_start",
                "num_on",
                "num_off",
                "players_on",
                "players_on_id",
                "players_on_pos",
                "players_off",
                "players_off_id",
                "players_off_pos",
                "shot",
                "shot_adj",
                "goal",
                "goal_adj",
                "pred_goal",
                "pred_goal_adj",
                "miss",
                "block",
                "corsi",
                "corsi_adj",
                "fenwick",
                "fenwick_adj",
                "hd_shot",
                "hd_goal",
                "hd_miss",
                "hd_fenwick",
                "fac",
                "hit",
                "give",
                "take",
                "pen0",
                "pen2",
                "pen4",
                "pen5",
                "pen10",
                "stop",
                "ozf",
                "nzf",
                "dzf",
                "ozs",
                "nzs",
                "dzs",
                "otf",
            ]

            if columns in ["full", "all"]:
                event_cols = [
                    "event_on_1",
                    "event_on_1_id",
                    "event_on_1_pos",
                    "event_on_2",
                    "event_on_2_id",
                    "event_on_2_pos",
                    "event_on_3",
                    "event_on_3_id",
                    "event_on_3_pos",
                    "event_on_4",
                    "event_on_4_id",
                    "event_on_4_pos",
                    "event_on_5",
                    "event_on_5_id",
                    "event_on_5_pos",
                    "event_on_6",
                    "event_on_6_id",
                    "event_on_6_pos",
                    "event_on_7",
                    "event_on_7_id",
                    "event_on_7_pos",
                ]

                event_pos = cols.index("event_on_g_id") + 1

                cols[event_pos:event_pos] = event_cols

                opp_cols = [
                    "opp_on_1",
                    "opp_on_1_id",
                    "opp_on_1_pos",
                    "opp_on_2",
                    "opp_on_2_id",
                    "opp_on_2_pos",
                    "opp_on_3",
                    "opp_on_3_id",
                    "opp_on_3_pos",
                    "opp_on_4",
                    "opp_on_4_id",
                    "opp_on_4_pos",
                    "opp_on_5",
                    "opp_on_5_id",
                    "opp_on_5_pos",
                    "opp_on_6",
                    "opp_on_6_id",
                    "opp_on_6_pos",
                    "opp_on_7",
                    "opp_on_7_id",
                    "opp_on_7_pos",
                ]

                opp_pos = cols.index("opp_on_g_id") + 1

                cols[opp_pos:opp_pos] = opp_cols

                other_cols = ["opp_strength_state", "opp_score_state"]

                other_pos = cols.index("event_angle") + 1

                cols[other_pos:other_pos] = other_cols

            if columns == "all":
                more_cols = [
                    "home_zone",
                    "home_team",
                    "away_team",
                    "home_goalie",
                    "away_goalie",
                    "home_skaters",
                    "away_skaters",
                    "home_score",
                    "away_score",
                    "home_zonestart",
                    "face_index",
                    "pen_index",
                    "shift_index",
                    "game_score_state",
                    "game_strength_state",
                ]

                pos = cols.index("is_home") + 1

                cols[pos:pos] = more_cols

            cols = [x for x in cols if x in pbp_clean]

            pbp_clean = pbp_clean[cols]

            cols = [x for x in list(PBPSchema.dtypes.keys()) if x in pbp_clean.columns]

            pbp_clean = PBPSchema.validate(pbp_clean[cols])

            pbp_concat.append(pbp_clean)

            if progress_total == 1 or idx + 1 == progress_total:
                pbp_clean = pd.concat(pbp_concat, ignore_index=True)

                pbar_message = "Finished loading play-by-play data"

            progress.update(csv_task, description=pbar_message, advance=1, refresh=True)

    return pbp_clean

prep_stats

prep_stats(
    pbp,
    level="game",
    score=False,
    teammates=False,
    opposition=False,
    disable_progress_bar=False,
)

Prepares an individual and on-ice stats dataframe using EvolvingHockey data.

Aggregates to desired level. Capable of returning cuts that account for strength state, period, score state, teammates, and opposition.

Returns a Pandas DataFrame.

Parameters:

Name Type Description Default
pbp DataFrame

Dataframe from the prep_pbp function with the default columns argument

required
level str

Level to aggregate stats, e.g., 'game'

'game'
score bool

Whether to aggregate to score state level

False
teammates bool

Whether to account for teammates when aggregating

False
opposition bool

Whether to account for opposition when aggregating

False
disable_progress_bar bool

Whether to disable progress bar

False

Returns:

Name Type Description
season int

8-digit season code, e.g., 20232024

session str

Regular season or playoffs, e.g., R

game_id int

10-digit game identifier, e.g., 2023020015

game_date str

Date of game in Eastern time-zone, e.g., 2023-10-12

player str

Name of the player, e.g., FILIP.FORSBERG

player_id str

Player EH ID, e.g., FILIP.FORSBERG

position str

Player's position, e.g., L

team str

3-letter abbreviation of the player's team, e.g., NSH

opp_team DataFrame

object 3-letter abbreviation of the opposing team, e.g., SEA

strength_state str

Strength state from the perspective of the event team, e.g., 5v5

score_state str

Score state from the perspective of the event team, e.g., 0v0

game_period int

Game period, e.g., 1

forwards str

Names of the event team's forwards that are on the ice during the event, e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY

forwards_id str

EH IDs of the event team's forwards that are on the ice during the event, e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY

defense str

Names of the event team's defensemen that are on the ice during the event, e.g., ALEX.CARRIER, RYAN.MCDONAGH

defense_id str

EH IDs of the event team's defensemen that are on the ice during the event, e.g., ALEX.CARRIER, RYAN.MCDONAGH

own_goalie str

Name of the goalie for the event team, e.g., JUUSE.SAROS

own_goalie_id str

Identifier for the event team goalie that can be used to match with Evolving Hockey data, e.g., JUUSE.SAROS

opp_forwards str

Names of the opponent's forwards that are on the ice during the event, e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS

opp_forwards_id str

EH IDs of the event team's forwards that are on the ice during the event, e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS

opp_defense str

Names of the opposing team's defensemen that are on the ice during the event, e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN

opp_defense_id str

EH IDs of the opposing team's defensemen that are on the ice during the event, e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN

opp_goalie str

Name of the opposing goalie for the event team, e.g., PHILIPP.GRUBAUER

opp_goalie_id str

Identifier for the opposing goalie that can be used to match with Evolving Hockey data, e.g., PHILIPP.GRUBAUER

toi float

Time on-ice in minutes, e.g., 1.616667

g float

Number of individual goals scored, e.g, 0

a1 float

Number of primary assists, e.g, 0

a2 float

Number of secondary assists, e.g, 0

isf float

Number of indiviudal shots registered, e.g., 0

iff float

Number of indiviudal fenwick events registered, e.g., 0

icf float

Number of indiviudal corsi events registered, e.g., 0

ixg float

Sum value of individual predicted goals (xG), e.g., 0

gax float

Sum value of goals scored above expected, e.g., 0

ihdg float

Sum value of individual high-danger goals scored, e.g., 0

ihdf float

Sum value of individual high-danger fenwick events registered, e.g., 0

ihdsf float

Sum value of individual high-danger shots taken, e.g., 0

ihdm float

Sum value of individual high-danger shots missed, e.g., 0

imsf float

Sum value of individual missed shots, 0

isb float

Sum value of shots taken that were ultimately blocked, e.g., 0

ibs float

Sum value of opponent shots taken that the player ultimately blocked, e.g., 0

igive float

Sum of individual giveaways, e.g., 0

itake float

Sum of individual takeaways, e.g., 0

ihf float

Sum of individual hits for, e.g., 0

iht float

Sum of individual hits taken, e.g., 0

ifow float

Sum of individual faceoffs won, e.g., 0

ifol float

Sum of individual faceoffs lost, e.g., 0

iozfw float

Sum of individual faceoffs won in offensive zone, e.g., 0

iozfl float

Sum of individual faceoffs lost in offensive zone, e.g., 0

inzfw float

Sum of individual faceoffs won in neutral zone, e.g., 0

inzfl float

Sum of individual faceoffs lost in neutral zone, e.g., 0

idzfw float

Sum of individual faceoffs won in defensive zone, e.g., 0

idzfl float

Sum of individual faceoffs lost in defensive zone, e.g., 0

a1_xg float

Sum of xG from primary assists, e.g., 0

a2_xg float

Sum of xG from secondary assists, e.g., 0

ipent0 float

Sum of individual 0-minute penalties taken, e.g., 0

ipent2 float

Sum of individual 2-minute penalties taken, e.g., 0

ipent4 float

Sum of individual 4-minute penalties taken, e.g., 0

ipent5 float

Sum of individual 5-minute penalties taken, e.g., 0

ipent10 float

Sum of individual 10-minute penalties taken, e.g., 0

ipend0 float

Sum of individual 0-minute penalties drawn, e.g., 0

ipend2 float

Sum of individual 2-minute penalties drawn, e.g., 0

ipend4 float

Sum of individual 4-minute penalties drawn, e.g., 0

ipend5 float

Sum of individual 5-minute penalties drawn, e.g., 0

ipend10 float

Sum of individual 10-minute penalties drawn, e.g., 0

ozs float

Sum of changes with offensive zone starts, e.g., 0

nzs float

Sum of changes with neutral zone starts, e.g., 0

dzs float

Sum of changes with defensive zone starts, e.g., 1

otf float

Sum of changes on-the-fly, e.g., 0

gf float

Sum of goals scored while player is on-ice, e.g., 0

gf_adj float

Sum of venue- and score-adjusted goals scored while player is on-ice, e.g., 0

hdgf float

Sum of high-danger goals scored while player is on-ice, e.g., 0

ga float

Sum of goals allowed while player is on-ice, e.g., 0

ga_adj float

Sum of venue- and score-adjusted goals allowed while player is on-ice, e.g., 0

hdga float

Sum of high-danger goals allowed while player is on-ice, e.g., 0

xgf float

Sum of expected goals generated while player is on-ice, e.g., 0.017266

xgf_adj float

Sum of venue- and score-adjusted expected goals generated while player is on-ice, e.g., 0.016472

xga float

Sum of expected goals allowed while player is on-ice, e.g., 0.123475

xga_adj float

Sum of venue- and score-adjusted expected goals allowed while player is on-ice, e.g., 0.129772

sf float

Sum of shots taken while player is on-ice, e.g., 1

sf_adj float

Sum of venue- and score-adjusted shots taken while player is on-ice, e.g., .972

hdsf float

Sum of high-danger shots taken while player is on-ice, e.g., 0

sa float

Sum of shots allowed while player is on-ice, e.g., 0

sa_adj float

Sum of venue- and score-adjusted shots allowed while player is on-ice, e.g., 0

hdsa float

Sum of high-danger shots allowed while player is on-ice, e.g., 0

ff float

Sum of fenwick events generated while player is on-ice, e.g., 1

ff_adj float

Sum of venue- and score-adjusted fenwick events generated while player is on-ice, e.g., 0.968

hdff float

Sum of high-danger fenwick events generated while player is on-ice, e.g., 0

fa float

Sum of fenwick events allowed while player is on-ice, e.g., 1

fa_adj float

Sum of venue- and score-adjusted fenwick events allowed while player is on-ice, e.g., 1.034

hdfa float

Sum of high-danger fenwick events allowed while player is on-ice, e.g., 1

cf float

Sum of corsi events generated while player is on-ice, e.g., 1

cf_adj float

Sum of venue- and score-adjusted corsi events generated while player is on-ice, e.g., 0.970

ca float

Sum of corsi events allowed while player is on-ice, e.g., 2

ca_adj float

Sum of venue- and score-adjusted corsi events allowed while player is on-ice, e.g., 2.064

bsf float

Sum of shots taken that were ultimately blocked while player is on-ice, e.g., 0

bsa float

Sum of shots allowed that were ultimately blocked while player is on-ice, e.g., 1

msf float

Sum of shots taken that missed net while player is on-ice, e.g., 0

hdmsf float

Sum of high-danger shots taken that missed net while player is on-ice, e.g., 0

msa float

Sum of shots allowed that missed net while player is on-ice, e.g., 1

hdmsa float

Sum of high-danger shots allowed that missed net while player is on-ice, e.g., 1

hf float

Sum of hits dished out while player is on-ice, e.g., 0

ht float

Sum of hits taken while player is on-ice, e.g., 0

ozf float

Sum of offensive zone faceoffs that occur while player is on-ice, e.g., 0

nzf float

Sum of neutral zone faceoffs that occur while player is on-ice, e.g., 0

dzf float

Sum of defensive zone faceoffs that occur while player is on-ice, e.g., 1

fow float

Sum of faceoffs won while player is on-ice, e.g., 1

fol float

Sum of faceoffs lost while player is on-ice, e.g., 0

ozfw float

Sum of offensive zone faceoffs won while player is on-ice, e.g., 0

ozfl float

Sum of offensive zone faceoffs lost while player is on-ice, e.g., 1

nzfw float

Sum of neutral zone faceoffs won while player is on-ice, e.g., 0

nzfl float

Sum of neutral zone faceoffs lost while player is on-ice, e.g., 0

dzfw float

Sum of defensive zone faceoffs won while player is on-ice, e.g., 1

dzfl float

Sum of defensive zone faceoffs lost while player is on-ice, e.g., 0

pent0 float

Sum of individual 0-minute penalties taken while player is on-ice, e.g., 0

pent2 float

Sum of individual 2-minute penalties taken while player is on-ice, e.g., 0

pent4 float

Sum of individual 4-minute penalties taken while player is on-ice, e.g., 0

pent5 float

Sum of individual 5-minute penalties taken while player is on-ice, e.g., 0

pent10 float

Sum of individual 10-minute penalties taken while player is on-ice, e.g., 0

pend0 float

Sum of individual 0-minute penalties drawn while player is on-ice, e.g., 0

pend2 float

Sum of individual 2-minute penalties drawn while player is on-ice, e.g., 0

pend4 float

Sum of individual 4-minute penalties drawn while player is on-ice, e.g., 0

pend5 float

Sum of individual 5-minute penalties drawn while player is on-ice, e.g., 0

pend10 float

Sum of individual 10-minute penalties drawn while player is on-ice, e.g., 0

Examples:

Basic play-by-play DataFrame

>>> shifts_raw = pd.read_csv("./raw_shifts.csv")
>>> pbp_raw = pd.read_csv("./raw_pbp.csv")
>>> pbp = prep_pbp(pbp_raw, shifts_raw)

Basic game-level stats, with no teammates or opposition

>>> stats = prep_stats(pbp)

Period-level stats, grouped by teammates

>>> stats = prep_stats(pbp, level="period", teammates=True)

Session-level (e.g., regular seasion) stats, grouped by teammates and opposition

>>> stats = prep_stats(pbp, level="session", teammates=True, opposition=True)
Source code in src/chickenstats/evolving_hockey/stats.py
def prep_stats(
    pbp: pd.DataFrame,
    level: Literal["period", "game", "session", "season"] = "game",
    score: bool = False,
    teammates: bool = False,
    opposition: bool = False,
    disable_progress_bar: bool = False,
) -> pd.DataFrame:
    """Prepares an individual and on-ice stats dataframe using EvolvingHockey data.

    Aggregates to desired level. Capable of returning cuts that account for strength state,
    period, score state, teammates, and opposition.

    Returns a Pandas DataFrame.

    Parameters:
        pbp (pd.DataFrame):
            Dataframe from the prep_pbp function with the default columns argument
        level (str):
            Level to aggregate stats, e.g., 'game'
        score (bool):
            Whether to aggregate to score state level
        teammates (bool):
            Whether to account for teammates when aggregating
        opposition (bool):
            Whether to account for opposition when aggregating
        disable_progress_bar (bool):
            Whether to disable progress bar

    Returns:
        season (int):
            8-digit season code, e.g., 20232024
        session (str):
            Regular season or playoffs, e.g., R
        game_id (int):
            10-digit game identifier, e.g., 2023020015
        game_date (str):
            Date of game in Eastern time-zone, e.g., 2023-10-12
        player (str):
            Name of the player, e.g., FILIP.FORSBERG
        player_id (str):
            Player EH ID, e.g., FILIP.FORSBERG
        position (str):
            Player's position, e.g., L
        team (str):
            3-letter abbreviation of the player's team, e.g., NSH
        opp_team: object
            3-letter abbreviation of the opposing team, e.g., SEA
        strength_state (str):
            Strength state from the perspective of the event team, e.g., 5v5
        score_state (str):
            Score state from the perspective of the event team, e.g., 0v0
        game_period (int):
            Game period, e.g., 1
        forwards (str):
            Names of the event team's forwards that are on the ice during the event,
            e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY
        forwards_id (str):
            EH IDs of the event team's forwards that are on the ice during the event,
            e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY
        defense (str):
            Names of the event team's defensemen that are on the ice during the event,
            e.g., ALEX.CARRIER, RYAN.MCDONAGH
        defense_id (str):
            EH IDs of the event team's defensemen that are on the ice during the event,
            e.g., ALEX.CARRIER, RYAN.MCDONAGH
        own_goalie (str):
            Name of the goalie for the event team, e.g., JUUSE.SAROS
        own_goalie_id (str):
            Identifier for the event team goalie that can be used to match with Evolving Hockey data, e.g., JUUSE.SAROS
        opp_forwards (str):
            Names of the opponent's forwards that are on the ice during the event,
            e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS
        opp_forwards_id (str):
            EH IDs of the event team's forwards that are on the ice during the event,
            e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS
        opp_defense(str):
            Names of the opposing team's defensemen that are on the ice during the event,
            e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN
        opp_defense_id (str):
            EH IDs of the opposing team's defensemen that are on the ice during the event,
            e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN
        opp_goalie (str):
            Name of the opposing goalie for the event team, e.g., PHILIPP.GRUBAUER
        opp_goalie_id (str):
            Identifier for the opposing goalie that can be used to match with Evolving Hockey data,
            e.g., PHILIPP.GRUBAUER
        toi (float):
            Time on-ice in minutes, e.g., 1.616667
        g (float):
            Number of individual goals scored, e.g, 0
        a1 (float):
            Number of primary assists, e.g, 0
        a2 (float):
            Number of secondary assists, e.g, 0
        isf (float):
            Number of indiviudal shots registered, e.g., 0
        iff (float):
            Number of indiviudal fenwick events registered, e.g., 0
        icf (float):
            Number of indiviudal corsi events registered, e.g., 0
        ixg (float):
            Sum value of individual predicted goals (xG), e.g., 0
        gax (float):
            Sum value of goals scored above expected, e.g., 0
        ihdg (float):
            Sum value of individual high-danger goals scored, e.g., 0
        ihdf (float):
            Sum value of individual high-danger fenwick events registered, e.g., 0
        ihdsf (float):
            Sum value of individual high-danger shots taken, e.g., 0
        ihdm (float):
            Sum value of individual high-danger shots missed, e.g., 0
        imsf (float):
            Sum value of individual missed shots, 0
        isb (float):
            Sum value of shots taken that were ultimately blocked, e.g., 0
        ibs (float):
            Sum value of opponent shots taken that the player ultimately blocked, e.g., 0
        igive (float):
            Sum of individual giveaways, e.g., 0
        itake (float):
            Sum of individual takeaways, e.g., 0
        ihf (float):
            Sum of individual hits for, e.g., 0
        iht (float):
            Sum of individual hits taken, e.g., 0
        ifow (float):
            Sum of individual faceoffs won, e.g., 0
        ifol (float):
            Sum of individual faceoffs lost, e.g., 0
        iozfw (float):
            Sum of individual faceoffs won in offensive zone, e.g., 0
        iozfl (float):
            Sum of individual faceoffs lost in offensive zone, e.g., 0
        inzfw (float):
            Sum of individual faceoffs won in neutral zone, e.g., 0
        inzfl (float):
            Sum of individual faceoffs lost in neutral zone, e.g., 0
        idzfw (float):
            Sum of individual faceoffs won in defensive zone, e.g., 0
        idzfl (float):
            Sum of individual faceoffs lost in defensive zone, e.g., 0
        a1_xg (float):
            Sum of xG from primary assists, e.g., 0
        a2_xg (float):
            Sum of xG from secondary assists, e.g., 0
        ipent0 (float):
            Sum of individual 0-minute penalties taken, e.g., 0
        ipent2 (float):
            Sum of individual 2-minute penalties taken, e.g., 0
        ipent4 (float):
            Sum of individual 4-minute penalties taken, e.g., 0
        ipent5 (float):
            Sum of individual 5-minute penalties taken, e.g., 0
        ipent10 (float):
            Sum of individual 10-minute penalties taken, e.g., 0
        ipend0 (float):
            Sum of individual 0-minute penalties drawn, e.g., 0
        ipend2 (float):
            Sum of individual 2-minute penalties drawn, e.g., 0
        ipend4 (float):
            Sum of individual 4-minute penalties drawn, e.g., 0
        ipend5 (float):
            Sum of individual 5-minute penalties drawn, e.g., 0
        ipend10 (float):
            Sum of individual 10-minute penalties drawn, e.g., 0
        ozs (float):
            Sum of changes with offensive zone starts, e.g., 0
        nzs (float):
            Sum of changes with neutral zone starts, e.g., 0
        dzs (float):
            Sum of changes with defensive zone starts, e.g., 1
        otf (float):
            Sum of changes on-the-fly, e.g., 0
        gf (float):
            Sum of goals scored while player is on-ice, e.g., 0
        gf_adj (float):
            Sum of venue- and score-adjusted goals scored while player is on-ice, e.g., 0
        hdgf (float):
            Sum of high-danger goals scored while player is on-ice, e.g., 0
        ga (float):
            Sum of goals allowed while player is on-ice, e.g., 0
        ga_adj (float):
            Sum of venue- and score-adjusted goals allowed while player is on-ice, e.g., 0
        hdga (float):
            Sum of high-danger goals allowed while player is on-ice, e.g., 0
        xgf (float):
            Sum of expected goals generated while player is on-ice, e.g., 0.017266
        xgf_adj (float):
            Sum of venue- and score-adjusted expected goals generated while player is on-ice, e.g., 0.016472
        xga (float):
            Sum of expected goals allowed while player is on-ice, e.g., 0.123475
        xga_adj (float):
            Sum of venue- and score-adjusted expected goals allowed while player is on-ice, e.g., 0.129772
        sf (float):
            Sum of shots taken while player is on-ice, e.g., 1
        sf_adj (float):
            Sum of venue- and score-adjusted shots taken while player is on-ice, e.g., .972
        hdsf (float):
            Sum of high-danger shots taken while player is on-ice, e.g., 0
        sa (float):
            Sum of shots allowed while player is on-ice, e.g., 0
        sa_adj (float):
            Sum of venue- and score-adjusted shots allowed while player is on-ice, e.g., 0
        hdsa (float):
            Sum of high-danger shots allowed while player is on-ice, e.g., 0
        ff (float):
            Sum of fenwick events generated while player is on-ice, e.g., 1
        ff_adj (float):
            Sum of venue- and score-adjusted fenwick events generated while player is on-ice, e.g., 0.968
        hdff (float):
            Sum of high-danger fenwick events generated while player is on-ice, e.g., 0
        fa (float):
            Sum of fenwick events allowed while player is on-ice, e.g., 1
        fa_adj (float):
            Sum of venue- and score-adjusted fenwick events allowed while player is on-ice, e.g., 1.034
        hdfa (float):
            Sum of high-danger fenwick events allowed while player is on-ice, e.g., 1
        cf (float):
            Sum of corsi events generated while player is on-ice, e.g., 1
        cf_adj (float):
            Sum of venue- and score-adjusted corsi events generated while player is on-ice, e.g., 0.970
        ca (float):
            Sum of corsi events allowed while player is on-ice, e.g., 2
        ca_adj (float):
            Sum of venue- and score-adjusted corsi events allowed while player is on-ice, e.g., 2.064
        bsf (float):
            Sum of shots taken that were ultimately blocked while player is on-ice, e.g., 0
        bsa (float):
            Sum of shots allowed that were ultimately blocked while player is on-ice, e.g., 1
        msf (float):
            Sum of shots taken that missed net while player is on-ice, e.g., 0
        hdmsf (float):
            Sum of high-danger shots taken that missed net while player is on-ice, e.g., 0
        msa (float):
            Sum of shots allowed that missed net while player is on-ice, e.g., 1
        hdmsa (float):
            Sum of high-danger shots allowed that missed net while player is on-ice, e.g., 1
        hf (float):
            Sum of hits dished out while player is on-ice, e.g., 0
        ht (float):
            Sum of hits taken while player is on-ice, e.g., 0
        ozf (float):
            Sum of offensive zone faceoffs that occur while player is on-ice, e.g., 0
        nzf (float):
            Sum of neutral zone faceoffs that occur while player is on-ice, e.g., 0
        dzf (float):
            Sum of defensive zone faceoffs that occur while player is on-ice, e.g., 1
        fow (float):
            Sum of faceoffs won while player is on-ice, e.g., 1
        fol (float):
            Sum of faceoffs lost while player is on-ice, e.g., 0
        ozfw (float):
            Sum of offensive zone faceoffs won while player is on-ice, e.g., 0
        ozfl (float):
            Sum of offensive zone faceoffs lost while player is on-ice, e.g., 1
        nzfw (float):
            Sum of neutral zone faceoffs won while player is on-ice, e.g., 0
        nzfl (float):
            Sum of neutral zone faceoffs lost while player is on-ice, e.g., 0
        dzfw (float):
            Sum of defensive zone faceoffs won while player is on-ice, e.g., 1
        dzfl (float):
            Sum of defensive zone faceoffs lost while player is on-ice, e.g., 0
        pent0 (float):
            Sum of individual 0-minute penalties taken while player is on-ice, e.g., 0
        pent2 (float):
            Sum of individual 2-minute penalties taken while player is on-ice, e.g., 0
        pent4 (float):
            Sum of individual 4-minute penalties taken while player is on-ice, e.g., 0
        pent5 (float):
            Sum of individual 5-minute penalties taken while player is on-ice, e.g., 0
        pent10 (float):
            Sum of individual 10-minute penalties taken while player is on-ice, e.g., 0
        pend0 (float):
            Sum of individual 0-minute penalties drawn while player is on-ice, e.g., 0
        pend2 (float):
            Sum of individual 2-minute penalties drawn while player is on-ice, e.g., 0
        pend4 (float):
            Sum of individual 4-minute penalties drawn while player is on-ice, e.g., 0
        pend5 (float):
            Sum of individual 5-minute penalties drawn while player is on-ice, e.g., 0
        pend10 (float):
            Sum of individual 10-minute penalties drawn while player is on-ice, e.g., 0

    Examples:
        Basic play-by-play DataFrame
        >>> shifts_raw = pd.read_csv("./raw_shifts.csv")
        >>> pbp_raw = pd.read_csv("./raw_pbp.csv")
        >>> pbp = prep_pbp(pbp_raw, shifts_raw)

        Basic game-level stats, with no teammates or opposition
        >>> stats = prep_stats(pbp)

        Period-level stats, grouped by teammates
        >>> stats = prep_stats(pbp, level="period", teammates=True)

        Session-level (e.g., regular seasion) stats, grouped by teammates and opposition
        >>> stats = prep_stats(pbp, level="session", teammates=True, opposition=True)

    """
    with ChickenProgress(disable=disable_progress_bar) as progress:
        pbar_message = "Prepping stats data..."

        stats_task = progress.add_task(pbar_message, total=1)

        ind = prep_ind(pbp, level, score, teammates, opposition)

        oi = prep_oi(pbp, level, score, teammates, opposition)

        zones = prep_zones(pbp, level, score, teammates, opposition)

        merge_cols = [
            "season",
            "session",
            "game_id",
            "game_date",
            "player",
            "player_id",
            "position",
            "team",
            "opp_team",
            "strength_state",
            "score_state",
            "game_period",
            "forwards",
            "forwards_id",
            "defense",
            "defense_id",
            "own_goalie",
            "own_goalie_id",
            "opp_forwards",
            "opp_forwards_id",
            "opp_defense",
            "opp_defense_id",
            "opp_goalie",
            "opp_goalie_id",
        ]

        merge_cols = [
            x
            for x in merge_cols
            if x in ind.columns and x in oi.columns and x in zones.columns
        ]

        stats = oi.merge(
            ind, how="left", left_on=merge_cols, right_on=merge_cols
        ).fillna(0)

        stats = stats.merge(
            zones, how="left", left_on=merge_cols, right_on=merge_cols
        ).fillna(0)

        stats = stats.loc[stats.toi > 0].reset_index(drop=True).copy()

        columns = [x for x in StatSchema.dtypes.keys() if x in stats.columns]

        stats = StatSchema.validate(stats[columns])

        pbar_message = "Finished prepping stats data"

        progress.update(stats_task, description=pbar_message, advance=1, refresh=True)

    return stats

prep_lines

prep_lines(
    pbp,
    position="f",
    level="game",
    score=False,
    teammates=False,
    opposition=False,
    disable_progress_bar=False,
)

Prepares a line stats dataframe using EvolvingHockey data.

Aggregates to desired level. Capable of returning cuts that account for strength state, period, score state, teammates, and opposition.

Returns a Pandas DataFrame.

Parameters:

Name Type Description Default
pbp DataFrame

Dataframe from the prep_pbp function with the default columns argument

required
position str

Position to aggregate, forwards or defense, e.g., 'f'

'f'
level str

Level to aggregate stats, e.g., 'game'

'game'
score bool

Whether to aggregate to score state level

False
teammates bool

Whether to account for teammates when aggregating

False
opposition bool

Whether to account for opposition when aggregating

False
disable_progress_bar bool

Whether to disable progress bar

False

Returns:

Name Type Description
season int

8-digit season code, e.g., 20232024

session str

Regular season or playoffs, e.g., R

game_id int

10-digit game identifier, e.g., 2023020015

game_date str

Date of game in Eastern time-zone, e.g., 2023-10-12

team str

3-letter abbreviation of the line's team, e.g., NSH

opp_team str

3-letter abbreviation of the opposing team, e.g., SEA

strength_state str

Strength state from the perspective of the event team, e.g., 5v5

score_state str

Score state from the perspective of the event team, e.g., 0v0

game_period int

Game period, e.g., 1

forwards str

Names of the event team's forwards that are on the ice during the event, e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY

forwards_id str

EH IDs of the event team's forwards that are on the ice during the event, e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY

defense str

Names of the event team's defensemen that are on the ice during the event, e.g., ALEX.CARRIER, RYAN.MCDONAGH

defense_id str

EH IDs of the event team's defensemen that are on the ice during the event, e.g., ALEX.CARRIER, RYAN.MCDONAGH

own_goalie str

Name of the goalie for the event team, e.g., JUUSE.SAROS

own_goalie_id str

Identifier for the event team goalie that can be used to match with Evolving Hockey data, e.g., JUUSE.SAROS

opp_forwards str

Names of the opponent's forwards that are on the ice during the event, e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS

opp_forwards_id str

EH IDs of the event team's forwards that are on the ice during the event, e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS

opp_defense str

Names of the opposing team's defensemen that are on the ice during the event, e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN

opp_defense_id str

EH IDs of the opposing team's defensemen that are on the ice during the event, e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN

opp_goalie str

Name of the opposing goalie for the event team, e.g., PHILIPP.GRUBAUER

opp_goalie_id str

Identifier for the opposing goalie that can be used to match with Evolving Hockey data, e.g., PHILIPP.GRUBAUER

toi float

Time on-ice in minutes, e.g., 1.616667

gf float

Sum of goals scored while line is on-ice, e.g., 0

gf_adj float

Sum of venue- and score-adjusted goals scored while line is on-ice, e.g., 0

hdgf float

Sum of high-danger goals scored while line is on-ice, e.g., 0

ga float

Sum of goals allowed while line is on-ice, e.g., 0

ga_adj float

Sum of venue- and score-adjusted goals allowed while line is on-ice, e.g., 0

hdga float

Sum of high-danger goals allowed while line is on-ice, e.g., 0

xgf float

Sum of expected goals generated while line is on-ice, e.g., 0.017266

xgf_adj float

Sum of venue- and score-adjusted expected goals generated while line is on-ice, e.g., 0.016472

xga float

Sum of expected goals allowed while line is on-ice, e.g., 0.123475

xga_adj float

Sum of venue- and score-adjusted expected goals allowed while line is on-ice, e.g., 0.129772

sf float

Sum of shots taken while line is on-ice, e.g., 1

sf_adj float

Sum of venue- and score-adjusted shots taken while line is on-ice, e.g., .972

hdsf float

Sum of high-danger shots taken while line is on-ice, e.g., 0

sa float

Sum of shots allowed while line is on-ice, e.g., 0

sa_adj float

Sum of venue- and score-adjusted shots allowed while line is on-ice, e.g., 0

hdsa float

Sum of high-danger shots allowed while line is on-ice, e.g., 0

ff float

Sum of fenwick events generated while line is on-ice, e.g., 1

ff_adj float

Sum of venue- and score-adjusted fenwick events generated while line is on-ice, e.g., 0.968

hdff float

Sum of high-danger fenwick events generated while line is on-ice, e.g., 0

fa float

Sum of fenwick events allowed while line is on-ice, e.g., 1

fa_adj float

Sum of venue- and score-adjusted fenwick events allowed while line is on-ice, e.g., 1.034

hdfa float

Sum of high-danger fenwick events allowed while line is on-ice, e.g., 1

cf float

Sum of corsi events generated while line is on-ice, e.g., 1

cf_adj float

Sum of venue- and score-adjusted corsi events generated while line is on-ice, e.g., 0.970

ca float

Sum of corsi events allowed while line is on-ice, e.g., 2

ca_adj float

Sum of venue- and score-adjusted corsi events allowed while line is on-ice, e.g., 2.064

bsf float

Sum of shots taken that were ultimately blocked while line is on-ice, e.g., 0

bsa float

Sum of shots allowed that were ultimately blocked while line is on-ice, e.g., 1

msf float

Sum of shots taken that missed net while line is on-ice, e.g., 0

hdmsf float

Sum of high-danger shots taken that missed net while line is on-ice, e.g., 0

msa float

Sum of shots allowed that missed net while line is on-ice, e.g., 1

hdmsa float

Sum of high-danger shots allowed that missed net while line is on-ice, e.g., 1

hf float

Sum of hits dished out while line is on-ice, e.g., 0

ht float

Sum of hits taken while line is on-ice, e.g., 0

ozf float

Sum of offensive zone faceoffs that occur while line is on-ice, e.g., 0

nzf float

Sum of neutral zone faceoffs that occur while line is on-ice, e.g., 0

dzf float

Sum of defensive zone faceoffs that occur while line is on-ice, e.g., 1

fow float

Sum of faceoffs won while line is on-ice, e.g., 1

fol float

Sum of faceoffs lost while line is on-ice, e.g., 0

ozfw float

Sum of offensive zone faceoffs won while line is on-ice, e.g., 0

ozfl float

Sum of offensive zone faceoffs lost while line is on-ice, e.g., 1

nzfw float

Sum of neutral zone faceoffs won while line is on-ice, e.g., 0

nzfl float

Sum of neutral zone faceoffs lost while line is on-ice, e.g., 0

dzfw float

Sum of defensive zone faceoffs won while line is on-ice, e.g., 1

dzfl float

Sum of defensive zone faceoffs lost while line is on-ice, e.g., 0

pent0 float

Sum of individual 0-minute penalties taken while line is on-ice, e.g., 0

pent2 float

Sum of individual 2-minute penalties taken while line is on-ice, e.g., 0

pent4 float

Sum of individual 4-minute penalties taken while line is on-ice, e.g., 0

pent5 float

Sum of individual 5-minute penalties taken while line is on-ice, e.g., 0

pent10 float

Sum of individual 10-minute penalties taken while line is on-ice, e.g., 0

pend0 float

Sum of individual 0-minute penalties drawn while line is on-ice, e.g., 0

pend2 float

Sum of individual 2-minute penalties drawn while line is on-ice, e.g., 0

pend4 float

Sum of individual 4-minute penalties drawn while line is on-ice, e.g., 0

pend5 float

Sum of individual 5-minute penalties drawn while line is on-ice, e.g., 0

pend10 float

Sum of individual 10-minute penalties drawn while line is on-ice, e.g., 0

Examples:

Basic play-by-play DataFrame

>>> shifts_raw = pd.read_csv("./raw_shifts.csv")
>>> pbp_raw = pd.read_csv("./raw_pbp.csv")
>>> pbp = prep_pbp(pbp_raw, shifts_raw)

Basic game-level stats for forwards, with no teammates or opposition

>>> lines = prep_lines(pbp, position="f")

Period-level stats for defense, grouped by teammates

>>> lines = prep_lines(pbp, position="d", level="period", teammates=True)

Session-level (e.g., regular seasion) stats, grouped by teammates and opposition

>>> lines = prep_lines(
...     pbp, position="f", level="session", teammates=True, opposition=True
... )
Source code in src/chickenstats/evolving_hockey/stats.py
 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
def prep_lines(
    pbp: pd.DataFrame,
    position: Literal["f", "d"] = "f",
    level: Literal["period", "game", "session", "season"] = "game",
    score: bool = False,
    teammates: bool = False,
    opposition: bool = False,
    disable_progress_bar: bool = False,
):
    """Prepares a line stats dataframe using EvolvingHockey data.

    Aggregates to desired level. Capable of returning cuts that account for strength state,
    period, score state, teammates, and opposition.

    Returns a Pandas DataFrame.

    Parameters:
        pbp (pd.DataFrame):
            Dataframe from the prep_pbp function with the default columns argument
        position (str):
            Position to aggregate, forwards or defense, e.g., 'f'
        level (str):
            Level to aggregate stats, e.g., 'game'
        score (bool):
            Whether to aggregate to score state level
        teammates (bool):
            Whether to account for teammates when aggregating
        opposition (bool):
            Whether to account for opposition when aggregating
        disable_progress_bar (bool):
            Whether to disable progress bar

    Returns:
        season (int):
            8-digit season code, e.g., 20232024
        session (str):
            Regular season or playoffs, e.g., R
        game_id (int):
            10-digit game identifier, e.g., 2023020015
        game_date (str):
            Date of game in Eastern time-zone, e.g., 2023-10-12
        team (str):
            3-letter abbreviation of the line's team, e.g., NSH
        opp_team (str):
            3-letter abbreviation of the opposing team, e.g., SEA
        strength_state (str):
            Strength state from the perspective of the event team, e.g., 5v5
        score_state (str):
            Score state from the perspective of the event team, e.g., 0v0
        game_period (int):
            Game period, e.g., 1
        forwards (str):
            Names of the event team's forwards that are on the ice during the event,
            e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY
        forwards_id (str):
            EH IDs of the event team's forwards that are on the ice during the event,
            e.g., FILIP.FORSBERG, JUUSO.PARSSINEN, RYAN.O'REILLY
        defense (str):
            Names of the event team's defensemen that are on the ice during the event,
            e.g., ALEX.CARRIER, RYAN.MCDONAGH
        defense_id (str):
            EH IDs of the event team's defensemen that are on the ice during the event,
            e.g., ALEX.CARRIER, RYAN.MCDONAGH
        own_goalie (str):
            Name of the goalie for the event team, e.g., JUUSE.SAROS
        own_goalie_id (str):
            Identifier for the event team goalie that can be used to match with Evolving Hockey data, e.g., JUUSE.SAROS
        opp_forwards (str):
            Names of the opponent's forwards that are on the ice during the event,
            e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS
        opp_forwards_id (str):
            EH IDs of the event team's forwards that are on the ice during the event,
            e.g., JARED.MCCANN, JORDAN.EBERLE, MATTY.BENIERS
        opp_defense (str):
            Names of the opposing team's defensemen that are on the ice during the event,
            e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN
        opp_defense_id (str):
            EH IDs of the opposing team's defensemen that are on the ice during the event,
            e.g., JAMIE.OLEKSIAK, WILLIAM.BORGEN
        opp_goalie (str):
            Name of the opposing goalie for the event team, e.g., PHILIPP.GRUBAUER
        opp_goalie_id (str):
            Identifier for the opposing goalie that can be used to match with Evolving Hockey data,
            e.g., PHILIPP.GRUBAUER
        toi (float):
            Time on-ice in minutes, e.g., 1.616667
        gf (float):
            Sum of goals scored while line is on-ice, e.g., 0
        gf_adj (float):
            Sum of venue- and score-adjusted goals scored while line is on-ice, e.g., 0
        hdgf (float):
            Sum of high-danger goals scored while line is on-ice, e.g., 0
        ga (float):
            Sum of goals allowed while line is on-ice, e.g., 0
        ga_adj (float):
            Sum of venue- and score-adjusted goals allowed while line is on-ice, e.g., 0
        hdga (float):
            Sum of high-danger goals allowed while line is on-ice, e.g., 0
        xgf (float):
            Sum of expected goals generated while line is on-ice, e.g., 0.017266
        xgf_adj (float):
            Sum of venue- and score-adjusted expected goals generated while line is on-ice, e.g., 0.016472
        xga (float):
            Sum of expected goals allowed while line is on-ice, e.g., 0.123475
        xga_adj (float):
            Sum of venue- and score-adjusted expected goals allowed while line is on-ice, e.g., 0.129772
        sf (float):
            Sum of shots taken while line is on-ice, e.g., 1
        sf_adj (float):
            Sum of venue- and score-adjusted shots taken while line is on-ice, e.g., .972
        hdsf (float):
            Sum of high-danger shots taken while line is on-ice, e.g., 0
        sa (float):
            Sum of shots allowed while line is on-ice, e.g., 0
        sa_adj (float):
            Sum of venue- and score-adjusted shots allowed while line is on-ice, e.g., 0
        hdsa (float):
            Sum of high-danger shots allowed while line is on-ice, e.g., 0
        ff (float):
            Sum of fenwick events generated while line is on-ice, e.g., 1
        ff_adj (float):
            Sum of venue- and score-adjusted fenwick events generated while line is on-ice, e.g., 0.968
        hdff (float):
            Sum of high-danger fenwick events generated while line is on-ice, e.g., 0
        fa (float):
            Sum of fenwick events allowed while line is on-ice, e.g., 1
        fa_adj (float):
            Sum of venue- and score-adjusted fenwick events allowed while line is on-ice, e.g., 1.034
        hdfa (float):
            Sum of high-danger fenwick events allowed while line is on-ice, e.g., 1
        cf (float):
            Sum of corsi events generated while line is on-ice, e.g., 1
        cf_adj (float):
            Sum of venue- and score-adjusted corsi events generated while line is on-ice, e.g., 0.970
        ca (float):
            Sum of corsi events allowed while line is on-ice, e.g., 2
        ca_adj (float):
            Sum of venue- and score-adjusted corsi events allowed while line is on-ice, e.g., 2.064
        bsf (float):
            Sum of shots taken that were ultimately blocked while line is on-ice, e.g., 0
        bsa (float):
            Sum of shots allowed that were ultimately blocked while line is on-ice, e.g., 1
        msf (float):
            Sum of shots taken that missed net while line is on-ice, e.g., 0
        hdmsf (float):
            Sum of high-danger shots taken that missed net while line is on-ice, e.g., 0
        msa (float):
            Sum of shots allowed that missed net while line is on-ice, e.g., 1
        hdmsa (float):
            Sum of high-danger shots allowed that missed net while line is on-ice, e.g., 1
        hf (float):
            Sum of hits dished out while line is on-ice, e.g., 0
        ht (float):
            Sum of hits taken while line is on-ice, e.g., 0
        ozf (float):
            Sum of offensive zone faceoffs that occur while line is on-ice, e.g., 0
        nzf (float):
            Sum of neutral zone faceoffs that occur while line is on-ice, e.g., 0
        dzf (float):
            Sum of defensive zone faceoffs that occur while line is on-ice, e.g., 1
        fow (float):
            Sum of faceoffs won while line is on-ice, e.g., 1
        fol (float):
            Sum of faceoffs lost while line is on-ice, e.g., 0
        ozfw (float):
            Sum of offensive zone faceoffs won while line is on-ice, e.g., 0
        ozfl (float):
            Sum of offensive zone faceoffs lost while line is on-ice, e.g., 1
        nzfw (float):
            Sum of neutral zone faceoffs won while line is on-ice, e.g., 0
        nzfl (float):
            Sum of neutral zone faceoffs lost while line is on-ice, e.g., 0
        dzfw (float):
            Sum of defensive zone faceoffs won while line is on-ice, e.g., 1
        dzfl (float):
            Sum of defensive zone faceoffs lost while line is on-ice, e.g., 0
        pent0 (float):
            Sum of individual 0-minute penalties taken while line is on-ice, e.g., 0
        pent2 (float):
            Sum of individual 2-minute penalties taken while line is on-ice, e.g., 0
        pent4 (float):
            Sum of individual 4-minute penalties taken while line is on-ice, e.g., 0
        pent5 (float):
            Sum of individual 5-minute penalties taken while line is on-ice, e.g., 0
        pent10 (float):
            Sum of individual 10-minute penalties taken while line is on-ice, e.g., 0
        pend0 (float):
            Sum of individual 0-minute penalties drawn while line is on-ice, e.g., 0
        pend2 (float):
            Sum of individual 2-minute penalties drawn while line is on-ice, e.g., 0
        pend4 (float):
            Sum of individual 4-minute penalties drawn while line is on-ice, e.g., 0
        pend5 (float):
            Sum of individual 5-minute penalties drawn while line is on-ice, e.g., 0
        pend10 (float):
            Sum of individual 10-minute penalties drawn while line is on-ice, e.g., 0

    Examples:
        Basic play-by-play DataFrame
        >>> shifts_raw = pd.read_csv("./raw_shifts.csv")
        >>> pbp_raw = pd.read_csv("./raw_pbp.csv")
        >>> pbp = prep_pbp(pbp_raw, shifts_raw)

        Basic game-level stats for forwards, with no teammates or opposition
        >>> lines = prep_lines(pbp, position="f")

        Period-level stats for defense, grouped by teammates
        >>> lines = prep_lines(pbp, position="d", level="period", teammates=True)

        Session-level (e.g., regular seasion) stats, grouped by teammates and opposition
        >>> lines = prep_lines(
        ...     pbp, position="f", level="session", teammates=True, opposition=True
        ... )

    """
    with ChickenProgress(disable=disable_progress_bar) as progress:
        pbar_message = "Prepping lines data..."

        lines_task = progress.add_task(pbar_message, total=1)

        # Creating the "for" dataframe

        # Accounting for desired level of aggregation

        if level == "session" or level == "season":
            group_base = ["season", "session", "event_team", "strength_state"]

        if level == "game":
            group_base = [
                "season",
                "game_id",
                "game_date",
                "session",
                "event_team",
                "opp_team",
                "strength_state",
            ]

        if level == "period":
            group_base = [
                "season",
                "game_id",
                "game_date",
                "session",
                "event_team",
                "opp_team",
                "game_period",
                "strength_state",
            ]

        # Accounting for score state

        if score is True:
            group_base = group_base + ["score_state"]

        # Accounting for desired position

        group_list = group_base + [f"event_on_{position}", f"event_on_{position}_id"]

        # Accounting for teammates

        if teammates is True:
            if position == "f":
                group_list = group_list + [
                    "event_on_d",
                    "event_on_d_id",
                    "event_on_g",
                    "event_on_g_id",
                ]

            if position == "d":
                group_list = group_list + [
                    "event_on_f",
                    "event_on_f_id",
                    "event_on_g",
                    "event_on_g_id",
                ]

        # Accounting for opposition

        if opposition is True:
            group_list = group_list + [
                "opp_on_f",
                "opp_on_f_id",
                "opp_on_d",
                "opp_on_d_id",
                "opp_on_g",
                "opp_on_g_id",
            ]

            if "opp_team" not in group_list:
                group_list.append("opp_team")

        # Creating dictionary of statistics for the groupby function

        stats = [
            "pred_goal",
            "pred_goal_adj",
            "corsi",
            "corsi_adj",
            "fenwick",
            "fenwick_adj",
            "goal",
            "goal_adj",
            "miss",
            "block",
            "shot",
            "shot_adj",
            "hd_goal",
            "hd_shot",
            "hd_fenwick",
            "hd_miss",
            "event_length",
            "fac",
            "ozf",
            "nzf",
            "dzf",
            "hit",
            "give",
            "take",
            "pen0",
            "pen2",
            "pen4",
            "pen5",
            "pen10",
        ]

        agg_stats = {x: "sum" for x in stats if x in pbp.columns}

        # Aggregating the "for" dataframe

        lines_f = pbp.groupby(group_list, as_index=False, dropna=False).agg(agg_stats)

        # Creating the dictionary to change column names

        columns = [
            "xgf",
            "xgf_adj",
            "cf",
            "cf_adj",
            "ff",
            "ff_adj",
            "gf",
            "gf_adj",
            "msf",
            "bsf",
            "sf",
            "sf_adj",
            "hdgf",
            "hdsf",
            "hdff",
            "hdmsf",
            "toi",
            "fow",
            "ozfw",
            "nzfw",
            "dzfw",
            "hf",
            "give",
            "take",
            "pent0",
            "pent2",
            "pent4",
            "pent5",
            "pent10",
        ]

        columns = dict(zip(stats, columns))

        # Accounting for positions

        columns.update(
            {
                "event_on_f": "forwards",
                "event_on_f_id": "forwards_id",
                "event_team": "team",
                "event_on_d": "defense",
                "event_on_d_id": "defense_id",
                "event_on_g": "own_goalie",
                "event_on_g_id": "own_goalie_id",
                "opp_on_f": "opp_forwards",
                "opp_on_f_id": "opp_forwards_id",
                "opp_on_d": "opp_defense",
                "opp_on_d_id": "opp_defense_id",
                "opp_on_g": "opp_goalie",
                "opp_on_g_id": "opp_goalie_id",
            }
        )

        # columns = {k: v for k, v in columns.items() if k in lines_f.columns}

        lines_f = lines_f.rename(columns=columns)

        cols = [
            "forwards",
            "forwards_id",
            "defense",
            "defense_id",
            "own_goalie",
            "own_goalie_id",
            "opp_forwards",
            "opp_forwards_id",
            "opp_defense",
            "opp_defense_id",
            "opp_goalie",
            "opp_goalie_id",
        ]

        cols = [x for x in cols if x in lines_f]

        for col in cols:
            lines_f[col] = lines_f[col].fillna("EMPTY")

        # Creating the against dataframe

        # Accounting for desired level of aggregation

        if level == "session" or level == "season":
            group_base = ["season", "session", "opp_team", "opp_strength_state"]

        if level == "game":
            group_base = [
                "season",
                "game_id",
                "game_date",
                "session",
                "event_team",
                "opp_team",
                "opp_strength_state",
            ]

        if level == "period":
            group_base = [
                "season",
                "game_id",
                "game_date",
                "session",
                "event_team",
                "opp_team",
                "game_period",
                "opp_strength_state",
            ]

        # Accounting for score state

        if score is True:
            group_base = group_base + ["opp_score_state"]

        # Accounting for desired position

        group_list = group_base + [f"opp_on_{position}", f"opp_on_{position}_id"]

        # Accounting for teammates

        if teammates is True:
            if position == "f":
                group_list = group_list + [
                    "opp_on_d",
                    "opp_on_d_id",
                    "opp_on_g",
                    "opp_on_g_id",
                ]

            if position == "d":
                group_list = group_list + [
                    "opp_on_f",
                    "opp_on_f_id",
                    "opp_on_g",
                    "opp_on_g_id",
                ]

        # Accounting for opposition

        if opposition is True:
            group_list = group_list + [
                "event_on_f",
                "event_on_f_id",
                "event_on_d",
                "event_on_d_id",
                "event_on_g",
                "event_on_g_id",
            ]

            if "event_team" not in group_list:
                group_list.append("event_team")

        # Creating dictionary of statistics for the groupby function

        stats = [
            "pred_goal",
            "pred_goal_adj",
            "corsi",
            "corsi_adj",
            "fenwick",
            "fenwick_adj",
            "goal",
            "goal_adj",
            "miss",
            "block",
            "shot",
            "shot_adj",
            "hd_goal",
            "hd_shot",
            "hd_fenwick",
            "hd_miss",
            "event_length",
            "fac",
            "ozf",
            "nzf",
            "dzf",
            "hit",
            "pen0",
            "pen2",
            "pen4",
            "pen5",
            "pen10",
        ]

        agg_stats = {x: "sum" for x in stats if x in pbp.columns}

        # Aggregating "against" dataframe

        lines_a = pbp.groupby(group_list, as_index=False, dropna=False).agg(agg_stats)

        # Creating the dictionary to change column names

        columns = [
            "xga",
            "xga_adj",
            "ca",
            "ca_adj",
            "fa",
            "fa_adj",
            "ga",
            "ga_adj",
            "msa",
            "bsa",
            "sa",
            "sa_adj",
            "hdga",
            "hdsa",
            "hdfa",
            "hdmsa",
            "toi",
            "fol",
            "ozfl",
            "nzfl",
            "dzfl",
            "ht",
            "pend0",
            "pend2",
            "pend4",
            "pend5",
            "pend10",
        ]

        columns = dict(zip(stats, columns))

        # Accounting for positions

        columns.update(
            {
                "opp_team": "team",
                "event_team": "opp_team",
                "opp_on_f": "forwards",
                "opp_on_f_id": "forwards_id",
                "opp_strength_state": "strength_state",
                "opp_on_d": "defense",
                "opp_on_d_id": "defense_id",
                "event_on_f": "opp_forwards",
                "event_on_f_id": "opp_forwards_id",
                "event_on_d": "opp_defense",
                "event_on_d_id": "opp_defense_id",
                "opp_score_state": "score_state",
                "event_on_g": "opp_goalie",
                "event_on_g_id": "opp_goalie_id",
                "opp_on_g": "own_goalie",
                "opp_on_g_id": "own_goalie_id",
            }
        )

        # columns = {k: v for k, v in columns.items() if k in lines_a.columns}

        lines_a = lines_a.rename(columns=columns)

        cols = [
            "forwards",
            "forwards_id",
            "defense",
            "defense_id",
            "own_goalie",
            "own_goalie_id",
            "opp_forwards",
            "opp_forwards_id",
            "opp_defense",
            "opp_defense_id",
            "opp_goalie",
            "opp_goalie_id",
        ]

        cols = [x for x in cols if x in lines_a]

        for col in cols:
            lines_a[col] = lines_a[col].fillna("EMPTY")

        # Merging the "for" and "against" dataframes

        if level == "session" or level == "season":
            if position == "f":
                merge_list = [
                    "season",
                    "session",
                    "team",
                    "strength_state",
                    "forwards",
                    "forwards_id",
                ]

            if position == "d":
                merge_list = [
                    "season",
                    "session",
                    "team",
                    "strength_state",
                    "defense",
                    "defense_id",
                ]

        if level == "game":
            if position == "f":
                merge_list = [
                    "season",
                    "game_id",
                    "game_date",
                    "session",
                    "team",
                    "opp_team",
                    "strength_state",
                    "forwards",
                    "forwards_id",
                ]

            if position == "d":
                merge_list = [
                    "season",
                    "game_id",
                    "game_date",
                    "session",
                    "team",
                    "opp_team",
                    "strength_state",
                    "defense",
                    "defense_id",
                ]

        if level == "period":
            if position == "f":
                merge_list = [
                    "season",
                    "game_id",
                    "game_date",
                    "session",
                    "team",
                    "opp_team",
                    "strength_state",
                    "forwards",
                    "forwards_id",
                    "game_period",
                ]

            if position == "d":
                merge_list = [
                    "season",
                    "game_id",
                    "game_date",
                    "session",
                    "team",
                    "opp_team",
                    "strength_state",
                    "defense",
                    "defense_id",
                    "game_period",
                ]

        if score is True:
            merge_list.append("score_state")

        if teammates is True:
            if position == "f":
                merge_list = merge_list + [
                    "defense",
                    "defense_id",
                    "own_goalie",
                    "own_goalie_id",
                ]

            if position == "d":
                merge_list = merge_list + [
                    "forwards",
                    "forwards_id",
                    "own_goalie",
                    "own_goalie_id",
                ]

        if opposition is True:
            merge_list = merge_list + [
                "opp_forwards",
                "opp_forwards_id",
                "opp_defense",
                "opp_defense_id",
                "opp_goalie",
                "opp_goalie_id",
            ]

            if "opp_team" not in merge_list:
                merge_list.insert(3, "opp_team")

        lines = lines_f.merge(
            lines_a, how="outer", on=merge_list, suffixes=("_x", "")
        ).fillna(0)

        lines.toi = (lines.toi_x + lines.toi) / 60

        lines = lines.drop(columns="toi_x")

        lines["ozf"] = lines.ozfw + lines.ozfl

        lines["nzf"] = lines.nzfw + lines.nzfl

        lines["dzf"] = lines.dzfw + lines.dzfl

        cols = [x for x in LineSchema.dtypes.keys() if x in lines.columns]

        lines = lines[cols]

        lines = lines.loc[lines.toi > 0].reset_index(drop=True).copy()

        lines = LineSchema.validate(lines)

        pbar_message = "Finished prepping lines data"

        progress.update(lines_task, description=pbar_message, advance=1, refresh=True)

    return lines

prep_team

prep_team(
    pbp,
    level="game",
    strengths=True,
    score=False,
    disable_progress_bar=False,
)

Prepares a team stats dataframe using Evolving Hockey data.

Aggregates to desired level. Capable of returning cuts that account for strength state, period, and score state. Returns a Pandas DataFrame.

Parameters:

Name Type Description Default
pbp DataFrame

Dataframe from the prep_pbp function with the default columns argument

required
level str

Level to aggregate stats, e.g., 'game'

'game'
strengths bool

Whether to aggregate to strength state level, e.g., True

True
score bool

Whether to aggregate to score state level

False
disable_progress_bar bool

Whether to disable progress bar

False

Returns:

Name Type Description
season int

8-digit season code, e.g., 20232024

session str

Regular season or playoffs, e.g., R

game_id int

10-digit game identifier, e.g., 2023020044

game_date str

Date of game in Eastern time-zone, e.g., 2023-10-17

team str

3-letter abbreviation of the team, e.g., NSH

opp_team str

3-letter abbreviation of the opposing team, e.g., EDM

strength_state str

Strength state from the perspective of the event team, e.g., 5v5

score_state str

Score state from the perspective of the event team, e.g., 1v6

game_period int

Game period, e.g., 3

toi float

Time on-ice in minutes, e.g., 18

gf float

Sum of goals scored, e.g., 0

gf_adj float

Sum of venue- and score-adjusted goals scored, e.g., 0

hdgf float

Sum of high-danger goals scored, e.g., 0

ga float

Sum of goals allowed, e.g., 0

ga_adj float

Sum of venue- and score-adjusted goals allowed, e.g., 0

hdga float

Sum of high-danger goals allowed, e.g., 0

xgf float

Sum of expected goals generated, e.g., 0.957070

xgf_adj float

Sum of venue- and score-adjusted expected goals generated, e.g., 0.883376

xga float

Sum of expected goals allowed, e.g., 0.535971

xga_adj float

Sum of venue- and score-adjusted expected goals allowed, e.g., 0.584744

sf float

Sum of shots taken, e.g., 10

sf_adj float

Sum of venue- and score-adjusted shots taken, e.g., 8.620

hdsf float

Sum of high-danger shots taken, e.g., 2

sa float

Sum of shots allowed, e.g., 4

sa_adj float

Sum of venue- and score-adjusted shots allowed, e.g., 4.764

hdsa float

Sum of high-danger shots allowed, e.g., 0

ff float

Sum of fenwick events generated, e.g., 14

ff_adj float

Sum of venue- and score-adjusted fenwick events generated, e.g., 12.026

hdff float

Sum of high-danger fenwick events generated, e.g., 2

fa float

Sum of fenwick events allowed, e.g., 8

fa_adj float

Sum of venue- and score-adjusted fenwick events allowed, e.g., 9.576

hdfa float

Sum of high-danger fenwick events allowed, e.g., 1

cf float

Sum of corsi events generated, e.g., 16

cf_adj float

Sum of venue- and score-adjusted corsi events generated, e.g., 13.488

ca float

Sum of corsi events allowed, e.g., 12.0

ca_adj float

Sum of venue- and score-adjusted corsi events allowed, e.g., 14.760

bsf float

Sum of shots taken that were ultimately blocked, e.g., 4

bsa float

Sum of shots allowed that were ultimately blocked, e.g., 2

msf float

Sum of shots taken that missed net, e.g., 4

hdmsf float

Sum of high-danger shots taken that missed net, e.g., 0

msa float

Sum of shots allowed that missed net, e.g., 4

hdmsa float

Sum of high-danger shots allowed that missed net, e.g., 1

ozf float

Sum of offensive zone faceoffs that occur, e.g., 6

nzf float

Sum of neutral zone faceoffs that occur, e.g., 4

dzf float

Sum of defensive zone faceoffs that occur, e.g., 6

fow float

Sum of faceoffs won, e.g., 8

fol float

Sum of faceoffs lost, e.g., 11

ozfw float

Sum of offensive zone faceoffs won, e.g., 3

ozfl float

Sum of offensive zone faceoffs lost, e.g., 1

nzfw float

Sum of neutral zone faceoffs won, e.g., 2

nzfl float

Sum of neutral zone faceoffs lost, e.g., 3

dzfw float

Sum of defensive zone faceoffs won, e.g., 3

dzfl float

Sum of defensive zone faceoffs lost, e.g., 7

hf float

Sum of hits dished out, e.g., 7

ht float

Sum of hits taken, e.g., 5

give float

Sum of giveaways, e.g., 5

take float

Sum of takeaways, e.g., 1

pent0 float

Sum of individual 0-minute penalties taken, e.g., 0

pent2 float

Sum of individual 2-minute penalties taken, e.g., 0

pent4 float

Sum of individual 4-minute penalties taken, e.g., 0

pent5 float

Sum of individual 5-minute penalties taken, e.g., 0

pent10 float

Sum of individual 10-minute penalties taken, e.g., 0

pend0 float

Sum of individual 0-minute penalties drawn, e.g., 0

pend2 float

Sum of individual 2-minute penalties drawn, e.g., 0

pend4 float

Sum of individual 4-minute penalties drawn, e.g., 0

pend5 float

Sum of individual 5-minute penalties drawn, e.g., 0

pend10 float

Sum of individual 10-minute penalties drawn, e.g., 0

Examples:

Basic play-by-play DataFrame

>>> shifts_raw = pd.read_csv("./raw_shifts.csv")
>>> pbp_raw = pd.read_csv("./raw_pbp.csv")
>>> pbp = prep_pbp(pbp_raw, shifts_raw)

Basic game-level stats for teams

>>> team = prep_team(pbp)

Period-level team stats, grouped by score state

>>> team = prep_team(pbp, level="period", score=True)
Source code in src/chickenstats/evolving_hockey/stats.py
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
def prep_team(
    pbp: pd.DataFrame,
    level: Literal["period", "game", "session", "season"] = "game",
    strengths: bool = True,
    score: bool = False,
    disable_progress_bar: bool = False,
) -> pd.DataFrame:
    """Prepares a team stats dataframe using Evolving Hockey data.

    Aggregates to desired level. Capable of returning cuts that account for strength state,
    period, and score state. Returns a Pandas DataFrame.

    Parameters:
        pbp (pd.DataFrame):
            Dataframe from the prep_pbp function with the default columns argument
        level (str):
            Level to aggregate stats, e.g., 'game'
        strengths (bool):
            Whether to aggregate to strength state level, e.g., True
        score (bool):
            Whether to aggregate to score state level
        disable_progress_bar (bool):
            Whether to disable progress bar

    Returns:
        season (int):
            8-digit season code, e.g., 20232024
        session (str):
            Regular season or playoffs, e.g., R
        game_id (int):
            10-digit game identifier, e.g., 2023020044
        game_date (str):
            Date of game in Eastern time-zone, e.g., 2023-10-17
        team (str):
            3-letter abbreviation of the team, e.g., NSH
        opp_team (str):
            3-letter abbreviation of the opposing team, e.g., EDM
        strength_state (str):
            Strength state from the perspective of the event team, e.g., 5v5
        score_state (str):
            Score state from the perspective of the event team, e.g., 1v6
        game_period (int):
            Game period, e.g., 3
        toi (float):
            Time on-ice in minutes, e.g., 18
        gf (float):
            Sum of goals scored, e.g., 0
        gf_adj (float):
            Sum of venue- and score-adjusted goals scored, e.g., 0
        hdgf (float):
            Sum of high-danger goals scored, e.g., 0
        ga (float):
            Sum of goals allowed, e.g., 0
        ga_adj (float):
            Sum of venue- and score-adjusted goals allowed, e.g., 0
        hdga (float):
            Sum of high-danger goals allowed, e.g., 0
        xgf (float):
            Sum of expected goals generated, e.g., 0.957070
        xgf_adj (float):
            Sum of venue- and score-adjusted expected goals generated, e.g., 0.883376
        xga (float):
            Sum of expected goals allowed, e.g., 0.535971
        xga_adj (float):
            Sum of venue- and score-adjusted expected goals allowed, e.g., 0.584744
        sf (float):
            Sum of shots taken, e.g., 10
        sf_adj (float):
            Sum of venue- and score-adjusted shots taken, e.g., 8.620
        hdsf (float):
            Sum of high-danger shots taken, e.g., 2
        sa (float):
            Sum of shots allowed, e.g., 4
        sa_adj (float):
            Sum of venue- and score-adjusted shots allowed, e.g., 4.764
        hdsa (float):
            Sum of high-danger shots allowed, e.g., 0
        ff (float):
            Sum of fenwick events generated, e.g., 14
        ff_adj (float):
            Sum of venue- and score-adjusted fenwick events generated, e.g., 12.026
        hdff (float):
            Sum of high-danger fenwick events generated, e.g., 2
        fa (float):
            Sum of fenwick events allowed, e.g., 8
        fa_adj (float):
            Sum of venue- and score-adjusted fenwick events allowed, e.g., 9.576
        hdfa (float):
            Sum of high-danger fenwick events allowed, e.g., 1
        cf (float):
            Sum of corsi events generated, e.g., 16
        cf_adj (float):
            Sum of venue- and score-adjusted corsi events generated, e.g., 13.488
        ca (float):
            Sum of corsi events allowed, e.g., 12.0
        ca_adj (float):
            Sum of venue- and score-adjusted corsi events allowed, e.g., 14.760
        bsf (float):
            Sum of shots taken that were ultimately blocked, e.g., 4
        bsa (float):
            Sum of shots allowed that were ultimately blocked, e.g., 2
        msf (float):
            Sum of shots taken that missed net, e.g., 4
        hdmsf (float):
            Sum of high-danger shots taken that missed net, e.g., 0
        msa (float):
            Sum of shots allowed that missed net, e.g., 4
        hdmsa (float):
            Sum of high-danger shots allowed that missed net, e.g., 1
        ozf (float):
            Sum of offensive zone faceoffs that occur, e.g., 6
        nzf (float):
            Sum of neutral zone faceoffs that occur, e.g., 4
        dzf (float):
            Sum of defensive zone faceoffs that occur, e.g., 6
        fow (float):
            Sum of faceoffs won, e.g., 8
        fol (float):
            Sum of faceoffs lost, e.g., 11
        ozfw (float):
            Sum of offensive zone faceoffs won, e.g., 3
        ozfl (float):
            Sum of offensive zone faceoffs lost, e.g., 1
        nzfw (float):
            Sum of neutral zone faceoffs won, e.g., 2
        nzfl (float):
            Sum of neutral zone faceoffs lost, e.g., 3
        dzfw (float):
            Sum of defensive zone faceoffs won, e.g., 3
        dzfl (float):
            Sum of defensive zone faceoffs lost, e.g., 7
        hf (float):
            Sum of hits dished out, e.g., 7
        ht (float):
            Sum of hits taken, e.g., 5
        give (float):
            Sum of giveaways, e.g., 5
        take (float):
            Sum of takeaways, e.g., 1
        pent0 (float):
            Sum of individual 0-minute penalties taken, e.g., 0
        pent2 (float):
            Sum of individual 2-minute penalties taken, e.g., 0
        pent4 (float):
            Sum of individual 4-minute penalties taken, e.g., 0
        pent5 (float):
            Sum of individual 5-minute penalties taken, e.g., 0
        pent10 (float):
            Sum of individual 10-minute penalties taken, e.g., 0
        pend0 (float):
            Sum of individual 0-minute penalties drawn, e.g., 0
        pend2 (float):
            Sum of individual 2-minute penalties drawn, e.g., 0
        pend4 (float):
            Sum of individual 4-minute penalties drawn, e.g., 0
        pend5 (float):
            Sum of individual 5-minute penalties drawn, e.g., 0
        pend10 (float):
            Sum of individual 10-minute penalties drawn, e.g., 0

    Examples:
        Basic play-by-play DataFrame
        >>> shifts_raw = pd.read_csv("./raw_shifts.csv")
        >>> pbp_raw = pd.read_csv("./raw_pbp.csv")
        >>> pbp = prep_pbp(pbp_raw, shifts_raw)

        Basic game-level stats for teams
        >>> team = prep_team(pbp)

        Period-level team stats, grouped by score state
        >>> team = prep_team(pbp, level="period", score=True)
    """
    with ChickenProgress(disable=disable_progress_bar) as progress:
        pbar_message = "Prepping team data..."

        team_task = progress.add_task(pbar_message, total=1)

        # Getting the "for" stats

        group_list = ["season", "session", "event_team"]

        if strengths is True:
            group_list.append("strength_state")

        if level == "game" or level == "period":
            group_list.insert(3, "opp_team")

            group_list[2:2] = ["game_id", "game_date"]

        if level == "period":
            group_list.append("game_period")

        if score is True:
            group_list.append("score_state")

        agg_stats = [
            "pred_goal",
            "pred_goal_adj",
            "shot",
            "shot_adj",
            "miss",
            "block",
            "corsi",
            "corsi_adj",
            "fenwick",
            "fenwick_adj",
            "goal",
            "goal_adj",
            "give",
            "take",
            "hd_goal",
            "hd_shot",
            "hd_fenwick",
            "hd_miss",
            "hit",
            "pen0",
            "pen2",
            "pen4",
            "pen5",
            "pen10",
            "fac",
            "ozf",
            "nzf",
            "dzf",
            "event_length",
        ]

        agg_dict = {x: "sum" for x in agg_stats if x in pbp.columns}

        new_cols = [
            "xgf",
            "xgf_adj",
            "sf",
            "sf_adj",
            "msf",
            "bsa",
            "cf",
            "cf_adj",
            "ff",
            "ff_adj",
            "gf",
            "gf_adj",
            "give",
            "take",
            "hdgf",
            "hdsf",
            "hdff",
            "hdmsf",
            "hf",
            "pent0",
            "pent2",
            "pent4",
            "pent5",
            "pent10",
            "fow",
            "ozfw",
            "nzfw",
            "dzfw",
            "toi",
        ]

        new_cols = dict(zip(agg_stats, new_cols))

        new_cols.update({"event_team": "team"})

        stats_for = (
            pbp.groupby(group_list, as_index=False)
            .agg(agg_dict)
            .rename(columns=new_cols)
        )

        # Getting the "against" stats

        group_list = ["season", "session", "opp_team"]

        if strengths is True:
            group_list.append("opp_strength_state")

        if level == "game" or level == "period":
            group_list.insert(3, "event_team")

            group_list[2:2] = ["game_id", "game_date"]

        if level == "period":
            group_list.append("game_period")

        if score is True:
            group_list.append("opp_score_state")

        agg_stats = [
            "pred_goal",
            "pred_goal_adj",
            "shot",
            "shot_adj",
            "miss",
            "block",
            "corsi",
            "corsi_adj",
            "fenwick",
            "fenwick_adj",
            "goal",
            "goal_adj",
            "hd_goal",
            "hd_shot",
            "hd_fenwick",
            "hd_miss",
            "hit",
            "pen0",
            "pen2",
            "pen4",
            "pen5",
            "pen10",
            "fac",
            "ozf",
            "nzf",
            "dzf",
            "event_length",
        ]

        agg_dict = {x: "sum" for x in agg_stats if x in pbp.columns}

        new_cols = [
            "xga",
            "xga_adj",
            "sa",
            "sa_adj",
            "msa",
            "bsf",
            "ca",
            "ca_adj",
            "fa",
            "fa_adj",
            "ga",
            "ga_adj",
            "hdga",
            "hdsa",
            "hdfa",
            "hdmsa",
            "ht",
            "pend0",
            "pend2",
            "pend4",
            "pend5",
            "pend10",
            "fol",
            "ozfl",
            "nzfl",
            "dzfl",
            "toi",
        ]

        new_cols = dict(zip(agg_stats, new_cols))

        new_cols.update(
            {
                "opp_team": "team",
                "opp_score_state": "score_state",
                "opp_strength_state": "strength_state",
                "event_team": "opp_team",
            }
        )

        stats_against = (
            pbp.groupby(group_list, as_index=False)
            .agg(agg_dict)
            .rename(columns=new_cols)
        )

        merge_list = [
            "season",
            "session",
            "game_id",
            "game_date",
            "team",
            "opp_team",
            "strength_state",
            "score_state",
            "game_period",
        ]

        merge_list = [
            x
            for x in merge_list
            if x in stats_for.columns and x in stats_against.columns
        ]

        team_stats = stats_for.merge(stats_against, on=merge_list, how="outer")

        team_stats["toi"] = (team_stats.toi_x + team_stats.toi_y) / 60

        team_stats = team_stats.drop(["toi_x", "toi_y"], axis=1)

        fos = ["ozf", "nzf", "dzf"]

        for fo in fos:
            team_stats[fo] = team_stats[f"{fo}w"] + team_stats[f"{fo}w"]

        team_stats = team_stats.dropna(subset="toi").reset_index(drop=True)

        cols = [x for x in TeamStatSchema.dtypes.keys() if x in team_stats.columns]

        team_stats = TeamStatSchema.validate(team_stats[cols])

        pbar_message = "Finished prepping team data"

        progress.update(team_task, description=pbar_message, advance=1, refresh=True)

    return team_stats