Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dharmatech/d4ebff2e8204eceb9a26136d8252efd3 to your computer and use it in GitHub Desktop.

Select an option

Save dharmatech/d4ebff2e8204eceb9a26136d8252efd3 to your computer and use it in GitHub Desktop.
import requests
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
# ----------------------------------------------------------------------
page_size = 10000
url = 'https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v2/accounting/od/debt_to_penny'
url_params = f'?page[size]={page_size}'
response = requests.get(url + url_params)
result_json = response.json()
# ----------------------------------------------------------------------
df = pd.DataFrame(result_json['data'])
df['record_date'] = pd.to_datetime(df['record_date'])
# ----------------------------------------------------------------------
plt.ion()
# ----------------------------------------------------------------------
df['debt_held_public_amt'] = pd.to_numeric(df['debt_held_public_amt'], errors='coerce')
df['intragov_hold_amt'] = pd.to_numeric(df['intragov_hold_amt' ], errors='coerce')
df['tot_pub_debt_out_amt'] = pd.to_numeric(df['tot_pub_debt_out_amt'])
plt.ion()
ax = df.plot(x='record_date', y=['debt_held_public_amt', 'intragov_hold_amt', 'tot_pub_debt_out_amt'], kind='line', rot=90)
ax.yaxis.set_major_formatter(mtick.FuncFormatter(lambda x, pos: '$' + '{:1.0f}'.format(x/1000/1000/1000/1000) + 'T'))
# ----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment