import sys                             # system module 
import pandas as pd                    # data package
import matplotlib.pyplot as plt        # graphics module  
import datetime as dt                  # date and time module
import numpy as np                     # foundation for Pandas 

%matplotlib inline                     

# check versions
print('Python version:', sys.version)
print('Pandas version: ', pd.__version__)
print('Today: ', dt.date.today())

    

"""
Explanation: “The Steroid Era” in Major League Baseball: A Look at the statistics
May 2016
Written by Anthony Riccio at NYU Stern 
Contact: ajr587@stern.nyu.edu
Overview
Mark McGwire & Sammy Sosa, the two names that come to mind I think about the steroid era in Major League Baseball (MLB). The steroid era refers to a period of time in the MLB where a number of players were believed to have used performance enhancing drugs (PEDs) to improve offensive performance.  David Wells stated that “25 to 40 percent of all Major Leaguers used PEDs.” Jose Canseco stated in his tell-all book ‘Juiced’ that as many as 80 percent of Major Leaguers used PEDs.
Although steroids where banned in the MLB in 1991, the league did not implement league wide testing until 2003.  The steroid era is generally considered to have run from the late ‘80s through the late 2000s. The one thing that baseball has a lot of is data, so I wanted to take a deeper drive into to offensive statistics around baseball in what was considered the steroid era and see if I can draw any conclusions based on the data.
End of explanation
"""


url = 'https://raw.githubusercontent.com/maxtoki/baseball_R/master/data/Batting.csv' # importing our data
bs = pd.read_csv(url)
bs.head() # taking a look at the data 

print('Dimensions: ', bs.shape)  # looking at the categories I can work with
print('Column labels: ', bs.columns)
print('Row labels: ', bs.index)

names = list(bs) # Changing the year label
bs = bs.rename(columns={names[1]: 'Year'})
bs.head(2)

bsmall = bs.head()  #inspecting the data
bsmall

bsmall.shape

bsmall.describe  # inspecting the data

"""
Explanation: Packages Imported
I imported pandas, a Python package that allows for data manipulation and analysis; Matplotlib.pyplot for graphing; Numpy for scientific computing 
Creating the Data Set
I wanted to get a data set with an extensive amount of baseball statistics on the most common offensive statistics (i.e. Home Runs, Hits) but also in a format the I can break down into easily managable pieces.
After some research, I stumbled upon Sean Lahman's archive of baseball statistics (http://www.seanlahman.com/baseball-archive/statistics/)
This is the data set I will be using for my project.
End of explanation
"""


hrs_by_year = bs.groupby(['Year'])['HR'].sum()  # grouping data by year and hrs

hrs_by_year[2004]  # check a data point

hrs_by_year.tail(25)

plt.figure(figsize = (16,7))
plt.plot(hrs_by_year.tail(25), color = 'red', marker= '*')
plt.suptitle('Total Home Runs by Year', fontsize=18)
plt.xlabel('Year', fontsize=12)
plt.ylabel('Home Runs', fontsize=12)

"""
Explanation: Number of Home Runs by Year
Taking a look at home runs over a 25 year period shows us something interesting - the number of home runs per season really begin to spike over the 94'-00' season. This period lines up with what has been determined to be the steroid era. This is a substantial piece of evidence to help us draw our conclusions. The number of home runs spike up during the proclaimed home run period, but then begin to dip down as the league became more stringent around testing.
End of explanation
"""


sb_by_year = bs.groupby(['Year'])['SB'].sum()  # grouping data by year and stolen bases

sb_by_year.tail(25)

plt.figure(figsize = (16,7)) # plotting the data
plt.plot(sb_by_year.tail(25), color = 'blue', marker= '*')
plt.suptitle('Stolen Bases by Year', fontsize=18)
plt.xlabel('Year', fontsize=12)
plt.ylabel('Stolen Bases', fontsize=12)

"""
Explanation: Number of Stolen Bases by Year
Next, I wanted to look at the number of stolen bases by season to see how that compared to home runs and doubles. I would think that because the players were bulking up and getting bigger that in essence they wouldn't be as fast and ultimately wouldn't steal as many bases. The data supports this conclusion or that the increase in home runs and doubles decreased the need for players to steal bases.
End of explanation
"""


scndbase_by_year = bs.groupby(['Year'])['2B'].sum()  # grouping data by year and doubles
scndbase_by_year.tail(25)

plt.figure(figsize = (16,7)) # plotting our data
plt.plot(scndbase_by_year.tail(25), color = 'green', marker= '*')
plt.suptitle('Doubles by Year', fontsize=18)
plt.xlabel('Year', fontsize=12)
plt.ylabel('Doubles', fontsize=12)

"""
Explanation: Number of Doubles by Year
I also wanted to look the number of doubles by season over the same time period to see if it was somehow correlated to home runs. I would would expect to see a positive correlation between the two because a high percentage of doubles are all most home runs that just didn't have the height or distance to make it out of the park. By looking at the data, we do see a spike in doubles around the same time as home runs.
End of explanation
"""


hits_by_year = bs.groupby(['Year'])['H'].sum() # grouping data by year and hits
hits_by_year.tail(25)

plt.figure(figsize = (16,7))  # plotting our data
plt.plot(hits_by_year.tail(25), color = 'blue', marker= '*',)
plt.suptitle('Hits by Year', fontsize=18)
plt.xlabel('Year', fontsize=12)
plt.ylabel('Hits', fontsize=12)

"""
Explanation: Number of Hits by Year
I wanted to also plot the number of hits because the number of hits could have increased at the same time as home runs. Meaning that the increase hits could have drove up the number of home runs in proportion, not supporting our assumption. The data shows a postive correlation between hits with home runs, and hits and doubles.
End of explanation
"""


df = bs.groupby(['Year'])['2B','HR','H'].sum()  #looking at the homeruns and doubles as a % of total hits
df = df.tail(25) 
df['Other'] = df['H'] - df['HR'] - df['2B']
df['% HR'] = df['HR'] / df['H']
df['% 2B'] = df['2B'] / df['H']
df['% Other'] = df['Other'] / df['H']
df = df.drop ('2B', 1)
df = df.drop ('HR', 1)
df = df.drop ('H', 1)
df = df.drop ('Other', 1)
df

my_plot = df.plot(kind='bar',stacked=True,figsize=(16,7), fontsize = (14))  # creating a stacked bar graph
my_plot.set_title("Hits by Category", fontsize = (16))
my_plot.set_xlabel("Year", fontsize = (14))
my_plot.set_ylabel("Hits", fontsize = (14))
my_plot.legend(["2B","HR","Other"], loc=9,ncol=3)

"""
Explanation: Stacked Bar Graph of Hits
So let's dive a little deeper and break down the number of home runs and doubles as a percentage of hits.  Looking a this stack bar graph helps show the percentage increase in doubles and home runs, but it does not clearly displaying as accurate of a picture as I would like.
End of explanation
"""


df2 = df.plot(kind='barh', figsize=(16,7), fontsize = (14), y = '% HR', title = '% of Hits HRs')  # ploting hrs as a % of total hits

"""
Explanation: % of Home Runs
So I decided to plot just the percentage of hits that were home runs to see if this can help us draw conclusions about the steroid era. Looking a the graph we can indeed see that even though hits increased, the percentage of home runs increased over the steroid period in baseball.
End of explanation
"""


