-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_timeouts.py
More file actions
70 lines (57 loc) · 2.78 KB
/
Copy pathget_timeouts.py
File metadata and controls
70 lines (57 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pandas as pd
df = pd.read_csv("data/New_Play_by_Play_data_2.csv")
timeout_df = pd.DataFrame(columns=["Game_id", "Period", "Team_id", "Time_remaining", "Timeouts_remaining", "Season", "Effectiveness", "index"])
num_timeouts_regulation = 7
num_timeouts_OT = 2
num_timeouts_in_last_3_mins = 2
num_timeouts_in_last_2_mins = 3
df = df[df["Event_Msg_Type"] == 9]
timeouts_left = {}
temp = []
for index in range(df.shape[0]):
row = df.iloc[index]
period = row["Period"]
game_id = row["Game_id"]
team_id = row["Team_id"]
pc_time = row["PC_Time"]
event_msg_type = row["Event_Msg_Type"]
index1 = row["index"]
season = int(str(game_id)[1:3]) + 2000
if team_id == 0:
if game_id == 21000640:
team_id = 1610612742
elif game_id == 21100652:
team_id = 1610612747
elif game_id == 21200203:
team_id = 1610612740
if event_msg_type == 9:
if season < 2017:
num_timeouts_regulation = 9
else:
num_timeouts_regulation = 7
if game_id not in timeouts_left:
timeouts_left[game_id] = {}
if period > 4:
timeouts_left[game_id][team_id] = timeouts_left[game_id].get(team_id, num_timeouts_OT) - 1
period = 4
else:
if season < 2017:
if period == 4 and pc_time <= 1200 and timeouts_left[game_id].get(team_id, num_timeouts_regulation) > num_timeouts_in_last_2_mins:
# If the game is in the last 2 minutes, you can only take 3 timeouts
timeouts_left[game_id][team_id] = num_timeouts_in_last_2_mins - 1
else:
timeouts_left[game_id][team_id] = timeouts_left[game_id].get(team_id, num_timeouts_regulation) - 1
else:
if period == 4 and pc_time <= 1800 and timeouts_left[game_id].get(team_id, num_timeouts_regulation) > num_timeouts_in_last_3_mins:
# If the game is in the last 3 minutes, you can only take 2 timeouts
timeouts_left[game_id][team_id] = num_timeouts_in_last_3_mins - 1
else:
timeouts_left[game_id][team_id] = timeouts_left[game_id].get(team_id, num_timeouts_regulation) - 1
if timeouts_left[game_id][team_id] < 0:
timeouts_left[game_id][team_id] = 0
time_remaining = (pc_time + (4 - period) * 7200)/10
temp.append([game_id, period, team_id, time_remaining, timeouts_left[game_id][team_id], season, -1,index1])
print("timeouts:", index)
print(index)
timeout_df = timeout_df.append(pd.DataFrame(temp, columns=["Game_id", "Period", "Team_id", "Time_remaining", "Timeouts_remaining", "Season", "Effectiveness","index"]))
timeout_df.to_csv("data/Timeouts-1.csv", index=False)