{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reloading matplotlib.backends.__future__\n", "Reloading __future__\n", "Reloading matplotlib.backends.matplotlib\n", "Reloading matplotlib\n", "Reloading distutils\n", "Reloading distutils.version\n", "Reloading distutils.string\n", "Reloading string\n", "Reloading re\n", "Reloading _sre\n", "Reloading sre_compile\n", "Reloading sre_parse\n", "Reloading sre_constants\n", "Reloading copy_reg\n", "Reloading types\n", "Reloading strop\n", "Reloading distutils.re\n", "Reloading distutils.types\n", "Reloading dateutil\n", "Reloading pyparsing\n", "Reloading weakref\n", "Reloading UserDict\n", "Reloading _abcoll\n", "Reloading abc\n", "Reloading _weakrefset\n", "Reloading _weakref\n", "Reloading exceptions\n", "Reloading copy\n", "Reloading org\n", "Reloading warnings\n", "Reloading linecache\n", "Reloading os\n", "Reloading errno\n", "Reloading posix\n", "Reloading posixpath\n", "Reloading stat\n", "Reloading genericpath\n", "Reloading _warnings\n", "Reloading collections\n", "Reloading _collections\n", "Reloading operator\n", "Reloading keyword\n", "Reloading heapq\n", "Reloading itertools\n", "Reloading _heapq\n", "Reloading thread\n", "Reloading shutil\n", "Reloading fnmatch\n", "Reloading pwd\n", "Reloading grp\n", "Reloading distutils.sysconfig\n", "Reloading distutils.os\n", "Reloading distutils.sys\n", "Reloading distutils.distutils\n", "Reloading distutils.errors\n", "Reloading matplotlib.cbook\n", "Reloading matplotlib.__future__\n", "Reloading matplotlib.datetime\n", "Reloading datetime\n", "Reloading matplotlib.errno\n", "Reloading matplotlib.functools\n", "Reloading functools\n", "Reloading _functools\n", "Reloading matplotlib.glob\n", "Reloading glob\n", "Reloading matplotlib.gzip\n", "Reloading gzip\n", "Reloading struct\n", "Reloading _struct\n", "Reloading time\n", "Reloading zlib\n", "Reloading io\n", "Reloading _io\n", "Reloading matplotlib.io\n", "Reloading matplotlib.locale\n", "Reloading locale\n", "Reloading encodings\n", "Reloading encodings.codecs\n", "Reloading codecs\n", "Reloading _codecs\n", "Reloading encodings.encodings\n", "Reloading encodings.__builtin__\n", "Reloading encodings.aliases\n", "Reloading _locale\n", "Reloading matplotlib.os\n", "Reloading matplotlib.re\n", "Reloading matplotlib.sys\n", "Reloading matplotlib.threading\n", "Reloading threading\n", "Reloading traceback\n", "Reloading matplotlib.time\n", "Reloading matplotlib.traceback\n", "Reloading matplotlib.types\n", "Reloading matplotlib.warnings\n", "Reloading matplotlib.weakref\n", "Reloading matplotlib.numpy\n", "Reloading numpy\n", "Reloading numpy.__config__\n", "Reloading numpy.version\n", "Reloading numpy._import_tools\n", "Reloading numpy.testing\n", "Reloading unittest\n", "Reloading unittest.result\n", "Reloading unittest.os\n", "Reloading unittest.sys\n", "Reloading unittest.traceback\n", "Reloading unittest.StringIO\n", "Reloading StringIO\n", "Reloading unittest.functools\n", "Reloading unittest.case\n", "Reloading unittest.collections\n", "Reloading unittest.difflib\n", "Reloading difflib\n", "Reloading unittest.pprint\n", "Reloading pprint\n", "Reloading cStringIO\n", "Reloading unittest.re\n", "Reloading unittest.types\n", "Reloading unittest.warnings\n", "Reloading unittest.util\n", "Reloading unittest.suite\n", "Reloading unittest.loader\n", "Reloading unittest.fnmatch\n", "Reloading unittest.main\n", "Reloading unittest.signals\n", "Reloading unittest.signal\n", "Reloading signal\n", "Reloading unittest.weakref\n", "Reloading unittest.runner\n", "Reloading unittest.time\n", "Reloading numpy.testing.utils\n", "Reloading numpy.testing.nosetester\n", "Reloading numpy.compat\n", "Reloading numpy.compat._inspect\n", "Reloading numpy.compat.py3k\n", "Reloading numpy.core\n", "Reloading numpy.core.info\n", "Reloading numpy.core.numeric\n", "Reloading numpy.core.umath\n", "Reloading numpy.core.numerictypes\n", "Reloading numpy.core.multiarray\n", "Reloading cPickle\n", "Reloading numpy.core._dotblas\n", "Reloading numpy.core.arrayprint\n", "Reloading numpy.core.fromnumeric\n", "Reloading numpy.core.records\n", "Reloading numpy.core.defchararray\n", "Reloading numpy.core.memmap\n", "Reloading numpy.core.function_base\n", "Reloading numpy.core.machar\n", "Reloading numpy.core.getlimits\n", "Reloading numpy.core.shape_base\n", "Reloading numpy.testing.numpytest\n", "Reloading numpy.lib\n", "Reloading math\n", "Reloading numpy.lib.info\n", "Reloading numpy.lib.type_check\n", "Reloading numpy.lib.ufunclike\n", "Reloading numpy.lib.index_tricks\n", "Reloading numpy.matrixlib\n", "Reloading numpy.matrixlib.defmatrix\n", "Reloading numpy.lib.function_base\n", "Reloading numpy.lib.twodim_base\n", "Reloading numpy.lib._compiled_base\n", "Reloading numpy.lib.utils\n", "Reloading numpy.lib.stride_tricks\n", "Reloading numpy.lib.nanfunctions\n", "Reloading numpy.lib.shape_base\n", "Reloading numpy.lib.polynomial\n", "Reloading numpy.linalg\n", "Reloading numpy.linalg.info\n", "Reloading numpy.linalg.linalg\n", "Reloading numpy.lib.arraysetops\n", "Reloading numpy.lib.npyio\n", "Reloading numpy.lib._datasource\n", "Reloading numpy.lib._iotools\n", "Reloading future_builtins\n", "Reloading numpy.lib.financial\n", "Reloading numpy.lib.arrayterator\n", "Reloading numpy.lib.arraypad\n", "Reloading numpy.ma\n", "Reloading numpy.ma.core\n", "Reloading numpy.ma.extras\n", "Reloading matplotlib.compat\n", "Reloading tempfile\n", "Reloading random\n", "Reloading binascii\n", "Reloading hashlib\n", "Reloading _hashlib\n", "Reloading _random\n", "Reloading fcntl\n", "Reloading matplotlib.rcsetup\n", "Reloading matplotlib.matplotlib\n", "Reloading matplotlib.fontconfig_pattern\n", "Reloading matplotlib.pyparsing\n", "Reloading matplotlib.colors\n", "Reloading matplotlib.textwrap\n", "Reloading textwrap\n", "Reloading matplotlib.backends.inspect\n", "Reloading inspect\n", "Reloading dis\n", "Reloading opcode\n", "Reloading imp\n", "Reloading tokenize\n", "Reloading token\n", "Reloading matplotlib.backends.warnings\n", "('0.14.1', '1.8.2')\n" ] }, { "ename": "TypeError", "evalue": "unbound method __init__() must be called with UserDict instance as first argument (got WeakValueDictionary instance instead)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.pyc\u001b[0m in \u001b[0;36mfigure\u001b[0;34m(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)\u001b[0m\n\u001b[1;32m 421\u001b[0m \u001b[0mframeon\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mframeon\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 422\u001b[0m \u001b[0mFigureClass\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mFigureClass\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 423\u001b[0;31m **kwargs)\n\u001b[0m\u001b[1;32m 424\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 425\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfigLabel\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.pyc\u001b[0m in \u001b[0;36mnew_figure_manager\u001b[0;34m(num, *args, **kwargs)\u001b[0m\n\u001b[1;32m 235\u001b[0m \"\"\"\n\u001b[1;32m 236\u001b[0m \u001b[0mFigureClass\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'FigureClass'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mFigure\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 237\u001b[0;31m \u001b[0mfigure\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFigureClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 238\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mnew_figure_manager_given_figure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfigure\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 239\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/site-packages/matplotlib/figure.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, figsize, dpi, facecolor, edgecolor, linewidth, frameon, subplotpars, tight_layout)\u001b[0m\n\u001b[1;32m 309\u001b[0m \u001b[0mframeon\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrcParams\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'figure.frameon'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 310\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 311\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdpi_scale_trans\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAffine2D\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 312\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdpi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdpi\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 313\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbbox_inches\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mBbox\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_bounds\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0mfigsize\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/site-packages/matplotlib/transforms.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, matrix, **kwargs)\u001b[0m\n\u001b[1;32m 1699\u001b[0m \u001b[0mIf\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0mmatrix\u001b[0m\u001b[0;34m*\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitialize\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mthe\u001b[0m \u001b[0midentity\u001b[0m \u001b[0mtransform\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1700\u001b[0m \"\"\"\n\u001b[0;32m-> 1701\u001b[0;31m \u001b[0mAffine2DBase\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1702\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmatrix\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1703\u001b[0m \u001b[0mmatrix\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0midentity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/site-packages/matplotlib/transforms.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1544\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1545\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1546\u001b[0;31m \u001b[0mTransform\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1547\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_inverted\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1548\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python2.7/site-packages/matplotlib/transforms.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, shorthand_name)\u001b[0m\n\u001b[1;32m 91\u001b[0m \u001b[0;31m# parents are deleted, references from the children won't keep\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 92\u001b[0m \u001b[0;31m# them alive.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 93\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parents\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mWeakValueDictionary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 94\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 95\u001b[0m \u001b[0;31m# TransformNodes start out as invalid until their values are\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.pyc\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, *args, **kw)\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mwr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_remove\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mremove\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 53\u001b[0;31m \u001b[0mUserDict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mUserDict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__getitem__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: unbound method __init__() must be called with UserDict instance as first argument (got WeakValueDictionary instance instead)" ] } ], "source": [ "%matplotlib inline\n", "#%pylab inline\n", "import pandas as pd\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import json\n", "\n", "import quandl\n", "import os\n", "\n", "from IPython.display import display\n", "\n", "#KEY = '...'\n", "#!mkdir ./.keys\n", "#with open('./.keys/quandl-api-key.json','w') as f:\n", "# json.dump({'key': KEY}, f)\n", "\n", "KEYFILE = './.keys/quandl-api-key.json'\n", "if os.path.exists(KEYFILE):\n", " with open('./.keys/quandl-api-key.json','r') as f:\n", " quandl_token = json.load(f)['key']\n", " \n", " import functools\n", " _quandl_get = functools.partial(quandl.get, authtoken=quandl_token)\n", "else:\n", " _quandl_get = quandl.get\n", " \n", "print(pd.__version__, np.__version__)\n", "fig = plt.figure()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# http://www.quandl.com/FRED-Federal-Reserve-Economic-Data/USARGDPR-Real-GDP-in-the-United-States\n", "# http://www.quandl.com/FRED-Federal-Reserve-Economic-Data/GDP-Gross-Domestic-Product-1-Decimal\n", "# http://www.quandl.com/FRED-Federal-Reserve-Economic-Data/FYGFD-Gross-Federal-Debt\n", "# http://www.quandl.com/FRED-Federal-Reserve-Economic-Data/USAPOPL-Population-in-the-United-States\n", "# http://www.quandl.com/FRED-Federal-Reserve-Economic-Data/CPIAUCSL-Consumer-Price-Index-for-All-Urban-Consumers-All-Items-USA-Inflation\n", "import collections\n", "_data = collections.OrderedDict()\n", "for _key in ['FRED/USARGDPR', 'FRED/GDP', 'FRED/FYGFD', 'FRED/USAPOPL', 'FRED/CPIAUCSL']:\n", " _data[_key.replace('/','_')] = _quandl_get(_key)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mpl.rcParams['figure.figsize'] = (20,4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "#for k,v in _data.iteritems():\n", "# v.plot(\n", "# title=k,\n", "# xlim=('1940','2020'),\n", "# ylim=(0, v.max()),\n", "# xticks=[str(x) for x in range(1949,2017,4)],\n", "# #x_compat=True,\n", "#)\n", "#usargdpr.plot(), usagdp.plot(), fygfd.plot(), popl.plot()\n", "#usargdpr." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "_data['FRED_USARGDPR'].head(), _data['FRED_FYGFD'].head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "((_data['FRED_USARGDPR'].resample('A', how='mean'))\n", " / _data['FRED_FYGFD'].resample('A', how='mean')).plot(title='USARGDPR / FYGFD')\n", "((_data['FRED_GDP'].resample('A', how='mean'))\n", " / _data['FRED_FYGFD'].resample('A', how='mean')).plot(title='GDP / FYGFD')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "((_data['FRED_GDP'].resample('A', how='mean'))\n", " / _data['FRED_USAPOPL'].resample('A', how='mean')).plot(title='FRED_GDP / FRED_USAPOPL')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "((_data['FRED_FYGFD'].resample('A', how='mean'))\n", " / _data['FRED_GDP'].resample('A', how='mean')).plot(title='FRED_FYGFD / FRED_GDP')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot = _data['FRED_CPIAUCSL'].resample('A', how='mean').plot(\n", " xticks=[str(x) for x in range(1949,2017,4)],\n", " x_compat=True,\n", " title=\"Yearly Inflation (CPI)\"\n", " )\n", "plot.legend(loc='upper left')\n", "\n", "\n", "def add_line(plot, _year, text=None):\n", " _max = plot.yaxis.get_view_interval()[-1]\n", " plot.plot((_year,_year), (0, _max),\n", " color='gray', linewidth=1.5, linestyle=\"--\")\n", " plot.annotate(\n", " text if text is not None else _year,\n", " xy=(_year, 0),\n", " xycoords='data',\n", " xytext=(+10, +30),\n", " textcoords='offset points',\n", " fontsize=12,\n", " #arrowprops=dict(arrowstyle=\"->\"), #, connectionstyle=\"\"), #arc3,rad=.2\"),\n", " rotation='vertical',\n", " verticalalignment='bottom',\n", " horizontalalignment='center')\n", "\n", "for year in range(1949, 2017, 4):\n", " add_line(plot, str(year))\n", "\n", "display(plot)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "us_presidents_csv_url = 'https://commondatastorage.googleapis.com/ckannet-storage/2012-05-08T122246/USPresident-Wikipedia-URLs-Thmbs-HS.csv' " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "def get_presidents_df(data_file='./data/us_presidents.csv', data_url=us_presidents_csv_url):\n", " datadir = os.path.dirname(data_file)\n", " os.path.exists(datadir) or os.makedirs(datadir)\n", " !wget --continue --no-clobber $data_url -O $data_file\n", " df = presidents = pd.read_csv(data_file)\n", " \n", " df = presidents[['President ','Took office ','Left office ']]\n", " df['Took office '] = pd.to_datetime(presidents['Took office '])\n", " df['Left office '] = pd.to_datetime(presidents['Left office '], coerce=True)\n", " #display(df)\n", " df = df.set_index('Took office ', drop=False, verify_integrity=True)\n", " df['term'] = df['Left office '] - df['Took office ']\n", " \n", " col = df['term']\n", " val = col[0]\n", " df['term'] = (\n", " col.apply(\n", " lambda x: x.astype('datetime64'),\n", " convert_dtype=False))\n", " col = df['term']\n", " val = col[0]\n", " print(val)\n", " \n", " #val.item().days\n", " #df['terms'] = df['term'].apply(\n", " # lambda x: (x.item().days if x.item() else 0)\n", " # / float(365.25*4))\n", " return df\n", "\n", "#df['terms'] = (df['term'] / np.timedelta64(1, 'D')) / float(365.25*4) # pandas 0.13\n", "\n", "df = get_presidents_df()\n", "display(df.head())\n", "\n", "def presidents_by_year(df=None):\n", " if df is None:\n", " df = get_presidents_df()\n", " for year,name in df.ix[:,['President ']].to_records():\n", " print(year.year, name)\n", "\n", "def add_presidents(plot, presidents=None, yearmin=0):\n", " if presidents is None:\n", " presidents = get_presidents_df()\n", "\n", " for year,name in presidents.ix[str(yearmin):,['President ']].to_records():\n", " #print year.year, name\n", " add_line(plot, year, name)\n", "\n", "def poli_plot(df, **kwargs):\n", " yearmin = df.index.min().year\n", " yearmax = 2017\n", " \n", " plot = df.plot(\n", " xticks=[str(x) for x in range(yearmax, yearmin,-4)],\n", " x_compat=True,\n", " **kwargs)\n", " plot.legend(loc='upper left')\n", " \n", " add_presidents(plot, yearmin=yearmin)\n", " return plot\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "df = _data['FRED_CPIAUCSL']\n", "poli_plot(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = ((_data['FRED_GDP'].resample('A', how='mean')) / _data['FRED_USAPOPL'].resample('A', how='mean'))\n", "poli_plot(df, title=\"GDP per capita (thousands of dollars)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = ((_data['FRED_FYGFD'].resample('A', how='mean')) / _data['FRED_USAPOPL'].resample('A', how='mean'))\n", "poli_plot(df, title=\"Federal debt per capita (thousands of dollars)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "inflation_factor_linear = (_data['FRED_CPIAUCSL'] / _data['FRED_CPIAUCSL'].max()).resample('A', how='mean')\n", "inflation_factor_uhh = 1 / inflation_factor_linear\n", "cpi = inflation_factor_uhh\n", "#display( cpi.head()) \n", "#display( cpi.tail())\n", "\n", "\n", "df = ((_data['FRED_FYGFD'].resample('A', how='mean')) / _data['FRED_USAPOPL'].resample('A', how='mean'))\n", "#print( df.columns )\n", "\n", "scaled = (df * cpi)\n", "#display(scaled.tail())\n", "display(poli_plot(_data['FRED_CPIAUCSL'], title=\"inflation (FRED CPI UCSL)\"))\n", "plot = poli_plot(_data['FRED_CPIAUCSL'].resample('A','mean').pct_change(), title='yearly % change in inflation')\n", "plot.axhline()\n", "\n", "display(plot)\n", "\n", "\n", "display(poli_plot(df, title=\"debt-per-capita\"))\n", "display(poli_plot(scaled, title=\"debt-per-capita scaled for inflation\"))\n", "plot = poli_plot(scaled.pct_change(), title=\"Yearly % change in debt-per-capita scaled for inflation\")\n", "plot.axhline()\n", "display(plot)\n", "\n", "\n", "#poli_plot(df)\n", "#poli_plot(df * inflation_factor_linear)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# TODO: add house/senate majority party\n", "# TODO: add major wars" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "!wget --continue --no-clobber https://github.com/unitedstates/congress-legislators/raw/master/legislators-historical.yaml -O ./data/legislators-historical.yaml\n", "!wget --continue --no-clobber https://github.com/unitedstates/congress-legislators/raw/master/legislators-current.yaml -O ./data/legislators-current.yaml\n", "#import yaml\n", "#data = None\n", "#with open('./data/legislators-historical.yaml','rb') as f:\n", "# data = yaml.load(f)\n", "!ls ./data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import yaml\n", "def iter_members(\n", " data_files=['./data/legislators-historical.yaml',\n", " './data/legislators-current.yaml']):\n", " \n", " for data_file in data_files:\n", " data = None\n", " with open(data_file,'rb') as f:\n", " data = yaml.load(f)\n", " for m in data:\n", " for t in m['terms']:\n", " yield (\n", " t['state'],\n", " t['type'],\n", " t['start'],\n", " t['end'],\n", " t.get('party'),\n", " m['name']['first'],\n", " m['name']['last'],\n", " m.get('bio',{}).get('gender', 'M'), # ...\n", " m.get('bio',{}).get('birthday')\n", " )\n", "iter_members.columns = [\n", " 'state',\n", " 'type',\n", " 'start',\n", " 'end',\n", " 'party',\n", " 'first',\n", " 'last',\n", " 'gender',\n", " 'birthday'\n", "]\n", "\n", "_legislator_data = list(iter_members())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df = pd.DataFrame.from_records(\n", " _legislator_data,\n", " columns=iter_members.columns)\n", "df['start'] = pd.to_datetime(df['start'])\n", "df['end'] = pd.to_datetime(df['end'])\n", "df['birthday'] = pd.to_datetime(df['birthday'])\n", "df.set_index('start', drop=False, inplace=True)\n", "display(df.head())\n", "display(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "col = df['party']\n", "uniques = dict.fromkeys(col.unique())\n", "#print(uniques)\n", "\n", "_party_map = {}\n", "repub, democ, other = [], [], []\n", "for x in uniques:\n", " if x is not None:\n", " if 'Republ' in x:\n", " repub.append(x)\n", " _party_map[x] = 'Republican'\n", " elif 'Democr' in x:\n", " democ.append(x)\n", " _party_map[x] = 'Democrat'\n", " else:\n", " other.append(x)\n", " _party_map[x] = 'Other' # ...\n", " else:\n", " other.append(x)\n", " _party_map[x] = 'Other' # ...\n", " \n", "\n", "print(len(repub), repub)\n", "print(len(democ), democ)\n", "print(len(other), other)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "display(df['1949':]['party'].value_counts())\n", "display(df[df['party']=='Liberal'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# print(df2[df2['state'] == 'NE'][['type', 'party','first','last']].sort().to_string())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print('# party')\n", "print(df[df['state'] == 'NE']['party'].value_counts(normalize=True))\n", "print('# type')\n", "print(df[df['state'] == 'NE']['type'].value_counts())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "df['two_party_fail'] = df['party'].apply(lambda x: _party_map.get(x))\n", "display( df['two_party_fail'].value_counts() )\n", "display( df['two_party_fail'].value_counts(normalize=True) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# objective: draw chart with per-year, per-two-party-counts\n", "# group by year\n", "# count factors\n", "\n", "def start_year(x):\n", " return x.year - (x.year % 2)\n", "\n", "grouper = df.groupby([start_year, 'two_party_fail'])\n", "\n", "whoa = grouper.aggregate({'two_party_fail':len}).unstack()\n", "\n", "display(whoa.plot())\n", "display(whoa.head())\n", "display(whoa.tail())\n", "\n", "#df.pivot_table(values='two_party_fail', cols=['start'], aggfunc=len)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "grouper = df.groupby([start_year, 'gender'])\n", "\n", "whoa = grouper.aggregate({'gender':len}).unstack()\n", "display(whoa.plot())\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Q. how are these misleading / maybe not as helpful as they could be?\n", "# 1. they count by start year, so they don't show the state at any given time\n", "# to show the state at any given time would require\n", "# a 'currently_serving' function\n", "# which, one might think could take into account standard terms/elections\n", "# as appropriate for rep/sen,\n", "# but there are special cases in mid-stream\n", "\n", "# 2. they do not stratify by rep/sen; the counts are lumped together\n", "# 'share_y' split by 'type' might be helpful\n", "\n", "# ... how many hours would it take to draw these in [spreadsheet tool]\n", "# only to realize that you have no idea what \n", "# 'settings' were used to create a (very beautiful) chart?\n", "# ... python tools for visual studio now support \n", "# something like `ipython --pylab=inline/qt`\n", "# ... i work on various platforms, so that's not an option for me\n", "# ... not sure what sort of configuration is required to get\n", "# anaconda ce working with this ide\n", "# ... ipython qt, ipython notebook\n", "# ... spyder ide\n", "# ... you can run these as scheduled jobs which generate online charts,\n", "# but then, still, without the source,\n", "# what smoke are you\n", "# ... \"you can get a good look at a t-bone steak by\"" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }