Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Created January 20, 2024 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharmatech/55738fc8f421eb0595c3bcffff84405f to your computer and use it in GitHub Desktop.
Save dharmatech/55738fc8f421eb0595c3bcffff84405f to your computer and use it in GitHub Desktop.
import pandas as pd
import treasury_gov_pandas
from bokeh.plotting import figure, show
from bokeh.models import NumeralTickFormatter, HoverTool
import bokeh.models
import bokeh.palettes
import bokeh.transform
# ----------------------------------------------------------------------
df = treasury_gov_pandas.update_records(
url = 'https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/dts/deposits_withdrawals_operating_cash')
df['record_date'] = pd.to_datetime(df['record_date'])
df['transaction_today_amt'] = pd.to_numeric(df['transaction_today_amt'])
# ----------------------------------------------------------------------
def show_concise(df):
return df.drop(columns=['table_nbr', 'table_nm', 'src_line_nbr', 'record_fiscal_year', 'record_fiscal_quarter', 'record_calendar_year', 'record_calendar_quarter', 'record_calendar_month', 'record_calendar_day'])
tmp = df[(df['transaction_type'] == 'Deposits') & ((df['transaction_catg'].str.contains('Tax')) | (df['transaction_catg'].str.contains('FTD'))) ]
# show_concise(tmp)
# print(show_concise(tmp).tail(500).to_string())
# tmp.value_counts('transaction_catg')
# ----------------------------------------------------------------------
p = figure(
title='TGA Taxes',
sizing_mode='stretch_both',
x_axis_type='datetime',
x_axis_label='record_date',
y_axis_label='amt')
p.add_tools(HoverTool(tooltips=[
('record_date', '@record_date{%F}'),
('transaction_today_amt', '@transaction_today_amt{$0.0a}'),
('transaction_type', '@transaction_type'),
('transaction_catg', '@transaction_catg'),
('transaction_desc', '@transaction_desc'),
],
formatters={ '@record_date' : 'datetime' }
))
width = pd.Timedelta(days=0.5)
colors = bokeh.palettes.Category20[20]
color_index = 0
for transaction_catg in tmp['transaction_catg'].unique():
items = tmp[tmp['transaction_catg'] == transaction_catg]
# p.line( x='record_date', y='transaction_today_amt', legend_label=transaction_catg, source=bokeh.models.ColumnDataSource(items))
p.vbar( x='record_date', top='transaction_today_amt', legend_label=transaction_catg, source=bokeh.models.ColumnDataSource(items), width=width, color=colors[color_index])
color_index = (color_index + 1) % len(colors)
p.xaxis.ticker = bokeh.models.DatetimeTicker(desired_num_ticks=30)
p.legend.click_policy = 'hide'
p.legend.location = 'top_left'
show(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment