import numpy as np
import pandas as pd

"""
Explanation: 911 Calls Capstone Project
For this capstone project we will be analyzing some 911 call data from Kaggle. The data contains the following fields:

lat : String variable, Latitude
lng: String variable, Longitude
desc: String variable, Description of the Emergency Call
zip: String variable, Zipcode
title: String variable, Title
timeStamp: String variable, YYYY-MM-DD HH:MM:SS
twp: String variable, Township
addr: String variable, Address
e: String variable, Dummy variable (always 1)

Just go along with this notebook and try to complete the instructions or answer the questions in bold using your Python and Data Science skills!
Data and Setup

 Import numpy and pandas
End of explanation
"""


import matplotlib.pyplot as plt
%matplotlib inline

"""
Explanation: Import visualization libraries and set %matplotlib inline.
End of explanation
"""


df = pd.read_csv('./911.csv')
df.head()

"""
Explanation: Read in the csv file as a dataframe called df
End of explanation
"""


df.info()

df.shape

"""
Explanation: Check the info() of the df
End of explanation
"""


zip_vc = df['zip'].value_counts()
type(zip_vc)
zip_vc.iloc[0:5]

"""
Explanation: Basic Questions
 What are the top 5 zipcodes for 911 calls?
End of explanation
"""


df['twp'].value_counts().iloc[0:5]

"""
Explanation: What are the top 5 townships (twp) for 911 calls?
End of explanation
"""


len(df['title'].value_counts())

df['title'].nunique()

"""
Explanation: Take a look at the 'title' column, how many unique title codes are there?
End of explanation
"""


df.head()

"""
Explanation: Creating new features
End of explanation
"""


z = lambda x : x.split(":")[0]

z('EMS: BACK PAINS/INJURY')

df['reason'] = df['title'].apply(lambda x : x.split(':')[0])
df.head()

"""
Explanation: In the titles column there are "Reasons/Departments" specified before the title code. These are EMS, Fire, and Traffic. Use .apply() with a custom lambda expression to create a new column called "Reason" that contains this string value. 
For example, if the title column value is EMS: BACK PAINS/INJURY , the Reason column value would be EMS.
End of explanation
"""


df['reason'].value_counts()

"""
Explanation: What is the most common Reason for a 911 call based off of this new column?
End of explanation
"""


import seaborn as sns

sns.countplot(df['reason'])

"""
Explanation: Now use seaborn to create a countplot of 911 calls by Reason.
End of explanation
"""


df['timeStamp'].iloc[0]

"""
Explanation: Now let us begin to focus on time information. What is the data type of the objects in the timeStamp column?
End of explanation
"""


df['timestamp2'] = pd.to_datetime(df['timeStamp'], infer_datetime_format=True)

df['timestamp2'].head()

"""
Explanation: You should have seen that these timestamps are still strings. Use pd.to_datetime to convert the column from strings to DateTime objects.
End of explanation
"""


time1 = df['timestamp2'].iloc[0]
time1.hour

time1.date()

time1.month

df['hour'] = df['timestamp2'].apply(lambda x:x.hour)
df['month'] = df['timestamp2'].apply(lambda x:x.month)
df['dayofweek'] = df['timestamp2'].apply(lambda x:x.dayofweek)

"""
Explanation: You can now grab specific attributes from a Datetime object by calling them. For example:
time = df['timeStamp'].iloc[0]
time.hour

You can use Jupyter's tab method to explore the various attributes you can call. Now that the timestamp column are actually DateTime objects, use .apply() to create 3 new columns called Hour, Month, and Day of Week. You will create these columns based off of the timeStamp column, reference the solutions if you get stuck on this step.
End of explanation
"""


dmap = {0:'Mon',1:'Tue',2:'Wed',3:'Thu',4:'Fri',5:'Sat',6:'Sun'}
df['dayofweek'] = df['dayofweek'].apply(lambda x:dmap[x])

df.head(3)

"""
Explanation: Notice how the Day of Week is an integer 0-6. Use the .map() with this dictionary to map the actual string names to the day of the week: 
dmap = {0:'Mon',1:'Tue',2:'Wed',3:'Thu',4:'Fri',5:'Sat',6:'Sun'}
End of explanation
"""


sns.countplot(x=df['dayofweek'],hue=df['reason'])

"""
Explanation: Now use seaborn to create a countplot of the Day of Week column with the hue based off of the Reason column.
End of explanation
"""


sns.countplot(x=df['month'], hue=df['reason'])

"""
Explanation: Now do the same for Month:
End of explanation
"""


byMonth = df.groupby('month').count()
byMonth

"""
Explanation: Did you notice something strange about the Plot?

 You should have noticed it was missing some Months, let's see if we can maybe fill in this information by plotting the information in another way, possibly a simple line plot that fills in the missing months, in order to do this, we'll need to do some work with pandas... 
 Now create a gropuby object called byMonth, where you group the DataFrame by the month column and use the count() method for aggregation. Use the head() method on this returned DataFrame.
End of explanation
"""


byMonth.plot(y='lat', kind='line')

"""
Explanation: Now create a simple plot off of the dataframe indicating the count of calls per month.
End of explanation
"""


byMonth.reset_index(inplace=True)

sns.lmplot(x='month',y='lat', data=byMonth)

"""
Explanation: Now see if you can use seaborn's lmplot() to create a linear fit on the number of calls per month. Keep in mind you may need to reset the index to a column.
End of explanation
"""


df['date'] = df['timestamp2'].apply(lambda x: x.date())

"""
Explanation: Create a new column called 'Date' that contains the date from the timeStamp column. You'll need to use apply along with the .date() method.
End of explanation
"""


byDate = df.groupby('date').count()
byDate.head()

byDate.plot(y='reason', kind='line', color='green')

"""
Explanation: Now groupby this Date column with the count() aggregate and create a plot of counts of 911 calls.
End of explanation
"""


sns.countplot(x='date', data=df, hue='reason')

dfFire = df[df['reason']=='Fire'].groupby('date').count()
dfEMS = df[df['reason']=='EMS'].groupby('date').count()
dfTraffic = df[df['reason']=='Traffic'].groupby('date').count()

fig, ax = plt.subplots(1,1, figsize=(15,5))
dfFire.plot(y='lat', kind='line', color='orange', ax=ax, label='Fire')
dfEMS.plot(y='lat', kind='line', color='blue', ax=ax, label='EMS')
dfTraffic.plot(y='lat', kind='line', color='red', ax=ax, label='Traffic')

"""
Explanation: Now recreate this plot but create 3 separate plots with each plot representing a Reason for the 911 call
End of explanation
"""


