{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "np.random.seed(12345)\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rc(\"figure\", figsize=(10, 6))\n",
    "PREVIOUS_MAX_ROWS = pd.options.display.max_rows\n",
    "pd.options.display.max_columns = 20\n",
    "pd.options.display.max_rows = 20\n",
    "pd.options.display.max_colwidth = 80\n",
    "np.set_printoptions(precision=4, suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "now = datetime.now()\n",
    "now\n",
    "now.year, now.month, now.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "delta = datetime(2011, 1, 7) - datetime(2008, 6, 24, 8, 15)\n",
    "delta\n",
    "delta.days\n",
    "delta.seconds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import timedelta\n",
    "start = datetime(2011, 1, 7)\n",
    "start + timedelta(12)\n",
    "start - 2 * timedelta(12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp = datetime(2011, 1, 3)\n",
    "str(stamp)\n",
    "stamp.strftime(\"%Y-%m-%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "value = \"2011-01-03\"\n",
    "datetime.strptime(value, \"%Y-%m-%d\")\n",
    "datestrs = [\"7/6/2011\", \"8/6/2011\"]\n",
    "[datetime.strptime(x, \"%m/%d/%Y\") for x in datestrs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "datestrs = [\"2011-07-06 12:00:00\", \"2011-08-06 00:00:00\"]\n",
    "pd.to_datetime(datestrs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "idx = pd.to_datetime(datestrs + [None])\n",
    "idx\n",
    "idx[2]\n",
    "pd.isna(idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = [datetime(2011, 1, 2), datetime(2011, 1, 5),\n",
    "         datetime(2011, 1, 7), datetime(2011, 1, 8),\n",
    "         datetime(2011, 1, 10), datetime(2011, 1, 12)]\n",
    "ts = pd.Series(np.random.standard_normal(6), index=dates)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts + ts[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.index.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp = ts.index[0]\n",
    "stamp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp = ts.index[2]\n",
    "ts[stamp]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts[\"2011-01-10\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "longer_ts = pd.Series(np.random.standard_normal(1000),\n",
    "                      index=pd.date_range(\"2000-01-01\", periods=1000))\n",
    "longer_ts\n",
    "longer_ts[\"2001\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "longer_ts[\"2001-05\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts[datetime(2011, 1, 7):]\n",
    "ts[datetime(2011, 1, 7):datetime(2011, 1, 10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts\n",
    "ts[\"2011-01-06\":\"2011-01-11\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.truncate(after=\"2011-01-09\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range(\"2000-01-01\", periods=100, freq=\"W-WED\")\n",
    "long_df = pd.DataFrame(np.random.standard_normal((100, 4)),\n",
    "                       index=dates,\n",
    "                       columns=[\"Colorado\", \"Texas\",\n",
    "                                \"New York\", \"Ohio\"])\n",
    "long_df.loc[\"2001-05\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.DatetimeIndex([\"2000-01-01\", \"2000-01-02\", \"2000-01-02\",\n",
    "                          \"2000-01-02\", \"2000-01-03\"])\n",
    "dup_ts = pd.Series(np.arange(5), index=dates)\n",
    "dup_ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "dup_ts.index.is_unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "dup_ts[\"2000-01-03\"]  # not duplicated\n",
    "dup_ts[\"2000-01-02\"]  # duplicated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped = dup_ts.groupby(level=0)\n",
    "grouped.mean()\n",
    "grouped.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts\n",
    "resampler = ts.resample(\"D\")\n",
    "resampler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pd.date_range(\"2012-04-01\", \"2012-06-01\")\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.date_range(start=\"2012-04-01\", periods=20)\n",
    "pd.date_range(end=\"2012-06-01\", periods=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.date_range(\"2000-01-01\", \"2000-12-01\", freq=\"BM\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.date_range(\"2012-05-02 12:56:31\", periods=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.date_range(\"2012-05-02 12:56:31\", periods=5, normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas.tseries.offsets import Hour, Minute\n",
    "hour = Hour()\n",
    "hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "four_hours = Hour(4)\n",
    "four_hours"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.date_range(\"2000-01-01\", \"2000-01-03 23:59\", freq=\"4H\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "Hour(2) + Minute(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.date_range(\"2000-01-01\", periods=10, freq=\"1h30min\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "monthly_dates = pd.date_range(\"2012-01-01\", \"2012-09-01\", freq=\"WOM-3FRI\")\n",
    "list(monthly_dates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = pd.Series(np.random.standard_normal(4),\n",
    "               index=pd.date_range(\"2000-01-01\", periods=4, freq=\"M\"))\n",
    "ts\n",
    "ts.shift(2)\n",
    "ts.shift(-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.shift(2, freq=\"M\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.shift(3, freq=\"D\")\n",
    "ts.shift(1, freq=\"90T\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas.tseries.offsets import Day, MonthEnd\n",
    "now = datetime(2011, 11, 17)\n",
    "now + 3 * Day()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "now + MonthEnd()\n",
    "now + MonthEnd(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "offset = MonthEnd()\n",
    "offset.rollforward(now)\n",
    "offset.rollback(now)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = pd.Series(np.random.standard_normal(20),\n",
    "               index=pd.date_range(\"2000-01-15\", periods=20, freq=\"4D\"))\n",
    "ts\n",
    "ts.groupby(MonthEnd().rollforward).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.resample(\"M\").mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pytz\n",
    "pytz.common_timezones[-5:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "tz = pytz.timezone(\"America/New_York\")\n",
    "tz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range(\"2012-03-09 09:30\", periods=6)\n",
    "ts = pd.Series(np.random.standard_normal(len(dates)), index=dates)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(ts.index.tz)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.date_range(\"2012-03-09 09:30\", periods=10, tz=\"UTC\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts\n",
    "ts_utc = ts.tz_localize(\"UTC\")\n",
    "ts_utc\n",
    "ts_utc.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts_utc.tz_convert(\"America/New_York\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts_eastern = ts.tz_localize(\"America/New_York\")\n",
    "ts_eastern.tz_convert(\"UTC\")\n",
    "ts_eastern.tz_convert(\"Europe/Berlin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.index.tz_localize(\"Asia/Shanghai\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp = pd.Timestamp(\"2011-03-12 04:00\")\n",
    "stamp_utc = stamp.tz_localize(\"utc\")\n",
    "stamp_utc.tz_convert(\"America/New_York\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp_moscow = pd.Timestamp(\"2011-03-12 04:00\", tz=\"Europe/Moscow\")\n",
    "stamp_moscow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp_utc.value\n",
    "stamp_utc.tz_convert(\"America/New_York\").value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp = pd.Timestamp(\"2012-03-11 01:30\", tz=\"US/Eastern\")\n",
    "stamp\n",
    "stamp + Hour()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp = pd.Timestamp(\"2012-11-04 00:30\", tz=\"US/Eastern\")\n",
    "stamp\n",
    "stamp + 2 * Hour()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range(\"2012-03-07 09:30\", periods=10, freq=\"B\")\n",
    "ts = pd.Series(np.random.standard_normal(len(dates)), index=dates)\n",
    "ts\n",
    "ts1 = ts[:7].tz_localize(\"Europe/London\")\n",
    "ts2 = ts1[2:].tz_convert(\"Europe/Moscow\")\n",
    "result = ts1 + ts2\n",
    "result.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pd.Period(\"2011\", freq=\"A-DEC\")\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "p + 5\n",
    "p - 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.Period(\"2014\", freq=\"A-DEC\") - p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "periods = pd.period_range(\"2000-01-01\", \"2000-06-30\", freq=\"M\")\n",
    "periods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.Series(np.random.standard_normal(6), index=periods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "values = [\"2001Q3\", \"2002Q2\", \"2003Q1\"]\n",
    "index = pd.PeriodIndex(values, freq=\"Q-DEC\")\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pd.Period(\"2011\", freq=\"A-DEC\")\n",
    "p\n",
    "p.asfreq(\"M\", how=\"start\")\n",
    "p.asfreq(\"M\", how=\"end\")\n",
    "p.asfreq(\"M\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pd.Period(\"2011\", freq=\"A-JUN\")\n",
    "p\n",
    "p.asfreq(\"M\", how=\"start\")\n",
    "p.asfreq(\"M\", how=\"end\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pd.Period(\"Aug-2011\", \"M\")\n",
    "p.asfreq(\"A-JUN\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "periods = pd.period_range(\"2006\", \"2009\", freq=\"A-DEC\")\n",
    "ts = pd.Series(np.random.standard_normal(len(periods)), index=periods)\n",
    "ts\n",
    "ts.asfreq(\"M\", how=\"start\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.asfreq(\"B\", how=\"end\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pd.Period(\"2012Q4\", freq=\"Q-JAN\")\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "p.asfreq(\"D\", how=\"start\")\n",
    "p.asfreq(\"D\", how=\"end\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "p4pm = (p.asfreq(\"B\", how=\"end\") - 1).asfreq(\"T\", how=\"start\") + 16 * 60\n",
    "p4pm\n",
    "p4pm.to_timestamp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "periods = pd.period_range(\"2011Q3\", \"2012Q4\", freq=\"Q-JAN\")\n",
    "ts = pd.Series(np.arange(len(periods)), index=periods)\n",
    "ts\n",
    "new_periods = (periods.asfreq(\"B\", \"end\") - 1).asfreq(\"H\", \"start\") + 16\n",
    "ts.index = new_periods.to_timestamp()\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range(\"2000-01-01\", periods=3, freq=\"M\")\n",
    "ts = pd.Series(np.random.standard_normal(3), index=dates)\n",
    "ts\n",
    "pts = ts.to_period()\n",
    "pts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range(\"2000-01-29\", periods=6)\n",
    "ts2 = pd.Series(np.random.standard_normal(6), index=dates)\n",
    "ts2\n",
    "ts2.to_period(\"M\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "pts = ts2.to_period()\n",
    "pts\n",
    "pts.to_timestamp(how=\"end\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"examples/macrodata.csv\")\n",
    "data.head(5)\n",
    "data[\"year\"]\n",
    "data[\"quarter\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pd.PeriodIndex(year=data[\"year\"], quarter=data[\"quarter\"],\n",
    "                       freq=\"Q-DEC\")\n",
    "index\n",
    "data.index = index\n",
    "data[\"infl\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range(\"2000-01-01\", periods=100)\n",
    "ts = pd.Series(np.random.standard_normal(len(dates)), index=dates)\n",
    "ts\n",
    "ts.resample(\"M\").mean()\n",
    "ts.resample(\"M\", kind=\"period\").mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range(\"2000-01-01\", periods=12, freq=\"T\")\n",
    "ts = pd.Series(np.arange(len(dates)), index=dates)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.resample(\"5min\").sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.resample(\"5min\", closed=\"right\").sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.resample(\"5min\", closed=\"right\", label=\"right\").sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas.tseries.frequencies import to_offset\n",
    "result = ts.resample(\"5min\", closed=\"right\", label=\"right\").sum()\n",
    "result.index = result.index + to_offset(\"-1s\")\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = pd.Series(np.random.permutation(np.arange(len(dates))), index=dates)\n",
    "ts.resample(\"5min\").ohlc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "frame = pd.DataFrame(np.random.standard_normal((2, 4)),\n",
    "                     index=pd.date_range(\"2000-01-01\", periods=2,\n",
    "                                         freq=\"W-WED\"),\n",
    "                     columns=[\"Colorado\", \"Texas\", \"New York\", \"Ohio\"])\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_daily = frame.resample(\"D\").asfreq()\n",
    "df_daily"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "frame.resample(\"D\").ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "frame.resample(\"D\").ffill(limit=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "frame.resample(\"W-THU\").ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "frame = pd.DataFrame(np.random.standard_normal((24, 4)),\n",
    "                     index=pd.period_range(\"1-2000\", \"12-2001\",\n",
    "                                           freq=\"M\"),\n",
    "                     columns=[\"Colorado\", \"Texas\", \"New York\", \"Ohio\"])\n",
    "frame.head()\n",
    "annual_frame = frame.resample(\"A-DEC\").mean()\n",
    "annual_frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Q-DEC: Quarterly, year ending in December\n",
    "annual_frame.resample(\"Q-DEC\").ffill()\n",
    "annual_frame.resample(\"Q-DEC\", convention=\"end\").asfreq()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "annual_frame.resample(\"Q-MAR\").ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 15\n",
    "times = pd.date_range(\"2017-05-20 00:00\", freq=\"1min\", periods=N)\n",
    "df = pd.DataFrame({\"time\": times,\n",
    "                   \"value\": np.arange(N)})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.set_index(\"time\").resample(\"5min\").count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = pd.DataFrame({\"time\": times.repeat(3),\n",
    "                    \"key\": np.tile([\"a\", \"b\", \"c\"], N),\n",
    "                    \"value\": np.arange(N * 3.)})\n",
    "df2.head(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_key = pd.Grouper(freq=\"5min\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "resampled = (df2.set_index(\"time\")\n",
    "             .groupby([\"key\", time_key])\n",
    "             .sum())\n",
    "resampled\n",
    "resampled.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "close_px_all = pd.read_csv(\"examples/stock_px.csv\",\n",
    "                           parse_dates=True, index_col=0)\n",
    "close_px = close_px_all[[\"AAPL\", \"MSFT\", \"XOM\"]]\n",
    "close_px = close_px.resample(\"B\").ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "close_px[\"AAPL\"].plot()\n",
    "close_px[\"AAPL\"].rolling(250).mean().plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "std250 = close_px[\"AAPL\"].pct_change().rolling(250, min_periods=10).std()\n",
    "std250[5:12]\n",
    "std250.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "expanding_mean = std250.expanding().mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.style.use('grayscale')\n",
    "close_px.rolling(60).mean().plot(logy=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "close_px.rolling(\"20D\").mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "aapl_px = close_px[\"AAPL\"][\"2006\":\"2007\"]\n",
    "\n",
    "ma30 = aapl_px.rolling(30, min_periods=20).mean()\n",
    "ewma30 = aapl_px.ewm(span=30).mean()\n",
    "\n",
    "aapl_px.plot(style=\"k-\", label=\"Price\")\n",
    "ma30.plot(style=\"k--\", label=\"Simple Moving Avg\")\n",
    "ewma30.plot(style=\"k-\", label=\"EW MA\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "spx_px = close_px_all[\"SPX\"]\n",
    "spx_rets = spx_px.pct_change()\n",
    "returns = close_px.pct_change()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "corr = returns[\"AAPL\"].rolling(125, min_periods=100).corr(spx_rets)\n",
    "corr.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "corr = returns.rolling(125, min_periods=100).corr(spx_rets)\n",
    "corr.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import percentileofscore\n",
    "def score_at_2percent(x):\n",
    "    return percentileofscore(x, 0.02)\n",
    "\n",
    "result = returns[\"AAPL\"].rolling(250).apply(score_at_2percent)\n",
    "result.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.display.max_rows = PREVIOUS_MAX_ROWS"
   ]
  }
 ],
 "metadata": {
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}