train_df['year'] = train_df.quote_date.apply(lambda d : d.year)
train_df['month'] = train_df.quote_date.apply(lambda d : d.month)

"""
Explanation: Add two columns, year and month, to the dataframe.
End of explanation
"""


cost_count_by_year_month = train_df.groupby(['year', 'month']).cost.count()

plt.subplots(figsize=(8,6))
cost_count_by_year_month.plot()
plt.title('The number of cost records in each year and month', {'fontsize' : 'large'})
plt.ylabel('count', {'fontsize' : 'large'})
plt.grid()
plt.show()

"""
Explanation: How many cost(i.e. records) are there in each (year, month).
End of explanation
"""


cost_count_by_year_month.ix[2011:]

"""
Explanation: We see that from 2011, the number of records increases significantly.
End of explanation
"""


median_cost_by_year_month = train_df.groupby(by=['year', 'month']).cost.median()
median_cost_by_year_month.plot()
plt.title('Median cost in each (year, month)')
plt.ylabel('cost')
plt.grid()
plt.show()

"""
Explanation: Let's plot the median cost in each (year, month) group.
End of explanation
"""


median_cost_by_year_month.ix[2011:].plot()
plt.title('Median cost in each (year, month)')
plt.ylabel('cost')
plt.grid()
plt.show()

"""
Explanation: But since only after 2011 there exists sufficient amount of quotes, let's zoom in to the post 2011 period.
End of explanation
"""


print median_cost_by_year_month[median_cost_by_year_month>50]

"""
Explanation: The 'outliers' in the median cost space are shown below.
End of explanation
"""


cost_by_year_month = train_df.groupby(by=['year', 'month']).cost

fig, ax = plt.subplots(figsize=(8, 6))
train_df.boxplot(column='cost', by='year', ax=ax)
# cost_by_year_month.plot(kind='box')

plt.xticks(rotation=90)
plt.show()

train_df_no_bracket = train_df[train_df.bracket_pricing == 'No']

train_df_no_bracket

"""
Explanation: We might be more interested in the distribution of the cost within each (year, month).
End of explanation
"""


