In [2]:
import pandas as pd

In [31]:
location = './motogp_2015_valencia_data.txt'
df = pd.read_csv(location, header=0, names=['Lap', 'RaceNumber','Time','Gap'], delimiter='\t', )

df.fillna(0.0, inplace=True)

df.info()
df.describe()
df.tail()


<class 'pandas.core.frame.DataFrame'>
Int64Index: 730 entries, 0 to 729
Data columns (total 4 columns):
Lap           730 non-null int64
RaceNumber    730 non-null int64
Time          730 non-null object
Gap           730 non-null object
dtypes: int64(2), object(2)
memory usage: 28.5+ KB
Out[31]:
Lap RaceNumber Time Gap
725 30 6 1'34.935 59.086
726 30 76 1'35.167 1'04.339
727 30 24 1'33.749 1'04.413
728 30 43 1'34.074 1'05.212
729 30 13 1'35.786 1'27.281

In [4]:
df['Time'].replace(r'\'',':',   regex=True, inplace=True)
df['Time'].replace(r'^', '00:', regex=True, inplace=True)
# Todo: Someone got lapped, gaps are inconsistently formatted
df['Gap'].replace(r'^(?!.\')', '0\'', regex=True, inplace=True)
df['Gap'].replace(r'\'',':',          regex=True, inplace=True)
df['Gap'].replace(r'^', '00:',        regex=True, inplace=True)

df['RaceNumber'] = df['RaceNumber'].astype(str)
df.head()
df.tail()


Out[4]:
Lap RaceNumber Time Gap
725 30 6 00:1:34.935 00:0:59.086
726 30 76 00:1:35.167 00:1:04.339
727 30 24 00:1:33.749 00:1:04.413
728 30 43 00:1:34.074 00:1:05.212
729 30 13 00:1:35.786 00:1:27.281

In [5]:
from pandas import to_timedelta

fixed_types = df.copy()

fixed_types['Time'] = to_timedelta(fixed_types['Time']).astype('timedelta64[ms]')
fixed_types['Time'] = [x / 1000.0 for x in fixed_types['Time']]
fixed_types['Gap'] = to_timedelta(fixed_types['Gap']).astype('timedelta64[ms]')
fixed_types['Gap'] = [x / 1000.0 for x in fixed_types['Gap']]
#fixed_types.describe()
#fixed_types.info
#fixed_types.dtypes
fixed_types.head()


Out[5]:
Lap RaceNumber Time Gap
0 1 99 97.122 0.000
1 1 93 97.318 0.196
2 1 26 97.473 0.351
3 1 29 97.907 0.785
4 1 4 98.267 1.145

In [7]:
pivot = fixed_types.pivot(index='Lap', columns='RaceNumber', values='Time')
pivot


