{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "notes"
    },
    "tags": [
     "setup"
    ]
   },
   "source": [
    "(c) 2016 - present. Enplus Advisors, Inc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": [
     "setup"
    ]
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": [
     "setup"
    ]
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame({\n",
    "    'ticker': ['AAPL', 'AAPL', 'MSFT', 'IBM', 'YHOO'],\n",
    "    'date': ['2015-12-30', '2015-12-31', '2015-12-30', '2015-12-30', '2015-12-30'],\n",
    "    'open': [426.23, 427.81, 42.3, 101.65, 35.53]\n",
    "})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "source": [
    "**Exercise:**\n",
    "\n",
    "* Select the `open` column as a `Series` using attribute lookup\n",
    "* Select the `open` column as a `Series` using `dict`-style lookup\n",
    "* Select the `date` column as a `DataFrame`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "res1a = df.open\n",
    "res1b = df['open']\n",
    "res1c = df[['open']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "source": [
    "**Exercise:**\n",
    "\n",
    "* Select all rows with the `AAPL` ticker and the `date` and `open`\n",
    "  columns.\n",
    "* Assign to the variable `df1` a new `DataFrame` with `ticker` as\n",
    "  the index.\n",
    "* Assign to the variable `df2` a new `DataFrame` with `date` as\n",
    "  the index. Create this `DataFrame` from `df1` with a single\n",
    "  statement.\n",
    "* Sort `df2` by the index values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "res2a = df.loc[df.ticker == 'AAPL', ['date', 'open']]\n",
    "df1 = df.set_index('ticker')\n",
    "df2 = df1.reset_index().set_index('date')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2_sorted = df2.sort_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "exercise"
    ]
   },
   "source": [
    "**Exercise:**\n",
    "\n",
    "* Create a copy of `df` called `df3`. Add a new column of `NaNs` \n",
    "  to `df3` called `close`. Assign `close` the same value as `open`\n",
    "  for all `open` values greater than 100.\n",
    "* Sort `df3` by its `close` values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ticker</th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AAPL</td>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>426.23</td>\n",
       "      <td>426.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AAPL</td>\n",
       "      <td>2015-12-31</td>\n",
       "      <td>427.81</td>\n",
       "      <td>427.81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MSFT</td>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>42.30</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>IBM</td>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>101.65</td>\n",
       "      <td>101.65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>YHOO</td>\n",
       "      <td>2015-12-30</td>\n",
       "      <td>35.53</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  ticker        date    open   close\n",
       "0   AAPL  2015-12-30  426.23  426.23\n",
       "1   AAPL  2015-12-31  427.81  427.81\n",
       "2   MSFT  2015-12-30   42.30     NaN\n",
       "3    IBM  2015-12-30  101.65  101.65\n",
       "4   YHOO  2015-12-30   35.53     NaN"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = df.copy()\n",
    "\n",
    "# this could be skipped from a functional standpoint, though\n",
    "# the instructions say to do it\n",
    "df3['close'] = np.nan \n",
    "\n",
    "gt100 = df3.open[df3.open > 100]\n",
    "df3.close = gt100 # you can use dot syntax b/c `close` already exists\n",
    "df3"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}