-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgameData.py
More file actions
77 lines (50 loc) · 2.24 KB
/
gameData.py
File metadata and controls
77 lines (50 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
## Ben Kite
## 2017-02-16
"""
Finds all game played in the year that you specify and saves
their results in a single .csv file.
The output file will be named with the year followed by "Games.csv".
"""
import pandas, os, argparse
from baseballReferenceScrape import pullGameData
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--year", help="Year of games to be collected. Eventually I'll allow multiple years (e.g., 2010-2012 or 2010 2012 2013, etc.)")
parser.add_argument("--datdir", help="Name of directory where the data should be stored. If the directory does not exist it will be created. Defaults to data/", default = "data/")
args = parser.parse_args()
year = str(args.year)
directory = str(args.datdir)
def YearData(year, directory):
year = str(year)
if not os.path.exists(directory):
os.makedirs(directory)
dataBase = dict()
teams = ['ATL', 'ARI', 'BAL', 'BOS', 'CHC', 'CHW', 'CIN', 'CLE', 'COL', 'DET',
'KCR', 'HOU', 'LAA', 'LAD', 'MIA', 'MIL', 'MIN', 'NYM', 'NYY', 'OAK',
'PHI', 'PIT', 'SDP', 'SEA', 'SFG', 'STL', 'TBR', 'TEX', 'TOR', 'WSN']
for tm in teams:
try:
dataBase[tm] = pullGameData(tm, year)
except IndexError:
pass
gameData = pandas.concat(dataBase)
gameData.rename(columns = {"Tm" :"HomeTeam",
"Opp":"AwayTeam",
"Record":"HomeRecord",
"Runs":"R",
"OppRuns":"RA",
"W-L":"HomeWL",
"Streak":"HomeStreak"}, inplace = True)
gameData = gameData.sort_values(["Date"])
#gameData = gameData.drop("gamenum", axis = 1)
#gameData = gameData.drop("gamenum2", axis = 1)
#gameData = gameData.drop("boxscore", axis = 1)
homeData = gameData[gameData["Location"] != "@"]
homeData = homeData.drop("Location", axis = 1)
homeData["Index"] = range(0, len(homeData))
outfile = directory + year + "Games.csv"
homeData["year"] = year
homeData.to_csv(outfile, index = False, encoding = "utf-8")
return(homeData)
## Now the function is defined, use it
YearData(year, directory)