Out[7]:
RaceNumber 13 19 23 24 25 26 29 35 38 4 ... 51 6 63 68 69 76 8 9 93 99
Lap
1 102.818 100.611 103.710 103.182 101.840 97.473 97.907 103.181 99.508 98.267 ... 100.126 99.907 102.618 100.900 100.848 101.531 102.865 99.274 97.318 97.122
2 94.106 93.407 94.359 94.580 93.098 91.590 91.491 93.836 92.633 92.041 ... 93.140 93.812 94.600 93.250 93.710 93.265 94.424 92.648 91.538 91.416
3 93.858 93.593 94.672 94.222 93.773 91.581 NaN 92.943 92.366 92.297 ... 92.884 93.301 94.016 93.632 93.670 93.648 94.063 92.385 91.491 91.367
4 94.190 93.386 94.706 94.097 93.264 91.705 NaN 92.956 92.915 92.653 ... 92.895 93.263 94.126 93.154 93.105 93.437 93.132 92.855 91.474 91.502
5 94.243 93.379 94.635 94.273 93.403 91.858 NaN 93.143 93.043 92.662 ... 92.957 93.273 93.820 92.776 93.351 93.890 92.944 93.160 91.455 91.542
6 94.271 93.343 94.713 94.310 93.079 91.559 NaN 92.728 93.099 93.076 ... 93.451 93.636 94.076 93.466 93.309 94.032 94.534 92.878 91.782 91.554
7 94.294 93.352 95.087 94.083 93.190 91.982 NaN 93.244 92.483 92.762 ... 93.162 93.747 93.792 93.116 93.527 93.955 94.062 93.651 91.586 91.483
8 94.107 93.625 95.131 93.759 93.332 91.931 NaN 93.120 92.537 93.018 ... 93.373 93.623 94.162 93.199 93.391 93.550 93.151 92.862 91.762 91.746
9 94.723 93.731 95.151 93.805 93.550 91.983 NaN 93.480 92.564 92.528 ... 93.272 93.533 93.977 93.006 94.219 93.530 93.469 92.937 91.691 91.700
10 93.980 94.090 95.017 94.068 93.143 91.759 NaN 93.139 92.566 92.706 ... 93.376 93.583 94.505 93.109 93.678 93.606 93.424 93.113 91.785 91.780
11 93.929 93.624 95.234 93.821 93.532 91.478 NaN 92.931 92.592 92.480 ... 93.240 93.986 94.448 93.104 93.551 93.572 93.580 93.056 91.716 91.853
12 93.966 93.312 95.027 93.652 92.869 91.731 NaN 92.773 92.851 92.877 ... 93.248 93.479 94.108 93.101 93.589 93.518 93.755 92.987 91.712 91.773
13 93.722 93.524 95.005 93.515 92.826 91.776 NaN 92.626 92.523 93.069 ... 93.224 93.620 93.620 93.239 93.525 93.440 93.095 93.000 91.788 91.802
14 93.919 93.732 94.971 93.757 92.700 91.672 NaN 92.646 92.423 92.738 ... 93.056 93.704 93.689 93.112 93.442 93.490 93.846 93.019 91.698 91.789
15 93.871 93.553 94.860 93.535 92.785 91.970 NaN 92.530 92.338 92.448 ... 93.085 93.517 93.567 93.505 93.685 93.513 93.370 93.022 91.733 91.738
16 94.144 93.605 94.609 93.537 92.850 91.934 NaN 92.531 92.866 92.762 ... 93.249 94.017 93.645 93.079 93.466 93.451 93.021 93.105 91.667 91.705
17 94.537 93.747 94.629 93.510 93.020 92.084 NaN 92.891 92.624 92.852 ... 93.165 93.785 93.595 93.290 93.936 93.754 93.196 93.146 91.627 91.558
18 94.780 93.557 94.792 93.436 93.562 91.840 NaN 92.535 92.682 92.968 ... 93.263 93.572 93.998 93.781 93.709 93.458 93.359 93.003 91.783 91.864
19 94.877 93.407 94.606 93.636 93.014 91.806 NaN 92.449 92.477 92.442 ... 93.408 93.573 94.158 93.944 93.456 93.422 93.323 93.268 91.681 91.571
20 95.166 93.657 94.790 93.879 92.719 91.974 NaN 92.350 92.569 92.631 ... 93.574 93.495 94.691 93.648 93.595 93.604 93.450 93.207 91.903 91.872
21 95.166 93.649 100.606 93.561 93.116 91.805 NaN 92.353 92.719 92.684 ... 93.472 93.651 94.460 93.653 93.665 93.558 93.622 93.136 91.893 92.035
22 95.045 93.663 NaN 93.507 92.911 91.809 NaN 92.713 92.626 92.635 ... 93.590 93.666 94.550 93.862 93.656 93.648 93.294 93.142 91.972 91.958
23 95.420 93.706 NaN 93.435 92.591 91.841 NaN 93.083 92.482 92.538 ... 93.357 93.646 94.680 93.716 93.808 94.258 93.519 93.630 92.058 92.073
24 95.004 93.554 NaN 93.565 92.531 91.507 NaN 92.240 92.720 92.619 ... 93.341 93.752 94.664 93.862 93.525 94.397 93.629 93.250 91.905 91.831
25 95.327 93.629 NaN 93.585 92.632 91.610 NaN 92.895 92.972 93.192 ... 93.562 93.533 NaN 93.744 93.694 94.529 93.424 93.186 91.781 91.865
26 95.331 93.582 NaN 93.774 92.689 91.898 NaN 93.491 93.002 93.482 ... 93.623 94.177 NaN 94.197 94.029 94.689 93.764 93.235 92.087 92.097
27 95.180 93.908 NaN 94.295 92.603 91.698 NaN 92.648 92.721 92.769 ... 93.514 94.031 NaN 94.306 93.999 94.579 93.918 93.227 92.025 92.111
28 95.377 93.729 NaN 94.055 92.381 91.693 NaN 92.416 92.886 92.671 ... 93.439 94.150 NaN 93.988 94.335 94.447 93.870 92.999 92.339 92.331
29 95.508 94.022 NaN 93.594 92.766 92.357 NaN 92.675 93.265 92.916 ... 93.503 94.483 NaN 94.265 94.348 94.765 93.574 93.097 92.432 92.282
30 95.786 94.333 NaN 93.749 93.341 92.114 NaN 92.742 93.147 93.467 ... 93.868 94.935 NaN 94.441 94.285 95.167 94.050 93.465 91.945 92.044

30 rows × 26 columns


In [8]:
leaders = pivot.loc[:,['99','93','26','46']]
leaders


Out[8]:
RaceNumber 99 93 26 46
Lap
1 97.122 97.318 97.473 100.982
2 91.416 91.538 91.590 92.782
3 91.367 91.491 91.581 91.997
4 91.502 91.474 91.705 91.820
5 91.542 91.455 91.858 93.021
6 91.554 91.782 91.559 92.724
7 91.483 91.586 91.982 92.078
8 91.746 91.762 91.931 92.306
9 91.700 91.691 91.983 92.362
10 91.780 91.785 91.759 92.506
11 91.853 91.716 91.478 92.455
12 91.773 91.712 91.731 92.580
13 91.802 91.788 91.776 92.111
14 91.789 91.698 91.672 92.108
15 91.738 91.733 91.970 92.184
16 91.705 91.667 91.934 92.132
17 91.558 91.627 92.084 92.184
18 91.864 91.783 91.840 92.171
19 91.571 91.681 91.806 92.237
20 91.872 91.903 91.974 92.213
21 92.035 91.893 91.805 92.155
22 91.958 91.972 91.809 92.238
23 92.073 92.058 91.841 92.311
24 91.831 91.905 91.507 92.483
25 91.865 91.781 91.610 92.288
26 92.097 92.087 91.898 92.549
27 92.111 92.025 91.698 92.440
28 92.331 92.339 91.693 92.490
29 92.282 92.432 92.357 92.453
30 92.044 91.945 92.114 92.793

In [25]:
from bokeh.charts import defaults
from bokeh.plotting import figure, output_notebook, show

# Set defaults for all charts
defaults.width = 800
defaults.height = 400

output_notebook()


BokehJS successfully loaded.

In [27]:
p = figure(plot_width=800, 
           plot_height=400, 
           x_axis_label='Lap', 
           y_axis_label='Sec',
           title= 'Lap time',
           x_range=(1, leaders.index.max()), 
          )

# add a line renderer with a NaN
p.line(leaders.index.values, leaders['99'], line_width=3, legend='JL99', line_color='blue')
p.line(leaders.index.values, leaders['93'], line_width=3, legend='MM93', line_color='red')
p.line(leaders.index.values, leaders['26'], line_width=3, legend='DP26', line_color='black')
p.line(leaders.index.values, leaders['46'], line_width=3, legend='VR46', line_color='green') #cant see yellow


show(p)



In [30]:
pivot_gap = fixed_types.pivot(index='Lap', columns='RaceNumber', values='Gap')
leaders_gap = pivot_gap.loc[:,['99','93','26','46']]

#want Y axis upside down
axis_to   = leaders_gap.loc[:,['26','46','93','99']].min().min()
axis_from = leaders_gap.loc[:,['26','46','93','99']].max().max()

p = figure(plot_width=800, 
           plot_height=400, 
           x_range=(1, leaders.index.max()),
           y_range=(axis_from, axis_to), 
           x_axis_label='Lap', 
           y_axis_label='Sec', 
           title = 'Gap to Leader'
          )

# add a line renderer with a NaN
p.line(leaders_gap.index.values, leaders_gap['99'], line_width=3, legend='JL99', line_color='blue')
p.line(leaders_gap.index.values, leaders_gap['93'], line_width=3, legend='MM93', line_color='red')
p.line(leaders_gap.index.values, leaders_gap['26'], line_width=3, legend='DP26', line_color='black')
p.line(leaders_gap.index.values, leaders_gap['46'], line_width=3, legend='VR46', line_color='green') #cant see yellow


show(p)



In [ ]:
pivot_gap

In [ ]:
leaders_gap

In [ ]:


In [ ]: