-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataframe_functions.py
354 lines (281 loc) · 13.7 KB
/
dataframe_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import pandas as pd
import numpy as np
def umpire_entry(umpire_info, key: str) -> pd.DataFrame:
'''
Returns a dataframe that contains information of umpires in the match
Parameters:
umpire_info: a list of umpires in the match
key: index of the match
Returns:
a dataframe of who umpired the match
'''
umpire_df = pd.DataFrame(columns=['key', 'umpire'])
if umpire_info == np.nan:
umpire_dict = {'key': key, 'umpire': np.nan}
umpire_df = pd.concat([umpire_df, pd.DataFrame([umpire_dict])], ignore_index=True)
else:
for umpire in umpire_info:
umpire_dict = {'key': key, 'umpire': umpire}
umpire_df = pd.concat([umpire_df, pd.DataFrame([umpire_dict])], ignore_index=True)
return umpire_df
def toss_entry(toss_info: dict, key: str) -> pd.DataFrame:
'''
Returns a dataframe that contains toss information of the match
Parameters:
toss_info: a dict of toss information
key: index of the match
Returns:
a dataframe of toss information of the match
'''
# possibility of adding the 'uncontested' variable in the future
toss_df = pd.DataFrame(columns=['key', 'decision', 'winner'])
toss_dict = {
'key': key,
'decision': toss_info['decision'],
'winner': toss_info['winner']}
toss_df = pd.concat([toss_df, pd.DataFrame([toss_dict])], ignore_index=True)
return toss_df
def team_entry(key: str, team_info: list) -> pd.DataFrame:
'''
Returns a dataframe that teams that participated in the match
Parameters:
team: a list of teams that participated in the match
key: index of the match
Returns:
a dataframe of teams that participated in the match
'''
team_df = pd.DataFrame(columns=['key', 'team'])
for team in team_info:
team_dict = {'key': key, 'team': team}
team_df = pd.concat([team_df, pd.DataFrame([team_dict])], ignore_index=True)
return team_df
def info_entry(key: str, gen_info: dict) -> pd.DataFrame:
'''
Returns a dataframe that contains general information about a match
Parameters:
gen_info: a dict of info from the main dict
key: index of the match
Returns:
a dataframe of general information of the match
'''
gen_df = pd.DataFrame(columns=['key', 'city', 'competition', 'date', 'gender',
'match_type', 'neutral_venue', 'overs', 'player_of_match', 'venue'])
gen_dict = {
'key': key,
'gender': gen_info['gender'],
'match_type': gen_info['match_type']}
conditional_cols = ['venue', 'city',
'competition', 'overs', 'neutral_venue']
for col in conditional_cols:
if col in gen_info.keys():
gen_dict[col] = gen_info[col]
gen_dict['date'] = gen_info['dates'][0]
if 'player_of_match' in gen_info.keys():
gen_dict['player_of_match'] = gen_info['player_of_match'][0]
gen_df = pd.concat([gen_df, pd.DataFrame([gen_dict])], ignore_index=True)
return gen_df
def innings_entry(key_id: str, innings_list: list) -> pd.DataFrame:
'''
Returns a dataframe that contains winner and margin information about a match
Parameters:
outcome_info: a dict of info from the main dict
key: index of the match
Returns:
a dataframe of winner and margin information of the match
'''
innings_df = pd.DataFrame(columns=['key', 'inning_no', 'batting_team', 'delivery_no', 'batter', 'bowler', 'non_striker',
'runs_batter', 'runs_extras', 'runs_non_boundary', 'runs_total', 'wicket_fielder', 'wicket_kind', 'wicket_player_out', 'extras_type'])
for inning in innings_list:
for key, value in inning.items():
if 'super over' in key.lower():
inning_no += 1
else:
inning_no = int(key[0])
batting_team = value['team']
for a_ball in value['deliveries']:
ball_data_dict = {}
delivery_no = list(a_ball.keys())[0]
ball_data_dict['key'] = key_id
ball_data_dict['inning_no'] = inning_no
ball_data_dict['batting_team'] = batting_team
ball_data_dict['delivery_no'] = delivery_no
# print(delivery_no, type(delivery_no))
# print(a_ball, type(a_ball))
delivery_data = a_ball[delivery_no]
ball_data_dict['non_striker'] = delivery_data['non_striker']
ball_data_dict['bowler'] = delivery_data['bowler']
runs_data = delivery_data['runs']
try:
ball_data_dict['batter'] = delivery_data['batsman']
ball_data_dict['runs_batter'] = runs_data['batsman']
except KeyError:
try:
ball_data_dict['batter'] = delivery_data['batter']
ball_data_dict['runs_batter'] = runs_data['batter']
print('reference is batter now')
except KeyError:
ball_data_dict['batter'] = np.nan
ball_data_dict['runs_batter'] = np.nan
print('Lookup reference for batter')
ball_data_dict['runs_extras'] = runs_data['extras']
ball_data_dict['runs_total'] = runs_data['total']
ball_data_dict['runs_non_boundary'] = runs_data['non_boundary'] if 'non_boundary' in runs_data.keys(
) else 0
del runs_data
if 'extras' in delivery_data.keys():
extras_data = delivery_data['extras']
ball_data_dict['extras_type'] = list(extras_data.keys())[0]
ball_data_dict['extras_run'] = extras_data[ball_data_dict['extras_type']]
del extras_data
else:
ball_data_dict['extras_type'] = np.nan
ball_data_dict['extras_run'] = np.nan
if 'wicket' in delivery_data.keys():
wicket_data = delivery_data['wicket']
if type(wicket_data) == list:
ball_data_dict['wicket_fielder'] = wicket_data[0]['fielders'][0] if 'fielders' in wicket_data[0].keys(
) else np.nan
ball_data_dict['wicket_kind'] = wicket_data[0]['kind']
ball_data_dict['wicket_player_out'] = wicket_data[0]['player_out']
innings_df = pd.concat(
[innings_df, pd.DataFrame([ball_data_dict])], ignore_index=True)
ball_data_dict['wicket_fielder'] = wicket_data[1]['fielders'][0] if 'fielders' in wicket_data[1].keys(
) else np.nan
ball_data_dict['wicket_kind'] = wicket_data[1]['kind']
ball_data_dict['wicket_player_out'] = wicket_data[1]['player_out']
else:
ball_data_dict['wicket_fielder'] = wicket_data['fielders'][0] if 'fielders' in wicket_data.keys(
) else np.nan
ball_data_dict['wicket_kind'] = wicket_data['kind']
ball_data_dict['wicket_player_out'] = wicket_data['player_out']
del wicket_data
else:
ball_data_dict['wicket_fielder'] = np.nan
ball_data_dict['wicket_kind'] = np.nan
ball_data_dict['wicket_player_out'] = np.nan
innings_df = pd.concat([innings_df, pd.DataFrame([ball_data_dict])], ignore_index=True)
del ball_data_dict
over_series = pd.Series(dtype='object')
ball_series = pd.Series(dtype='object')
# print(list(innings_df.inning_no.unique()))
for inn in list(innings_df.inning_no.unique()):
# print(inn)
temp_df = innings_df[innings_df.inning_no ==
inn][['delivery_no', 'extras_type']]
temp_df['new_ball'] = temp_df['delivery_no'].copy(deep=True)
temp_df['new_ball'] = temp_df['new_ball'].astype('str')
ball_split = temp_df['new_ball'].str.split('.', n=1, expand=True)
temp_df['over'] = ball_split[0]
temp_df['n_ball'] = ball_split[1]
overs = sorted(set(temp_df[(temp_df.extras_type == 'wides') | (
temp_df.extras_type == 'noballs')]['over']))
for over in overs:
temp = temp_df[temp_df.over == str(over)].copy(deep=True)
for ball in range(temp.shape[0]):
temp.iloc[ball, temp_df.columns.get_loc('n_ball')] = ball + 1
temp_df.loc[temp_df.delivery_no.isin(
temp.delivery_no), 'n_ball'] = temp.n_ball
for ball in range(temp.shape[0] - 1):
if temp.iloc[ball, temp_df.columns.get_loc('extras_type')] == 'wides' or temp.iloc[ball, temp_df.columns.get_loc('extras_type')] == 'noballs':
for a_ball in range(ball + 1, temp.shape[0]):
temp.iloc[a_ball, temp_df.columns.get_loc('n_ball')] = str(
int(temp.iloc[a_ball, temp_df.columns.get_loc('n_ball')]) - 1)
temp_df.loc[temp_df.delivery_no.isin(
temp.delivery_no), 'n_ball'] = temp.n_ball
del overs
# print(temp_df)
over_series = pd.concat([over_series, temp_df.over], ignore_index=True)
ball_series = pd.concat([ball_series, temp_df.n_ball], ignore_index=True)
del temp_df
innings_df['over'] = over_series
innings_df['ball'] = ball_series
# innings_df = innings_df.drop(columns=['ball'])
# print(innings_df)
# print(over_series, type(over_series))
# print(ball_series, type(ball_series))
del over_series
del ball_series
innings_df['over'] = innings_df['over'].astype('float').astype('Int64')
innings_df['ball'] = innings_df['ball'].astype('float').astype('Int64')
innings_df['inning_no'] = innings_df['inning_no'].astype('Int64')
innings_df['runs_batter'] = innings_df['runs_batter'].astype('Int64')
innings_df['runs_extras'] = innings_df['runs_extras'].astype('Int64')
innings_df['runs_total'] = innings_df['runs_total'].astype('Int64')
return innings_df
def outcome_entry(key: str, outcome_info: dict) -> pd.DataFrame:
'''
Returns a dataframe that contains winner and margin information about a match
Parameters:
outcome_info: a dict of info from the main dict
key: index of the match
Returns:
a dataframe of winner and margin information of the match
'''
outcome_df = pd.DataFrame(columns=[
'key', 'by_innings', 'by_type', 'by_margin', 'eliminator', 'bowl_out', 'method', 'result', 'winner'])
outcome_dict = {'key': key}
if 'by' in outcome_info.keys():
if 'runs' in outcome_info['by'].keys():
outcome_dict['by_type'] = 'runs'
outcome_dict['by_margin'] = outcome_info['by']['runs']
if 'wickets' in outcome_info['by'].keys():
outcome_dict['by_type'] = 'wickets'
outcome_dict['by_margin'] = outcome_info['by']['wickets']
outcome_dict['by_innings'] = outcome_info['by']['innings'] if 'innings' in outcome_info['by'].keys(
) else np.nan
else:
outcome_dict['by_type'] = np.nan
outcome_dict['by_margin'] = np.nan
uncertain_columns = ['winner', 'eliminator',
'result', 'method', 'bowl_out']
for a_col in uncertain_columns:
outcome_dict[a_col] = outcome_info[a_col] if a_col in outcome_info.keys(
) else np.nan
outcome_df = pd.concat([outcome_df, pd.DataFrame([outcome_dict])], ignore_index=True)
return outcome_df
def date_entry(key: str, dates: list) -> pd.DataFrame:
dates_df = pd.DataFrame(columns=['key', 'date'])
for date in dates:
date_dict = dict()
date_dict['key'] = key
date_dict['date'] = date
dates_df = pd.concat([dates_df, pd.DataFrame([date_dict])], ignore_index=True)
return dates_df
def pom_entry(key: str, pom_list: list) -> pd.DataFrame:
pom_df = pd.DataFrame(columns=['key', 'player_of_match'])
for pom in pom_list:
pom_dict = dict()
pom_dict['key'] = key
pom_dict['player_of_match'] = pom
pom_df = pd.concat([pom_df, pd.DataFrame([pom_dict])], ignore_index=True)
return pom_df
# super sub
def supersub_entry(key: str, supersub_info: dict) -> pd.DataFrame:
supersub_dict = dict()
supersub_df = pd.DataFrame(columns=['key', 'team', 'player'])
for team in supersub_info:
supersub_dict['key'] = key
supersub_dict['team'] = team
supersub_dict['player'] = supersub_info[team]
supersub_df = pd.concat([supersub_df, pd.DataFrame([supersub_dict])], ignore_index=True)
return supersub_df
# bowl_out
def bowl_out_entry(key: str, bowl_out_info: dict) -> pd.DataFrame:
bo_df = pd.DataFrame(columns=['key', 'bowler', 'outcome'])
for bo_dict in bowl_out_info:
bo_dict['key'] = key
bo_df = pd.concat([bo_df, pd.DataFrame([bo_dict])], ignore_index=True)
return bo_df
def player_entry(key: str, players: dict, registry: dict) -> pd.DataFrame:
player_df = pd.DataFrame(columns=['key', 'team', 'player', 'reg_no'])
player_dict = dict()
for team in players:
for player in players[team]:
player_dict = dict()
player_dict['key'] = key
player_dict['team'] = team
player_dict['player'] = player
player_dict['reg_no'] = registry['people'][player]
player_df = pd.concat([player_df, pd.DataFrame([player_dict])], ignore_index=True)
player_dict = None
return player_df