{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reports from Court Records"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from __future__ import division, print_function\n",
    "import pandas as pd\n",
    "import sys\n",
    "import calendar\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import plotly.plotly as py\n",
    "from plotly.offline import init_notebook_mode, iplot\n",
    "import plotly.graph_objs as go\n",
    "import numpy as np\n",
    "init_notebook_mode(connected=True)\n",
    "\n",
    "\n",
    "def get_calendar_df():\n",
    "    '''Read the parsed court records from a file'''\n",
    "    df = pd.read_csv('calendars_parsed.csv')\n",
    "    df['Date'] = pd.to_datetime(df['Date'], errors='coerce')\n",
    "    df['Title'] = df['Title'].astype(str)\n",
    "    df['day_of_week'] = df['Date'].dt.day_name()\n",
    "    df['month'] = df['Date'].dt.month_name()\n",
    "    df['day_of_month'] = df['Date'].dt.day\n",
    "    df['year'] = df['Date'].dt.year\n",
    "    return df[df.Status == 'Confirmed']\n",
    "\n",
    "\n",
    "def filter_df(df, date_min=None, date_max=None, day_of_week=None,\n",
    "              day_of_month=None, month=None, year=None,\n",
    "              case_number=None, case_title=None, event=None, court=None):\n",
    "    '''Filter a dataframe based on specified criteria'''\n",
    "    if date_min is not None:\n",
    "        df = df.loc[df['Date'] >= date_min]\n",
    "    if date_max is not None:\n",
    "        df = df.loc[df['Date'] <= date_max]\n",
    "    if day_of_week is not None:\n",
    "        df = df[df['day_of_week'].str.lower().str.contains(day_of_week.lower())]\n",
    "    if day_of_month is not None:\n",
    "        df = df[df['day_of_month'] == day_of_month]\n",
    "    if month is not None:\n",
    "        df = df[df['month'].str.lower().str.contains(month.lower())]\n",
    "    if year is not None:\n",
    "        df = df[df['year'] == year]\n",
    "    if case_number is not None:\n",
    "        df = df[df['Case Number'].str.lower().str.contains(case_number.lower())]\n",
    "    if case_title is not None:\n",
    "        df = df[df['Title'].str.lower().str.contains(case_title.lower())]\n",
    "    if event is not None:\n",
    "        df = df[df['Event'].str.lower().str.contains(event.lower())]\n",
    "    if court is not None:\n",
    "        df = df[df['Court'].str.lower().str.contains(court.lower())]\n",
    "    return df\n",
    "\n",
    "\n",
    "def number_by_month(df):\n",
    "    '''Return a count of the number of rows of the dataframe by month'''\n",
    "    df_by_month = pd.DataFrame(columns=['Month', 'Count'])\n",
    "    min_year = min(df.year)\n",
    "    max_year = max(df.year)\n",
    "    for year in range(min_year, max_year+1):\n",
    "        for month_number in range(1, 13):\n",
    "            month = calendar.month_name[month_number]\n",
    "            count = rows(filter_df(df, month=month, year=year))\n",
    "            df_by_month = df_by_month.append(\n",
    "                {'Month': month + ' ' + str(year), 'Count': count},\n",
    "                ignore_index=True)\n",
    "    first_nonzero_index = df_by_month.Count.replace(0, np.nan).first_valid_index()\n",
    "    last_nonzero_index = df_by_month.Count.replace(0, np.nan).last_valid_index()\n",
    "    return df_by_month.iloc[first_nonzero_index:last_nonzero_index+1]\n",
    "\n",
    "\n",
    "def number_by_day_of_week(df):\n",
    "    '''Return a count of the number of rows of the dataframe by day of week'''\n",
    "    df_by_day_of_week = pd.DataFrame(columns=['Day of Week', 'Count'])\n",
    "    for day_number in range(1, 8):\n",
    "        day_name = calendar.day_name[day_number]\n",
    "        count = rows(filter_df(df, day_of_week=day_name))\n",
    "        df_by_day_of_week = df_by_day_of_week.append({'Day of Week': day_name, 'Count': count}, ignore_index=True)\n",
    "    return df_by_day_of_week\n",
    "\n",
    "\n",
    "def number_by_day_of_month(df):\n",
    "    '''Return a count of the number of rows of the dataframe by day of month'''\n",
    "    df_by_day_of_month = pd.DataFrame(columns=['Day of Month', 'Count'])\n",
    "    for day_number in range(1, 32):\n",
    "        count = rows(filter_df(df, day_of_month=day_number))\n",
    "        df_by_day_of_month = df_by_day_of_month.append({'Day of Month': day_number, 'Count': count}, ignore_index=True)\n",
    "    return df_by_day_of_month\n",
    "\n",
    "\n",
    "def rows(df):\n",
    "    '''Convenience function to return the number of rows of a dataframe'''\n",
    "    return len(df.index)\n",
    "\n",
    "\n",
    "def bar_chart(labels, counts, xlabel=None, ylabel=None):\n",
    "    '''Make a bar chart'''\n",
    "    percent = [str(round(100*count/sum(counts),1)) + '%' for count in counts]\n",
    "    data = [go.Bar(\n",
    "            x=labels,\n",
    "            y=counts,\n",
    "            text=percent,\n",
    "            textposition = 'auto',\n",
    "            opacity=0.6\n",
    "        )]\n",
    "    layout = go.Layout(xaxis=dict(title=xlabel), yaxis=dict(title=ylabel))\n",
    "    fig = go.Figure(data=data, layout=layout)\n",
    "    iplot(fig)\n",
    "\n",
    "\n",
    "def line_plot(labels, counts, xlabel=None, ylabel=None):\n",
    "    '''Make a bar chart'''\n",
    "    data = [go.Scatter(\n",
    "            x=labels,\n",
    "            y=counts\n",
    "        )]\n",
    "    layout = go.Layout(xaxis=dict(title=xlabel),\n",
    "                       yaxis=dict(title=ylabel, range=[0, 1.1*max(counts)]))\n",
    "    fig = go.Figure(data=data, layout=layout)\n",
    "    iplot(fig)\n",
    "\n",
    "\n",
    "df = get_calendar_df()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cases broken down by type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "opacity": 0.6,
         "text": [
          "25.0%",
          "9.1%",
          "18.4%",
          "29.3%",
          "10.8%",
          "7.3%"
         ],
         "textposition": "auto",
         "type": "bar",
         "uid": "416afa2e-c85c-11e9-beab-6c4008929994",
         "x": [
          "Small Claims",
          "Civil Collection",
          "Misdemeanors",
          "Felonies",
          "Family",
          "Other"
         ],
         "y": [
          5244,
          1913,
          3868,
          6155,
          2264,
          1539
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "title": "Number of hearings"
        }
       }
      },
      "text/html": [
       "<div id=\"3e132d8b-3cf4-4be1-8ab6-96db737d4bcd\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"3e132d8b-3cf4-4be1-8ab6-96db737d4bcd\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [5244, 1913, 3868, 6155, 2264, 1539], \"text\": [\"25.0%\", \"9.1%\", \"18.4%\", \"29.3%\", \"10.8%\", \"7.3%\"], \"x\": [\"Small Claims\", \"Civil Collection\", \"Misdemeanors\", \"Felonies\", \"Family\", \"Other\"], \"textposition\": \"auto\", \"uid\": \"416afa2e-c85c-11e9-beab-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"3e132d8b-3cf4-4be1-8ab6-96db737d4bcd\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"3e132d8b-3cf4-4be1-8ab6-96db737d4bcd\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [5244, 1913, 3868, 6155, 2264, 1539], \"text\": [\"25.0%\", \"9.1%\", \"18.4%\", \"29.3%\", \"10.8%\", \"7.3%\"], \"x\": [\"Small Claims\", \"Civil Collection\", \"Misdemeanors\", \"Felonies\", \"Family\", \"Other\"], \"textposition\": \"auto\", \"uid\": \"416afa2e-c85c-11e9-beab-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = ['Small Claims', 'Civil Collection', 'Misdemeanors', 'Felonies',\n",
    "          'Family', 'Other']\n",
    "counts = [0]*6\n",
    "counts[0] = rows(filter_df(df, case_number='SC'))\n",
    "counts[1] = rows(filter_df(df, case_number='CC'))\n",
    "counts[2] = rows(filter_df(df, case_number='CM'))\n",
    "counts[3] = rows(filter_df(df, case_number='-F1-|-F2-|-F3-|-F4-|-F5-|-F6-'))\n",
    "counts[4] = rows(filter_df(df, case_number='-AD-|-RS-|-JP-|-DC-|-DR-|-GU-'))\n",
    "counts[5] = rows(df) - sum(counts[0:5])\n",
    "bar_chart(labels, counts, ylabel='Number of hearings')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criminal cases by type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "opacity": 0.6,
         "text": [
          "0.8%",
          "1.6%",
          "2.4%",
          "2.7%",
          "8.5%",
          "43.2%",
          "37.1%",
          "3.8%"
         ],
         "textposition": "auto",
         "type": "bar",
         "uid": "44d45ec8-c85c-11e9-83f3-6c4008929994",
         "x": [
          "Felony 1",
          "Felony 2",
          "Felony 3",
          "Felony 4",
          "Felony 5",
          "Felony 6",
          "Misdemeanor",
          "Infraction"
         ],
         "y": [
          81,
          165,
          247,
          283,
          884,
          4495,
          3868,
          394
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "title": "Number of hearings"
        }
       }
      },
      "text/html": [
       "<div id=\"6cb11c00-602e-4e4f-b8db-ed8daff41b14\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"6cb11c00-602e-4e4f-b8db-ed8daff41b14\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [81, 165, 247, 283, 884, 4495, 3868, 394], \"text\": [\"0.8%\", \"1.6%\", \"2.4%\", \"2.7%\", \"8.5%\", \"43.2%\", \"37.1%\", \"3.8%\"], \"x\": [\"Felony 1\", \"Felony 2\", \"Felony 3\", \"Felony 4\", \"Felony 5\", \"Felony 6\", \"Misdemeanor\", \"Infraction\"], \"textposition\": \"auto\", \"uid\": \"44d45ec8-c85c-11e9-83f3-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"6cb11c00-602e-4e4f-b8db-ed8daff41b14\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"6cb11c00-602e-4e4f-b8db-ed8daff41b14\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [81, 165, 247, 283, 884, 4495, 3868, 394], \"text\": [\"0.8%\", \"1.6%\", \"2.4%\", \"2.7%\", \"8.5%\", \"43.2%\", \"37.1%\", \"3.8%\"], \"x\": [\"Felony 1\", \"Felony 2\", \"Felony 3\", \"Felony 4\", \"Felony 5\", \"Felony 6\", \"Misdemeanor\", \"Infraction\"], \"textposition\": \"auto\", \"uid\": \"44d45ec8-c85c-11e9-83f3-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = ['Felony 1', 'Felony 2', 'Felony 3', 'Felony 4', 'Felony 5', 'Felony 6', 'Misdemeanor', 'Infraction']\n",
    "counts = [0]*8\n",
    "df_criminal = filter_df(df, case_number='-CM-|-IF-|-F1-|-F2-|-F3-|-F4-|-F5-|-F6-')\n",
    "counts[0] = rows(filter_df(df_criminal, case_number='-F1-'))\n",
    "counts[1] = rows(filter_df(df_criminal, case_number='-F2-'))\n",
    "counts[2] = rows(filter_df(df_criminal, case_number='-F3-'))\n",
    "counts[3] = rows(filter_df(df_criminal, case_number='-F4-'))\n",
    "counts[4] = rows(filter_df(df_criminal, case_number='-F5-'))\n",
    "counts[5] = rows(filter_df(df_criminal, case_number='-F6-'))\n",
    "counts[6] = rows(filter_df(df_criminal, case_number='-CM-'))\n",
    "counts[7] = rows(filter_df(df_criminal, case_number='-IF-'))\n",
    "bar_chart(labels, counts, ylabel='Number of hearings')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Small claims cases by type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "opacity": 0.6,
         "text": [
          "11.4%",
          "9.3%",
          "27.4%",
          "1.9%",
          "50.0%"
         ],
         "textposition": "auto",
         "type": "bar",
         "uid": "482ce1da-c85c-11e9-af01-6c4008929994",
         "x": [
          "Eviction Hearing",
          "Damages Hearing",
          "Finance System of Richmond",
          "Natco Credit Union",
          "Other"
         ],
         "y": [
          1026,
          833,
          2455,
          172,
          4486
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "title": "Number of hearings"
        }
       }
      },
      "text/html": [
       "<div id=\"71101bed-f251-4a66-8252-1d147921c446\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"71101bed-f251-4a66-8252-1d147921c446\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [1026, 833, 2455, 172, 4486], \"text\": [\"11.4%\", \"9.3%\", \"27.4%\", \"1.9%\", \"50.0%\"], \"x\": [\"Eviction Hearing\", \"Damages Hearing\", \"Finance System of Richmond\", \"Natco Credit Union\", \"Other\"], \"textposition\": \"auto\", \"uid\": \"482ce1da-c85c-11e9-af01-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"71101bed-f251-4a66-8252-1d147921c446\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"71101bed-f251-4a66-8252-1d147921c446\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [1026, 833, 2455, 172, 4486], \"text\": [\"11.4%\", \"9.3%\", \"27.4%\", \"1.9%\", \"50.0%\"], \"x\": [\"Eviction Hearing\", \"Damages Hearing\", \"Finance System of Richmond\", \"Natco Credit Union\", \"Other\"], \"textposition\": \"auto\", \"uid\": \"482ce1da-c85c-11e9-af01-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = ['Eviction Hearing', 'Damages Hearing', 'Finance System of Richmond', 'Natco Credit Union', 'Other']\n",
    "df_sc = filter_df(df, case_number='-SC-')\n",
    "counts = [0]*5\n",
    "\n",
    "counts[0] = rows(filter_df(df_sc, event='eviction'))\n",
    "counts[1] = rows(filter_df(df_sc, event='damage'))\n",
    "counts[2] = rows(filter_df(df_sc, case_title='Fs V|Finance System'))\n",
    "counts[3] = rows(filter_df(df_sc, case_title='Natco'))\n",
    "counts[4] = sum(counts[0:4])\n",
    "\n",
    "bar_chart(labels, counts, ylabel='Number of hearings')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Civil collection cases by type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "opacity": 0.6,
         "text": [
          "11.1%",
          "17.5%",
          "11.6%",
          "2.2%",
          "1.6%",
          "6.0%",
          "50.0%"
         ],
         "textposition": "auto",
         "type": "bar",
         "uid": "4beafd70-c85c-11e9-9877-6c4008929994",
         "x": [
          "Reid Hospital",
          "Damages Hearing",
          "Finance System of Richmond",
          "Natco Credit Union",
          "Creditmax",
          "Capital One",
          "Other"
         ],
         "y": [
          271,
          426,
          284,
          54,
          39,
          146,
          1220
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "title": "Number of hearings"
        }
       }
      },
      "text/html": [
       "<div id=\"1987559c-12e3-4a99-b0f9-7388c8c52958\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"1987559c-12e3-4a99-b0f9-7388c8c52958\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [271, 426, 284, 54, 39, 146, 1220], \"text\": [\"11.1%\", \"17.5%\", \"11.6%\", \"2.2%\", \"1.6%\", \"6.0%\", \"50.0%\"], \"x\": [\"Reid Hospital\", \"Damages Hearing\", \"Finance System of Richmond\", \"Natco Credit Union\", \"Creditmax\", \"Capital One\", \"Other\"], \"textposition\": \"auto\", \"uid\": \"4beafd70-c85c-11e9-9877-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"1987559c-12e3-4a99-b0f9-7388c8c52958\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"1987559c-12e3-4a99-b0f9-7388c8c52958\", [{\"opacity\": 0.6, \"type\": \"bar\", \"y\": [271, 426, 284, 54, 39, 146, 1220], \"text\": [\"11.1%\", \"17.5%\", \"11.6%\", \"2.2%\", \"1.6%\", \"6.0%\", \"50.0%\"], \"x\": [\"Reid Hospital\", \"Damages Hearing\", \"Finance System of Richmond\", \"Natco Credit Union\", \"Creditmax\", \"Capital One\", \"Other\"], \"textposition\": \"auto\", \"uid\": \"4beafd70-c85c-11e9-9877-6c4008929994\"}], {\"yaxis\": {\"title\": \"Number of hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels = ['Reid Hospital', 'Damages Hearing', 'Finance System of Richmond',\n",
    "          'Natco Credit Union', 'Creditmax', 'Capital One', 'Other']\n",
    "df_cc = filter_df(df, case_number='-CC-')\n",
    "counts = [0]*7\n",
    "\n",
    "counts[0] = rows(filter_df(df_cc, case_title='Reid Hospital'))\n",
    "counts[1] = rows(filter_df(df_cc, event='damage'))\n",
    "counts[2] = rows(filter_df(df_cc, case_title='Fs V|Finance System'))\n",
    "counts[3] = rows(filter_df(df_cc, case_title='Natco'))\n",
    "counts[4] = rows(filter_df(df_cc, case_title='Creditmax'))\n",
    "counts[5] = rows(filter_df(df_cc, case_title='Capital One'))\n",
    "counts[6] = sum(counts[0:6])\n",
    "\n",
    "bar_chart(labels, counts, ylabel='Number of hearings')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Eviction hearings as a function of time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "type": "scatter",
         "uid": "4df353ba-c85c-11e9-9b5b-6c4008929994",
         "x": [
          "July 2018",
          "August 2018",
          "September 2018",
          "October 2018",
          "November 2018",
          "December 2018",
          "January 2019",
          "February 2019",
          "March 2019",
          "April 2019",
          "May 2019",
          "June 2019",
          "July 2019",
          "August 2019",
          "September 2019"
         ],
         "y": [
          44,
          83,
          41,
          63,
          70,
          55,
          94,
          58,
          63,
          81,
          85,
          74,
          113,
          58,
          45
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "range": [
          0,
          124.30000000000001
         ],
         "title": "Number of eviction hearings"
        }
       }
      },
      "text/html": [
       "<div id=\"c13a0c12-3b0a-4b76-b85c-557c40d4f5e3\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"c13a0c12-3b0a-4b76-b85c-557c40d4f5e3\", [{\"y\": [44, 83, 41, 63, 70, 55, 94, 58, 63, 81, 85, 74, 113, 58, 45], \"x\": [\"July 2018\", \"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\"], \"type\": \"scatter\", \"uid\": \"4df353ba-c85c-11e9-9b5b-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 124.30000000000001], \"title\": \"Number of eviction hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"c13a0c12-3b0a-4b76-b85c-557c40d4f5e3\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"c13a0c12-3b0a-4b76-b85c-557c40d4f5e3\", [{\"y\": [44, 83, 41, 63, 70, 55, 94, 58, 63, 81, 85, 74, 113, 58, 45], \"x\": [\"July 2018\", \"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\"], \"type\": \"scatter\", \"uid\": \"4df353ba-c85c-11e9-9b5b-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 124.30000000000001], \"title\": \"Number of eviction hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_evictions = filter_df(df, event='eviction')\n",
    "df_evictions_by_month = number_by_month(df_evictions)\n",
    "line_plot(df_evictions_by_month.Month,\n",
    "          df_evictions_by_month.Count,\n",
    "          xlabel=None,\n",
    "          ylabel='Number of eviction hearings')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Eviction-related damages hearings as a function of time for all landlords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "type": "scatter",
         "uid": "e03315f8-c85f-11e9-836f-6c4008929994",
         "x": [
          "August 2018",
          "September 2018",
          "October 2018",
          "November 2018",
          "December 2018",
          "January 2019",
          "February 2019",
          "March 2019",
          "April 2019",
          "May 2019",
          "June 2019",
          "July 2019",
          "August 2019",
          "September 2019",
          "October 2019"
         ],
         "y": [
          10,
          43,
          58,
          46,
          39,
          61,
          61,
          34,
          51,
          35,
          64,
          80,
          48,
          63,
          24
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "range": [
          0,
          88
         ],
         "title": "Number of eviction-related damages hearings"
        }
       }
      },
      "text/html": [
       "<div id=\"66aa6455-b710-4952-9df1-0e2a731f7de1\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"66aa6455-b710-4952-9df1-0e2a731f7de1\", [{\"y\": [10, 43, 58, 46, 39, 61, 61, 34, 51, 35, 64, 80, 48, 63, 24], \"x\": [\"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\", \"October 2019\"], \"type\": \"scatter\", \"uid\": \"e03315f8-c85f-11e9-836f-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 88.0], \"title\": \"Number of eviction-related damages hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"66aa6455-b710-4952-9df1-0e2a731f7de1\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"66aa6455-b710-4952-9df1-0e2a731f7de1\", [{\"y\": [10, 43, 58, 46, 39, 61, 61, 34, 51, 35, 64, 80, 48, 63, 24], \"x\": [\"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\", \"October 2019\"], \"type\": \"scatter\", \"uid\": \"e03315f8-c85f-11e9-836f-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 88.0], \"title\": \"Number of eviction-related damages hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_damages = filter_df(df, event='damages hearing')\n",
    "df_damages = df_damages[df_damages['Case Number'].isin(df_evictions['Case Number'])]\n",
    "df_damages_by_month = number_by_month(df_damages)\n",
    "line_plot(df_damages_by_month.Month,\n",
    "          df_damages_by_month.Count,\n",
    "          xlabel=None,\n",
    "          ylabel='Number of eviction-related damages hearings')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that damages hearings from evictions typically occur 30-45 days after a property is vacated."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fraction of overall eviction-related damages hearings over time initiated by K&R Hoosier Investments (Anderson Rentals) and KT Property Group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "type": "scatter",
         "uid": "2cde911a-c860-11e9-ae04-6c4008929994",
         "x": [
          "August 2018",
          "September 2018",
          "October 2018",
          "November 2018",
          "December 2018",
          "January 2019",
          "February 2019",
          "March 2019",
          "April 2019",
          "May 2019",
          "June 2019",
          "July 2019",
          "August 2019",
          "September 2019",
          "October 2019"
         ],
         "y": [
          0.7,
          0.2558139534883721,
          0.4482758620689655,
          0.41304347826086957,
          0.38461538461538464,
          0.5737704918032787,
          0.45901639344262296,
          0.38235294117647056,
          0.4117647058823529,
          0.3142857142857143,
          0.40625,
          0.5875,
          0.4375,
          0.5396825396825397,
          0.5
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "range": [
          0,
          0.77
         ],
         "title": "Fraction of total due to Anderson Rentals and KT Property Group"
        }
       }
      },
      "text/html": [
       "<div id=\"55bc5489-29a1-442f-b4dc-b88517e5ad48\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"55bc5489-29a1-442f-b4dc-b88517e5ad48\", [{\"y\": [0.7, 0.2558139534883721, 0.4482758620689655, 0.41304347826086957, 0.38461538461538464, 0.5737704918032787, 0.45901639344262296, 0.38235294117647056, 0.4117647058823529, 0.3142857142857143, 0.40625, 0.5875, 0.4375, 0.5396825396825397, 0.5], \"x\": [\"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\", \"October 2019\"], \"type\": \"scatter\", \"uid\": \"2cde911a-c860-11e9-ae04-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 0.77], \"title\": \"Fraction of total due to Anderson Rentals and KT Property Group\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"55bc5489-29a1-442f-b4dc-b88517e5ad48\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"55bc5489-29a1-442f-b4dc-b88517e5ad48\", [{\"y\": [0.7, 0.2558139534883721, 0.4482758620689655, 0.41304347826086957, 0.38461538461538464, 0.5737704918032787, 0.45901639344262296, 0.38235294117647056, 0.4117647058823529, 0.3142857142857143, 0.40625, 0.5875, 0.4375, 0.5396825396825397, 0.5], \"x\": [\"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\", \"October 2019\"], \"type\": \"scatter\", \"uid\": \"2cde911a-c860-11e9-ae04-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 0.77], \"title\": \"Fraction of total due to Anderson Rentals and KT Property Group\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_damages_filtered = filter_df(df, event='damages hearing', case_title='KT Property Group LLC|Hoosier Investment LLC')\n",
    "df_damages_filtered = df_damages_filtered[df_damages_filtered['Case Number'].isin(df_evictions['Case Number'])]\n",
    "df_damages_by_month_filtered = number_by_month(df_damages_filtered)\n",
    "line_plot(df_damages_by_month.Month,\n",
    "          df_damages_by_month_filtered.Count/df_damages_by_month.Count,\n",
    "          xlabel=None,\n",
    "          ylabel='Fraction of total due to Anderson Rentals and KT Property Group')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Felonies over time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "type": "scatter",
         "uid": "a949f60c-c85b-11e9-99f9-6c4008929994",
         "x": [
          "July 2018",
          "August 2018",
          "September 2018",
          "October 2018",
          "November 2018",
          "December 2018",
          "January 2019",
          "February 2019",
          "March 2019",
          "April 2019",
          "May 2019",
          "June 2019",
          "July 2019",
          "August 2019",
          "September 2019",
          "October 2019",
          "November 2019",
          "December 2019"
         ],
         "y": [
          278,
          396,
          246,
          377,
          365,
          230,
          440,
          348,
          338,
          435,
          390,
          343,
          209,
          81,
          6,
          2,
          1,
          1
         ]
        }
       ],
       "layout": {
        "yaxis": {
         "range": [
          0,
          484.00000000000006
         ],
         "title": "Number of felony hearings"
        }
       }
      },
      "text/html": [
       "<div id=\"2216fe1e-c3a0-462a-8a53-68c5438148d5\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"2216fe1e-c3a0-462a-8a53-68c5438148d5\", [{\"y\": [278, 396, 246, 377, 365, 230, 440, 348, 338, 435, 390, 343, 209, 81, 6, 2, 1, 1], \"x\": [\"July 2018\", \"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\", \"October 2019\", \"November 2019\", \"December 2019\"], \"type\": \"scatter\", \"uid\": \"a949f60c-c85b-11e9-99f9-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 484.00000000000006], \"title\": \"Number of felony hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"2216fe1e-c3a0-462a-8a53-68c5438148d5\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"2216fe1e-c3a0-462a-8a53-68c5438148d5\", [{\"y\": [278, 396, 246, 377, 365, 230, 440, 348, 338, 435, 390, 343, 209, 81, 6, 2, 1, 1], \"x\": [\"July 2018\", \"August 2018\", \"September 2018\", \"October 2018\", \"November 2018\", \"December 2018\", \"January 2019\", \"February 2019\", \"March 2019\", \"April 2019\", \"May 2019\", \"June 2019\", \"July 2019\", \"August 2019\", \"September 2019\", \"October 2019\", \"November 2019\", \"December 2019\"], \"type\": \"scatter\", \"uid\": \"a949f60c-c85b-11e9-99f9-6c4008929994\"}], {\"yaxis\": {\"range\": [0, 484.00000000000006], \"title\": \"Number of felony hearings\"}}, {\"linkText\": \"Export to plot.ly\", \"showLink\": true})});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_felony = filter_df(df, case_number='-F1-|-F2-|-F3-|-F4-|-F5-|-F6-')\n",
    "#print(df_felony.sort_values('Date'))\n",
    "df_felonies_by_month = number_by_month(df_felony)\n",
    "line_plot(df_felonies_by_month.Month,\n",
    "          df_felonies_by_month.Count,\n",
    "          xlabel=None,\n",
    "          ylabel='Number of felony hearings')"
   ]
  }
 ],
 "metadata": {
  "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}