Skip to content

Instantly share code, notes, and snippets.

@ccerv1
ccerv1 / clean_dates.py
Created April 26, 2019 05:22
Use strptime to standardize our date formats
from datetime import datetime
date_format = '%Y-%m-%dT%H:%M:%S'
currency['Date'] = currency['DateTime'].apply(lambda x: datetime.strptime(x, date_format))
inflation['Date'] = inflation['DateTime'].apply(lambda x: datetime.strptime(x, date_format))
prices['Date'] = prices['DATE'].apply(lambda x: datetime.strptime(x, '%m/%d/%y'))
@ccerv1
ccerv1 / load_real_prices_data.py
Created April 26, 2019 05:14
Ingests our three datasets for our price analysis
import pandas as pd
parent_url = 'https://raw.githubusercontent.com/numbers-coffee/blog/master/real_prices/'
currency = pd.read_csv(parent_url + 'currency.csv')
inflation = pd.read_csv(parent_url + 'inflation.csv')
prices = pd.read_csv(parent_url + 'prices.csv')
@ccerv1
ccerv1 / cagr_choropleth_v2.py
Created February 23, 2019 17:34
Pretty version of the CAGR choropleth map of coffee production
growing_map = cagr_map[cagr_map.CAGR > 0]
growing_map.plot(column='CAGR', cmap='YlGn', scheme='fisher_jenks', edgecolor='black', ax=ax)
shrinking_map = cagr_map[cagr_map.CAGR < 0]
shrinking_map.plot(column='CAGR', cmap='Reds_r', scheme='fisher_jenks', edgecolor='black', ax=ax)
ax.set_ylim([-23.5,23.5])
ax.set_xlim([-120,160])
fig
@ccerv1
ccerv1 / cagr_choropleth.py
Created February 23, 2019 13:49
V1 choropleth map of coffee production growth rates
cagr_map = pd.concat([world_map, world_production], axis=1, sort=True)
cagr_map.fillna(0, inplace=True)
cagr_map.plot(column='CAGR', cmap='Blues')
@ccerv1
ccerv1 / world_production_cagr.py
Created February 23, 2019 13:43
Apply a CAGR function to each country's production series
def cagr(series):
start_val = series.iloc[0]
if not start_val:
return None
end_val = series.iloc[-1]
num_vals = len(series)
CAGR = (end_val/start_val)**(1/num_vals)-1
return CAGR
world_production['CAGR'] = world_production.apply(cagr, axis=1)
@ccerv1
ccerv1 / country_rename.py
Created February 23, 2019 13:30
Rename country indices that have alternative spellings
world_production.rename({
"Cote d'Ivoire": "Côte d'Ivoire",
"Central African Republic": "Central African Rep.",
"Congo (Kinshasa)": "Dem. Rep. Congo",
"Dominican Republic": "Dominican Rep.",
"Laos": "Lao PDR"
}, inplace=True)
@ccerv1
ccerv1 / world_production.py
Created February 23, 2019 13:21
Extract world production over the last 25 years from USDA table
world_production = attribute_query('Production')
world_production.fillna(0,inplace=True)
world_production = world_production.iloc[:,-YEARS_IN_SERIES:]
@ccerv1
ccerv1 / pretty_world_map.py
Created February 23, 2019 13:16
Pretty version of the GeoPandas world map
fig, ax = plt.subplots(figsize=(20,8))
ax.set_aspect('equal')
world_map.plot(color='#f2f2f2', edgecolor="Grey", ax=ax)
@ccerv1
ccerv1 / world_map.py
Created February 23, 2019 13:14
Create a new table with relevant features only
world_map = world[world.name != 'Antarctica']
world_map = world_map[['name', 'geometry']]
world_map.set_index('name', inplace=True)
@ccerv1
ccerv1 / geopandas_basemap.py
Created February 23, 2019 12:48
Display the default GeoPandas base map of the world
import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world.plot()