sportsref icon indicating copy to clipboard operation
sportsref copied to clipboard

Getting Play by Play Data

Open theSanchize3 opened this issue 9 years ago • 4 comments

I am interested in trying to predict offensive playcalls for the NFL. Is there a way to get play by play data with info about what type of play was called (run left, run right, run middle, deep pass, short pass, etc). Even just knowing if the play was a run or a pass is enough. If so, can you suggest a script that would do so?

Thanks, Sanchez

theSanchize3 avatar Dec 17 '16 00:12 theSanchize3

It is possible, in fact I have done a very similar project already! You can read about it here. To get play by play data, check out the nfl.BoxScore.pbp function, used as so:

from sportsref import nfl
bs = nfl.BoxScore('201602070den')
df = bs.pbp()
# df is a DataFrame of play-by-play data, where each row is a play

It's not perfect, but it will have the main categories. For what purposes are you interested in predicting playcalls?

mdgoldberg avatar Dec 19 '16 07:12 mdgoldberg

Thanks. That article is very similar what I was planning on doing. When I ran the code you suggested, though, I got this error:

----> 1 from sportsref import nfl
      2 bs = nfl.BoxScore('201602070den')
      3 df = bs.pbp()
      4 # df is a DataFrame of play-by-play data, where each row is a play

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\__init__.py in <module>()
     10 import decorators
     11 import utils
---> 12 import nfl
     13 import nba
     14 

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\nfl\__init__.py in <module>()
----> 1 import finders
      2 import teams
      3 import players
      4 import boxscores
      5 import winProb

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\nfl\finders\__init__.py in <module>()
     13 # Fill in PlayerSeasonFinder docstring
     14 
---> 15 IOD = PSF.inputs_options_defaults()
     16 
     17 paramStr = '\n'.join(

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\decorators.pyc in wrapper(*args, **kwargs)
     27             orig_cwd = os.getcwd()
     28             os.chdir(dirPath)
---> 29             ret = func(*args, **kwargs)
     30             os.chdir(orig_cwd)
     31             return ret

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\nfl\finders\PSF.pyc in inputs_options_defaults()
    168         print 'Regenerating PSFConstants file'
    169 
--> 170         html = utils.get_html(PSF_URL)
    171         doc = pq(html)
    172 

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\decorators.pyc in wrapper(*args, **kwargs)
    197             return ret
    198         except KeyError:
--> 199             cache[key] = fun(*args, **kwargs)
    200             ret = _copy(cache[key])
    201             return ret

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\decorators.pyc in wrapper(url)
    147         # otherwise, download html and cache it
    148         else:
--> 149             text = func(url)
    150             with open(filename, 'w+') as f:
    151                 f.write(text.encode('ascii', 'replace'))

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\utils.pyc in get_html(url)
     23     start = time.time()
     24     browser = webdriver.PhantomJS(service_args=['--load-images=false'],
---> 25                                   service_log_path='/dev/null')
     26     browser.set_window_size(10000, 10000)
     27     browser.get(url)

C:\Users\Jacob\Anaconda2\lib\site-packages\selenium\webdriver\phantomjs\webdriver.pyc in __init__(self, executable_path, port, desired_capabilities, service_args, service_log_path)
     49             port=port,
     50             service_args=service_args,
---> 51             log_path=service_log_path)
     52         self.service.start()
     53 

C:\Users\Jacob\Anaconda2\lib\site-packages\selenium\webdriver\phantomjs\service.pyc in __init__(self, executable_path, port, service_args, log_path)
     48             self._cookie_temp_file = None
     49 
---> 50         service.Service.__init__(self, executable_path, port=port, log_file=open(log_path, 'w'))
     51 
     52     def _args_contain(self, arg):

IOError: [Errno 2] No such file or directory: '/dev/null'

Do you how to fix this error?

theSanchize3 avatar Dec 20 '16 01:12 theSanchize3

Out of curiosity, what are you planning on using my package for? Also, that bug should be fixed.

On Dec 19, 2016, 8:15 PM -0500, theSanchize3 [email protected], wrote:

Thanks. That article is very similar what I was planning on doing. When I ran the code you suggested, though, I got this error:

----> 1 from sportsref import nfl 2 bs = nfl.BoxScore('201602070den') 3 df = bs.pbp() 4 # df is a DataFrame of play-by-play data, where each row is a play

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref_init_.py in () 10 import decorators 11 import utils ---> 12 import nfl 13 import nba 14

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\nfl_init_.py in () ----> 1 import finders 2 import teams 3 import players 4 import boxscores 5 import winProb

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\nfl\finders_init_.py in () 13 # Fill in PlayerSeasonFinder docstring 14 ---> 15 IOD = PSF.inputs_options_defaults() 16 17 paramStr = '\n'.join(

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\decorators.pyc in wrapper(*args, **kwargs) 27 orig_cwd = os.getcwd() 28 os.chdir(dirPath) ---> 29 ret = func(*args, **kwargs) 30 os.chdir(orig_cwd) 31 return ret

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\nfl\finders\PSF.pyc in inputs_options_defaults() 168 print 'Regenerating PSFConstants file' 169 --> 170 html = utils.get_html(PSF_URL) 171 doc = pq(html) 172

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\decorators.pyc in wrapper(*args, **kwargs) 197 return ret 198 except KeyError: --> 199 cache[key] = fun(*args, **kwargs) 200 ret = _copy(cache[key]) 201 return ret

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\decorators.pyc in wrapper(url) 147 # otherwise, download html and cache it 148 else: --> 149 text = func(url) 150 with open(filename, 'w+') as f: 151 f.write(text.encode('ascii', 'replace'))

C:\Users\Jacob\Anaconda2\lib\site-packages\sportsref\utils.pyc in get_html(url) 23 start = time.time() 24 browser = webdriver.PhantomJS(service_args=['--load-images=false'], ---> 25 service_log_path='/dev/null') 26 browser.set_window_size(10000, 10000) 27 browser.get(url)

C:\Users\Jacob\Anaconda2\lib\site-packages\selenium\webdriver\phantomjs\webdriver.pyc in init(self, executable_path, port, desired_capabilities, service_args, service_log_path) 49 port=port, 50 service_args=service_args, ---> 51 log_path=service_log_path) 52 self.service.start() 53

C:\Users\Jacob\Anaconda2\lib\site-packages\selenium\webdriver\phantomjs\service.pyc in init(self, executable_path, port, service_args, log_path) 48 self._cookie_temp_file = None 49 ---> 50 service.Service.init(self, executable_path, port=port, log_file=open(log_path, 'w')) 51 52 def _args_contain(self, arg):

IOError: [Errno 2] No such file or directory: '/dev/null'

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

mdgoldberg avatar Dec 21 '16 04:12 mdgoldberg

I waht to see if I can determine which coaches are the most predictable, and if i can do that, determine if predictability affects success in any way. And thank you, I'll try it again when I get a chance.

theSanchize3 avatar Dec 21 '16 16:12 theSanchize3