{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pandasreporter as pr\n",
    "\n",
    "\n",
    "# B17001, Poverty Status by Sex by Age\n",
    "b17001 = pr.get_dataframe('B17001', '140', '05000US06073', cache=True).ct_columns\n",
    "# B17024, Age by Ratio of Income to Poverty Level\n",
    "b17024 = pr.get_dataframe('B17024', '140', '05000US06073', cache=True).ct_columns\n",
    "# B17017, Poverty Status by Household Type by Age of Householder\n",
    "b17017 = pr.get_dataframe('B17017', '140', '05000US06073', cache=True).ct_columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# B17001 Poverty Status by Sex by Age\n",
    "\n",
    "For the [Poverty Status by Sex by Age](https://censusreporter.org/tables/B17001/) we'll select the columns for male and female, below poverty, 65 and older. \n",
    "\n",
    "**NOTE** if you want to get seniors of a particular race, use table `C17001a-g`, condensed race iterations. The 'C' tables have fewer age ranges, but there is no 'C' table for all races: There is a `C17001a` for Whites, a condensed version of `B17001a`, but there is no `C17001` for a condensed version of `B17001`\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['B17001015 Total Income in the past 12 months below poverty level Male 65 to 74 years',\n",
       " 'Margins for B17001015 Total Income in the past 12 months below poverty level Male 65 to 74 years',\n",
       " 'B17001016 Total Income in the past 12 months below poverty level Male 75 years and over',\n",
       " 'Margins for B17001016 Total Income in the past 12 months below poverty level Male 75 years and over',\n",
       " 'B17001029 Total Income in the past 12 months below poverty level Female 65 to 74 years',\n",
       " 'Margins for B17001029 Total Income in the past 12 months below poverty level Female 65 to 74 years',\n",
       " 'B17001030 Total Income in the past 12 months below poverty level Female 75 years and over',\n",
       " 'Margins for B17001030 Total Income in the past 12 months below poverty level Female 75 years and over',\n",
       " 'B17001044 Total Income in the past 12 months at or above poverty level Male 65 to 74 years',\n",
       " 'Margins for B17001044 Total Income in the past 12 months at or above poverty level Male 65 to 74 years',\n",
       " 'B17001045 Total Income in the past 12 months at or above poverty level Male 75 years and over',\n",
       " 'Margins for B17001045 Total Income in the past 12 months at or above poverty level Male 75 years and over',\n",
       " 'B17001058 Total Income in the past 12 months at or above poverty level Female 65 to 74 years',\n",
       " 'Margins for B17001058 Total Income in the past 12 months at or above poverty level Female 65 to 74 years',\n",
       " 'B17001059 Total Income in the past 12 months at or above poverty level Female 75 years and over',\n",
       " 'Margins for B17001059 Total Income in the past 12 months at or above poverty level Female 75 years and over']"
      ]
     },
     "execution_count": 2,
     "output_type": "execute_result",
     "metadata": {}
    }
   ],
   "source": [
    "[e for e in b17001.columns if '65 to 74' in str(e) or '75 years' in str(e) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>geoid</th>\n",
       "      <th>B17001015 Total Income in the past 12 months below poverty level Male 65 to 74 years</th>\n",
       "      <th>Margins for B17001015 Total Income in the past 12 months below poverty level Male 65 to 74 years</th>\n",
       "      <th>B17001016 Total Income in the past 12 months below poverty level Male 75 years and over</th>\n",
       "      <th>Margins for B17001016 Total Income in the past 12 months below poverty level Male 75 years and over</th>\n",
       "      <th>B17001029 Total Income in the past 12 months below poverty level Female 65 to 74 years</th>\n",
       "      <th>Margins for B17001029 Total Income in the past 12 months below poverty level Female 65 to 74 years</th>\n",
       "      <th>B17001030 Total Income in the past 12 months below poverty level Female 75 years and over</th>\n",
       "      <th>Margins for B17001030 Total Income in the past 12 months below poverty level Female 75 years and over</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14000US06073004501</td>\n",
       "      <td>10.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14000US06073019803</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>14000US06073006000</td>\n",
       "      <td>18.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14000US06073008364</td>\n",
       "      <td>0.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14000US06073008507</td>\n",
       "      <td>0.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>41.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "output_type": "execute_result",
     "metadata": {}
    }
   ],
   "source": [
    "# Now create a subset dataframe with just the columns we need. \n",
    "b17001s = b17001[['geoid', 'B17001015', 'B17001016','B17001029','B17001030']]\n",
    "b17001s.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Senior poverty rates\n",
    "\n",
    "Creating the sums for the senior below poverty rates at the tract level is easy, but there is a *serious problem* with the results: the numbers are completely unstable. The minimum RSE is 22%, and the median is about 60%. These are useless results. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     576.000000\n",
       "mean       87.621218\n",
       "std       156.710591\n",
       "min        22.150407\n",
       "25%        43.645038\n",
       "50%        58.919310\n",
       "75%        82.136436\n",
       "max      1806.402183\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "output_type": "execute_result",
     "metadata": {}
    }
   ],
   "source": [
    "b17001_65mf = pr.CensusDataFrame()\n",
    "b17001_65mf['geoid'] = b17001['geoid']\n",
    "b17001_65mf['poverty_65'], b17001_65mf['poverty_65_m90'] = b17001.sum_m('B17001015', 'B17001016','B17001029','B17001030')\n",
    "b17001_65mf.add_rse('poverty_65')\n",
    "b17001_65mf.poverty_65_rse.replace([np.inf, -np.inf], np.nan).dropna().describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}