{"cells":[{"metadata":{"_uuid":"3751a3059274d4910d105367a1e347bf8bdf2d3b"},"cell_type":"markdown","source":"### It will be useful to know how long it takes (in seconds) from the time the punter kicks the football to the time it is received."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#import packages\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport plotly.plotly as py\nimport plotly.graph_objs as go\nfrom plotly import tools, offline\n%matplotlib inline ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#Read in data\n#Only reading in 1 week to save time/space\nNGS_df = pd.read_csv('../input/NGS-2017-reg-wk1-6.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5665d9a3c7866afe8267e5ef53f498e14258cf2b"},"cell_type":"markdown","source":"#### Creating a function to calculate the hang time"},{"metadata":{"trusted":true,"_uuid":"b5baf2e53c570f7431efd19c0e2e933adf627bf3"},"cell_type":"code","source":"def get_hang_time(ngs_df, start_event='punt', *stop_events):\n    punt_event = ngs_df.loc[ngs_df.Event==start_event] \\\n        .groupby(['Season_Year', 'GameKey','PlayID'], as_index = False)['Time'].min()\n    punt_event.rename(columns = {'Time':'punt_time'}, inplace=True)\n    punt_event['punt_time'] = pd.to_datetime(punt_event['punt_time'],\\\n                                             format='%Y-%m-%d %H:%M:%S.%f')\n    \n    receiving_event = ngs_df.loc[ngs_df.Event.isin(stop_events)] \\\n        .groupby(['Season_Year', 'GameKey','PlayID'], as_index = False)['Time'].min()\n    receiving_event.rename(columns = {'Time':'receiving_time'}, inplace=True)\n    receiving_event['receiving_time'] = pd.to_datetime(receiving_event['receiving_time'],\\\n                                             format='%Y-%m-%d %H:%M:%S.%f')\n    \n    punt_df = punt_event.merge(receiving_event, how='inner', on = ['Season_Year','GameKey','PlayID']) \\\n                .reset_index(drop=True)\n    \n    punt_df['hang_time'] = (punt_df['receiving_time'] - punt_df['punt_time']).dt.total_seconds()\n    \n    return punt_df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"00ed12edf637549f2f24f4c329f521b1355c50f2"},"cell_type":"markdown","source":"#### Call function only looking at plays that had the events with 'punt_received' or 'fair_catch'"},{"metadata":{"trusted":true,"_uuid":"5e0f8b0da84d74a5931bde189c0cbe89447fcfc7"},"cell_type":"code","source":"punt_df = get_hang_time(NGS_df, 'punt', 'punt_received', 'fair_catch')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"f89e11df847bc1e4eabcafb7076df04427e113c3"},"cell_type":"code","source":"#Show histogram of the hang_time column\npunt_df.hang_time.hist();","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"70a776422ecb58ff41f9826c894bbb99b4dbdcdb"},"cell_type":"markdown","source":"#### Some general statistics for hang times"},{"metadata":{"trusted":true,"_uuid":"6c6c4fccaf39f5789b7e46a3b04ea3a2c3f7a724"},"cell_type":"code","source":"print('The average hang time of a punt is {} seconds' .format(round(punt_df['hang_time'].mean(), 1)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a3c1e8d2b7d8f16f889d81192f886b4ccd01c8f"},"cell_type":"code","source":"print('The median hang time of a punt is {} seconds' .format(round(punt_df['hang_time'].median(), 1)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4adc1b47c8a0f94033b9b4d4e750fd2600ca42da"},"cell_type":"code","source":"print(str(round(len(punt_df.loc[punt_df.hang_time < 5.5]) / len(punt_df) * 100, 1)) \\\n    + '% of hang times are less than 5 1/2 seconds')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40f0bd3a5966afbc118c95e13d9b093fb79b4708"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}