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()
Out[31]:
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]:
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]:
In [7]:
pivot = fixed_types.pivot(index='Lap', columns='RaceNumber', values='Time')
pivot
Out[7]:
In [8]:
leaders = pivot.loc[:,['99','93','26','46']]
leaders
Out[8]:
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()
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 [ ]: