In [16]:
import collections
import datetime
import itertools as it
import math
import numpy as np
import pandas as pd
import cric
In [17]:
summaries = cric.match_summaries_from_dir('../data/t20s')
matches_df = cric.match.create_match_summaries_frame(it.islice(summaries, 200))
Read file 200 of 730
In [18]:
innings = cric.match.pic.pick_innings(it.islice(summaries, 1).next())
inn1_data = innings[0].items()[0][1]
inn2_data = innings[1].items()[0][1]
Read file 201 of 730
In [19]:
ifr = cric.inning.create_innings_dataframe(1, inn1_data)
ifr.fillna('-').head(18)
Out[19]:
batsman
bowler
non_striker
rb
re
rt
rb_cum
re_cum
rt_cum
wf
wk
wpo
w_cum
inning
over
ball
1
0.0
0.1
BB McCullum
SW Tait
PJ Ingram
0
4
4
0
4
4
-
-
-
0
0.2
BB McCullum
SW Tait
PJ Ingram
0
0
0
0
4
4
-
-
-
0
0.3
BB McCullum
SW Tait
PJ Ingram
4
0
4
4
4
8
-
-
-
0
0.4
BB McCullum
SW Tait
PJ Ingram
1
0
1
5
4
9
-
-
-
0
0.5
PJ Ingram
SW Tait
BB McCullum
0
1
1
0
5
10
-
-
-
0
0.6
PJ Ingram
SW Tait
BB McCullum
0
0
0
0
5
10
-
-
-
0
0.7
PJ Ingram
SW Tait
BB McCullum
0
0
0
0
5
10
-
bowled
PJ Ingram
1
1.0
0.1
BB McCullum
DP Nannes
MJ Guptill
6
0
6
11
5
16
-
-
-
1
0.2
BB McCullum
DP Nannes
MJ Guptill
3
0
3
14
5
19
-
-
-
1
0.3
MJ Guptill
DP Nannes
BB McCullum
1
0
1
1
5
20
-
-
-
1
0.4
BB McCullum
DP Nannes
MJ Guptill
0
0
0
14
5
20
-
-
-
1
0.5
BB McCullum
DP Nannes
MJ Guptill
4
0
4
18
5
24
-
-
-
1
0.6
BB McCullum
DP Nannes
MJ Guptill
0
0
0
18
5
24
-
-
-
1
2.0
0.1
MJ Guptill
SW Tait
BB McCullum
0
0
0
1
5
24
-
-
-
1
0.2
MJ Guptill
SW Tait
BB McCullum
0
0
0
1
5
24
-
-
-
1
0.3
MJ Guptill
SW Tait
BB McCullum
0
0
0
1
5
24
-
-
-
1
0.4
MJ Guptill
SW Tait
BB McCullum
0
0
0
1
5
24
-
-
-
1
0.5
MJ Guptill
SW Tait
BB McCullum
4
0
4
5
5
28
-
-
-
1
In [22]:
matches_df = matches_df.sort_index(level='date')
matches_df = matches_df[matches_df.gender == 'male']
#print matches_df.columns[:1]
#matches_df.xs('India', level='team.home', drop_level=False).head().T
In [23]:
idx, match = it.islice(matches_df.iterrows(), 1).next()
date, home_team, away_team = idx
first_match = cric.match.flat_match(date, home_team, away_team, match)
all_matches = pd.concat((cric.match.flat_match(*idx, match_s=match) for (idx, match) in matches_df.iterrows()), axis=0)
all_matches.columns
Out[23]:
Index([u'1st innings', u'batsman', u'bowler', u'city', u'match_type',
u'neutral_venue', u'non_striker', u'overs', u'rb', u'rb_cum', u're',
u're_cum', u'replacements', u'rt', u'rt_cum', u'supersubs',
u'toss.decision', u'toss.winner', u'umpires', u'venue', u'w_cum', u'wf',
u'wk', u'wpo'],
dtype='object')
In [24]:
all_matches.fillna('-').tail(20).T
Out[24]:
date
2017-06-05
team.home
West Indies
team.away
Afghanistan
inning
2
over
16.0
17.0
18.0
19.0
ball
0.1
0.2
0.3
0.4
0.5
0.6
0.1
0.2
0.3
0.4
0.5
0.6
0.1
0.2
0.3
0.4
0.5
0.6
0.1
0.2
1st innings
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
Afghanistan 146/6
batsman
JN Mohammed
MN Samuels
JN Mohammed
MN Samuels
MN Samuels
JN Mohammed
MN Samuels
MN Samuels
JN Mohammed
JN Mohammed
MN Samuels
MN Samuels
MN Samuels
JN Mohammed
JN Mohammed
JN Mohammed
MN Samuels
JN Mohammed
JN Mohammed
MN Samuels
bowler
Rashid Khan
Rashid Khan
Rashid Khan
Rashid Khan
Rashid Khan
Rashid Khan
Karim Janat
Karim Janat
Karim Janat
Karim Janat
Karim Janat
Karim Janat
Rashid Khan
Rashid Khan
Rashid Khan
Rashid Khan
Rashid Khan
Rashid Khan
Shapoor Zadran
Shapoor Zadran
city
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
St Kitts
match_type
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
T20
neutral_venue
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
non_striker
MN Samuels
JN Mohammed
MN Samuels
JN Mohammed
JN Mohammed
MN Samuels
JN Mohammed
JN Mohammed
MN Samuels
MN Samuels
JN Mohammed
JN Mohammed
JN Mohammed
MN Samuels
MN Samuels
MN Samuels
JN Mohammed
MN Samuels
MN Samuels
JN Mohammed
overs
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
rb
1
1
1
0
1
0
0
1
6
1
6
1
1
0
2
1
1
1
1
1
rb_cum
10
77
11
77
78
11
78
79
17
18
85
86
87
18
20
21
88
22
23
89
re
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
re_cum
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
replacements
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
rt
1
1
1
0
1
0
0
1
6
1
6
1
1
0
2
1
1
1
1
1
rt_cum
121
122
123
123
124
124
124
125
131
132
138
139
140
140
142
143
144
145
146
147
supersubs
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
toss.decision
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
toss.winner
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
Afghanistan
umpires
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
[LS Reifer, JS Wilson]
venue
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
Warner Park, Basseterre
w_cum
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
wf
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
wk
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
wpo
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
In [26]:
# compute the total runs scored by a batsman over a window of the last 12 balls faced by the batsman.
all_matches.groupby(['batsman'])['rb'].rolling(12, min_periods=2).sum().sort_index(level=['date', 'inning', 'over', 'ball'])
# compute the strike rate of a batsman over a window of the last 6 balls faced against a bowler.
all_matches.groupby(['batsman', 'bowler'])['rb'].rolling(6, min_periods=2).mean().sort_index(level=['date', 'inning', 'over', 'ball'])
Out[26]:
batsman bowler date team.home team.away inning over ball
AC Gilchrist DR Tuffey 2005-02-17 New Zealand Australia 1 0.0 0.1 NaN
0.2 0.000000
MJ Clarke DR Tuffey 2005-02-17 New Zealand Australia 1 0.0 0.3 NaN
0.4 0.500000
AC Gilchrist DR Tuffey 2005-02-17 New Zealand Australia 1 0.0 0.5 0.333333
MJ Clarke DR Tuffey 2005-02-17 New Zealand Australia 1 0.0 0.6 2.333333
0.7 1.750000
A Symonds KD Mills 2005-02-17 New Zealand Australia 1 1.0 0.1 NaN
0.2 2.000000
0.3 3.333333
0.4 2.750000
AC Gilchrist KD Mills 2005-02-17 New Zealand Australia 1 1.0 0.5 NaN
RT Ponting KD Mills 2005-02-17 New Zealand Australia 1 1.0 0.6 NaN
DR Tuffey 2005-02-17 New Zealand Australia 1 2.0 0.1 NaN
0.2 0.000000
0.3 0.000000
A Symonds DR Tuffey 2005-02-17 New Zealand Australia 1 2.0 0.4 NaN
0.5 1.000000
0.6 1.000000
KD Mills 2005-02-17 New Zealand Australia 1 3.0 0.1 3.000000
0.2 2.500000
0.3 3.500000
0.4 3.500000
0.5 3.166667
0.6 3.000000
0.7 2.333333
0.8 2.333333
RT Ponting DR Tuffey 2005-02-17 New Zealand Australia 1 4.0 0.1 1.000000
0.2 1.000000
DR Martyn DR Tuffey 2005-02-17 New Zealand Australia 1 4.0 0.3 NaN
...
MN Samuels Shapoor Zadran 2017-06-05 West Indies Afghanistan 2 14.0 0.3 1.333333
JN Mohammed Shapoor Zadran 2017-06-05 West Indies Afghanistan 2 14.0 0.4 1.000000
MN Samuels Shapoor Zadran 2017-06-05 West Indies Afghanistan 2 14.0 0.5 1.500000
JN Mohammed Shapoor Zadran 2017-06-05 West Indies Afghanistan 2 14.0 0.6 1.500000
MN Samuels Mohammad Nabi 2017-06-05 West Indies Afghanistan 2 15.0 0.1 1.333333
0.2 2.166667
0.3 2.500000
0.4 2.500000
0.5 2.666667
JN Mohammed Mohammad Nabi 2017-06-05 West Indies Afghanistan 2 15.0 0.6 0.666667
Rashid Khan 2017-06-05 West Indies Afghanistan 2 16.0 0.1 0.500000
MN Samuels Rashid Khan 2017-06-05 West Indies Afghanistan 2 16.0 0.2 0.500000
JN Mohammed Rashid Khan 2017-06-05 West Indies Afghanistan 2 16.0 0.3 0.666667
MN Samuels Rashid Khan 2017-06-05 West Indies Afghanistan 2 16.0 0.4 0.500000
0.5 0.500000
JN Mohammed Rashid Khan 2017-06-05 West Indies Afghanistan 2 16.0 0.6 0.666667
MN Samuels Karim Janat 2017-06-05 West Indies Afghanistan 2 17.0 0.1 1.166667
0.2 1.166667
JN Mohammed Karim Janat 2017-06-05 West Indies Afghanistan 2 17.0 0.3 2.666667
0.4 2.250000
MN Samuels Karim Janat 2017-06-05 West Indies Afghanistan 2 17.0 0.5 2.166667
0.6 2.166667
Rashid Khan 2017-06-05 West Indies Afghanistan 2 18.0 0.1 0.666667
JN Mohammed Rashid Khan 2017-06-05 West Indies Afghanistan 2 18.0 0.2 0.666667
0.3 0.833333
0.4 0.833333
MN Samuels Rashid Khan 2017-06-05 West Indies Afghanistan 2 18.0 0.5 0.833333
JN Mohammed Rashid Khan 2017-06-05 West Indies Afghanistan 2 18.0 0.6 0.833333
Shapoor Zadran 2017-06-05 West Indies Afghanistan 2 19.0 0.1 1.666667
MN Samuels Shapoor Zadran 2017-06-05 West Indies Afghanistan 2 19.0 0.2 1.666667
Name: rb, Length: 42825, dtype: float64
Content source: kochhar/cric
Similar notebooks: