import numpy as np
import pandas as pd
import requests
import os

# GET A CSV OF ALL STARBUCKS LOCATIONS

# If this link is ever broken, use the link above to get a new one

fname = 'All_Starbucks_Locations_in_the_World.csv'
if not(os.path.isfile(fname)):
    print 'Getting file from Socrata portal'
    r = requests.get('https://opendata.socrata.com/api/views/xy4y-c4mk/rows.csv?accessType=DOWNLOAD')
    f = open(fname, 'w')
    f.write(r.text.encode('utf-8'))
    f.close()
df = pd.read_csv(fname)

# LET'S GET SOME SUMMARY STATISTICS BY COUNTRY

by_country = pd.DataFrame(df.groupby(['Country'])['Store ID'].count())
by_country.sort('Store ID', ascending=False, inplace=True)
by_country.columns = ['count']
by_country['percentage'] = by_country['count'] / by_country['count'].sum()
by_country.head()

# DRILL DOWN BY STATES

filter = df['Country'] == 'US'
usa = pd.DataFrame(df[filter])
by_state = pd.DataFrame(usa.groupby(['Country Subdivision'])['Store ID'].count())
by_state.sort('Store ID', ascending=False, inplace=True)
by_state.columns = ['count']
by_state['percentage'] = by_state['count'] / by_state['count'].sum()
by_state.head()

"""
Explanation: Pandas

Pandas is the swiss army knife of data analysis.  It is worthy of a series of workshops.
If you are not familiar with Pandas already, you should read the 10 minutes to Pandas guide.  
For a more extensive and light hearted exploration of Pandas, also check A Pandas Cookbook by Julia Evans

Requests
Requests is a simple HTTP library for doing wget/curl type operations.
Open Data and Socrata

Socrata describes itself as a social data discovery platform.
Provides services to many cities including powering the Los Angeles data portal
Their own portal has lots of goodies also, including All Starbucks Locations in the world
End of explanation
"""


# FOCUS ON LOS ANGELES

cfilter = df['Country'] == 'US'
sfilter = df['Country Subdivision'] == 'CA'
lafilter = df['City'] == 'Los Angeles'
filter = cfilter & sfilter & lafilter
la = df[filter].copy()

# HOW MANY ROWS AND COLUMNS?

la.shape

# CAN YOU FIND YOUR FAVORITE?

la[['Street 1', 'Street 2']]

"""
Explanation: Copy is a gotcha
End of explanation
"""


co_series = la['Ownership Type']=='CO'
co_series.head()

~co_series.head()

co_series.tolist()

la.sort('Postal Code', inplace=True)
la.head()

"""
Explanation: A few Pandas features used in this workshop
End of explanation
"""


la.index

la.index = np.arange(la.shape[0])
la.index

"""
Explanation: Indexes
End of explanation
"""


la.head()

la.drop('Brand', axis=1, inplace=True)

cols = la.columns.tolist()
cols[0] = 'store_id'
la.columns = cols

la.head()

"""
Explanation: Column renaming and dropping
End of explanation
"""


