<jupyter_start><jupyter_text>Double check that you've exported your transaction history from Presto Online!Should be a `csv` file starting with `'THR'`<jupyter_code>from pathlib import Path
import matplotlib.pyplot as plt
import matplotlib.patheffects as pe
import numpy as np
import pandas as pd
try:
    foo = str(list(Path('.').glob('THR_*.csv'))[0])
except IndexError:
    print("YOU DIDN'T GET THE CSV FILE!\nTHIS INCIDENT WILL BE REPORTED! (╯°□°）╯︵ ┻━┻\n\n\n\n\n\n")
df = pd.read_csv(foo).drop('Location', axis=1)
df.head(3)
# filthy recasting
df['Date'] = df['Date'].astype('datetime64')
df['Year'] = df['Date'].dt.year
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
df['Month'] = pd.Categorical(df['Date'].dt.strftime('%b'), categories=months, ordered=True)
df['Day'] = df['Date'].dt.day
df['Amount'] = df['Amount'].str.replace('$','').str.replace(')','').str.replace('(','-').astype(float)
df['Discount'] = df['Discount'].str.replace('$','').str.replace(')','').str.replace('(','-').astype(float)
df['Balance'] = df['Balance'].str.replace('$','').str.replace(')','').str.replace('(','-').astype(float)
df = df.sort_values(by='Date').reset_index(drop=True)
daily_transaction_counts = df.groupby([
    df.Year, df.Month, df.Day
])['Type '].count()
max_trans = daily_transaction_counts.idxmax()
max_trans_dt = pd.datetime.strptime('{}-{}-{}'.format(*max_trans), '%Y-%b-%d')

print(
    'The day with the highest number of transactions (total of '+
    f'{daily_transaction_counts[max_trans]}) was: {max_trans_dt.strftime("%A %B %d, %Y")}'
)
print('The transactions for that date:')
df.loc[
    df['Date'].dt.date == max_trans_dt.date()
]
print('Fares paid using Presto card by year/month')
df.loc[
    df['Type '] == 'Fare Payment'
].groupby([
    'Year', 'Month'
])['Amount'].sum()
print('Dollar amount loaded to Presto card by year/month')
df.loc[
    df['Type '] == 'Load Amount'
].groupby([
    'Year', 'Month'
])['Amount'].sum()<jupyter_output>Dollar amount loaded to Presto card by year/month<jupyter_text>**NB**: My account is missing a $3 transaction during March 2019 that definitely happened but never made it to Presto servers...<jupyter_code>loaded = df.loc[df['Type '] == 'Load Amount', 'Amount'].sum()
spent = df.loc[df['Type '] == 'Fare Payment', 'Amount'].sum()

print(f'Total amount loaded to card: ${loaded:.2f}')
print(f'Total amount spent on fares: ${spent:.2f}')
print(f'Expected balance: ${loaded-spent:.2f}')
print(f'Actual balance: ${df.loc[df["Type "] != "Transfer"].iloc[-1].Balance:.2f}')
df.tail()
agency_colors = {
    'Toronto Transit Commission': '#DB2017', # TTC deep red
    'Union Pearson Express': '#4C4807', # UPX forest green
}

# setup figure params
fig, ax = plt.subplots(figsize=(9,9), dpi=100)

# only want fare payment amounts, grouped by year/month/agency
plot_data = df.loc[
    df['Type '] == 'Fare Payment'
].groupby([
    'Year', 'Month', 'Transit Agency'
])['Amount'].sum().unstack('Transit Agency').sort_values(by=['Year', 'Month'])

# plot stacked vertical bar
plot_data.plot(
    kind='bar', 
    ax=ax, 
    label='',
    width=0.9,
    edgecolor='k',
    # map agency to correct colour, dark-grey as default
    color=[agency_colors.get(agency, '#333333') for agency in plot_data.columns],
    stacked=True
)

# add horizontal line to show where the MDP cost cutoff is
# 2018-2019
ax.hlines(y=134, xmin=-1, xmax=7.5, label='Metropass Discount Program', linestyles='dashed')
ax.annotate('2018-2019: $134.00', (-0.5, 135.6), size=12, color='white', weight='bold', horizontalalignment='left',
    path_effects=[pe.withStroke(linewidth=3, foreground='black')]
)
# 2019-2020
ax.hlines(y=138.55, xmin=7.5, xmax=len(plot_data), label=None, linestyles='dashed')
ax.annotate('2019-2020: $138.55', (ax.get_xlim()[1].round(2)-0.2, 140.5), size=12, color='white', weight='bold', horizontalalignment='right',
    path_effects=[pe.withStroke(linewidth=3, foreground='black')]
)
# add vertical lines connecting the MDP transitions
ax.vlines(x=7.5, ymin=134, ymax=138.55, label=None, linestyles='dashed')

# add $ values for bar components
# thanks <NAME>. : https://stackoverflow.com/a/25449186
# thanks xg.plt.py : https://stackoverflow.com/a/50161387
for p in ax.patches:
    if p.get_height() != 0:
        width, height = p.get_width(), p.get_height()
        x, y = p.get_xy()
        ax.annotate(
            '${:.2f}'.format(height), 
            (x + width * 0.5, y + height * 0.5),
            ha='center',
            va='center',
            color='white',
            weight='bold',
            path_effects=[pe.withStroke(linewidth=3, foreground='black')]
        )

# plot formatting
ax.set_title('Monthly Presto Fare Payment Totals by Agency')
ax.set_xticklabels(
    ['%s - %s' % pair for pair in plot_data.index.tolist()],
    rotation = 45
)
ax.set_ylabel('Amount spent on fares ($)')
ax.set_xlabel('Year/Month')
ax.set_ylim(top=160)
ax.legend(framealpha=1, edgecolor='k', loc='upper left')
fig.tight_layout();
fig.savefig('../demo.png', dpi=300)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20137659783677483, 'fraction_numerical': 0.030285152409046213, 'mean_word_length': 4.714606741573034, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '580689', 'license': 'MIT', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1828, 'n_words': 411, 'path': 'notebooks/presto_costs.ipynb', 'repo_name': 'm9brady/Presto-EDA', 'size': '5084'}