Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / continuous_date_series.py
Created April 26, 2019 05:27
Create a DataFrame with a continuous series of dates
from datetime import timedelta
start_date = datetime(1999, 4, 1)
end_date = datetime(2019, 3, 31)
this_date = start_date
date_range = []
while this_date <= end_date:
date_range.append(this_date)
this_date += timedelta(1)
@ccerv1
ccerv1 / merge_asof_real_prices.py
Created April 26, 2019 05:32
Merges are three datasets onto a continuous date series
df = pd.merge_asof(continuous_dates, currency[['Date', 'Value']], on='Date', direction='nearest')
df = pd.merge_asof(df, inflation[['Date', 'Value']], on='Date', direction='forward')
df = pd.merge_asof(df, prices[['Date', 'CLOSE']], on='Date', direction='backward')
@ccerv1
ccerv1 / format_real_price_df.py
Created April 26, 2019 05:34
Apply some formatting so our table looks nice
df.set_index('Date', inplace=True)
df.rename(columns={
'Value_x': 'FX',
'Value_y': 'Inflation',
'CLOSE': 'C Market'
}, inplace=True)
@ccerv1
ccerv1 / index_to_date.py
Created August 15, 2019 13:38
Takes a date we want to index to and a series of values, and returns a new series that shows the ratio of historical values to the current (or indexed) value
index_date = datetime(2019,3,31)
def index_to_date(date, series):
index_val = series.loc[date]
indexed_series = series / index_val
return indexed_series
df['FX Index'] = index_to_date(index_date, df['FX'])
df.plot(kind='line', y=['FX', 'FX Index'], secondary_y='FX', alpha=.5)
@ccerv1
ccerv1 / index_annual_rate_to_date.py
Created August 15, 2019 13:43
Converts annualized rates to daily rates, and indexes them to a given date
def index_annual_rate_to_date(date, series):
daily_rate = (1 + series/100) ** (1/365)
cumulative_rate = daily_rate.sort_index(ascending=False).cumprod()
return index_to_date(date, cumulative_rate)
df['Inflation Index'] = index_annual_rate_to_date(index_date, df['Inflation'])
df.plot(kind='line', y=['Inflation', 'Inflation Index'], secondary_y='Inflation')
@ccerv1
ccerv1 / real_c_prices.py
Created August 15, 2019 13:46
Convert nominal to real coffee prices
df['C Market (Real)'] = df['C Market'] * df['FX Index'] * df['Inflation Index']
df.rename(columns={'C Market': 'C Market (Nominal)'}, inplace=True)
@ccerv1
ccerv1 / plot_real_nominal_prices.py
Created August 15, 2019 13:48
Plot real and nominal prices in the same graph
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(20,8))
df.plot(kind='line', y=['C Market (Real)', 'C Market (Nominal)'], ax=ax)
current_price = df.iloc[-1]['C Market (Nominal)']
ax.axhline(current_price, color='black')
@ccerv1
ccerv1 / RPGF3_attestation_fetcher.py
Last active October 23, 2023 17:26
RPGF3 Attestations
import json
import os
import requests
EAS_SCHEMA = "0x76e98cce95f3ba992c2ee25cef25f756495147608a3da3aa2e5ca43109fe77cc"
START_TIME = 0
RAW_APPLICANT_JSON = "raw_applicant_data.json"
CLEANED_APPLICANT_JSON = "cleaned_applicant_data.json"