{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Accessing the profiling raw data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:16.473981Z",
     "start_time": "2022-09-21T08:58:14.299137Z"
    }
   },
   "outputs": [],
   "source": [
    "import tslumen\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:16.481424Z",
     "start_time": "2022-09-21T08:58:16.476617Z"
    },
    "nbsphinx": "hidden"
   },
   "outputs": [],
   "source": [
    "# patch it\n",
    "_read_csv = pd.read_csv\n",
    "pd.read_csv = lambda fpath, *args, **kwargs: _read_csv(fpath.split('/')[-1], *args, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:17.219685Z",
     "start_time": "2022-09-21T08:58:16.486825Z"
    }
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv(\n",
    "    'https://datahub.io/core/gdp/r/gdp.csv', \n",
    "    parse_dates=[2], \n",
    "    date_parser=lambda dt: pd.to_datetime(f'{dt}-12-31'),\n",
    ")\n",
    "df = df[df['Country Code'].isin(['HIC', 'MIC', 'LIC'])]\\\n",
    "    .set_index(['Year', 'Country Code'])['Value'].unstack(1)\n",
    "meta = {\n",
    "    'frame': {\n",
    "        'Description': 'Country, regional and world GDP in current US Dollars ($). Regional means collections of countries e.g. Europe & Central Asia.',\n",
    "        'Source': '<a href=\"https://datahub.io/core/gold-prices\">DataHub</a>',\n",
    "    },\n",
    "    'series': {\n",
    "        'HIC': 'High income GDP in current USD',\n",
    "        'MIC': 'Middle income GDP in current USD',\n",
    "        'LIC': 'Low income GDP in current USD',\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:24.277531Z",
     "start_time": "2022-09-21T08:58:17.222167Z"
    }
   },
   "outputs": [],
   "source": [
    "profile = tslumen.DefaultProfiler().profile(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The profiling results are stored in a `dataclass` containing summary information like name, start and end timestamps and errors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:24.308734Z",
     "start_time": "2022-09-21T08:58:24.281248Z"
    }
   },
   "outputs": [],
   "source": [
    "print(f'''\\\n",
    "name:         {profile.name}\n",
    "start:        {profile.start}\n",
    "end:          {profile.end}\n",
    "config:       dictionary with {len(profile.result.config)} elements\n",
    "success:      {profile.success}\n",
    "warnings:     {profile.warnings}\n",
    "exception:    {profile.exception}\n",
    "exec_details: ''')\n",
    "display(profile.result.exec_details)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Result is pretty printed when using an IPython environment like Jupyter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:25.101622Z",
     "start_time": "2022-09-21T08:58:24.311072Z"
    }
   },
   "outputs": [],
   "source": [
    "profile"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-04-01T11:12:49.008698Z",
     "start_time": "2021-04-01T11:12:49.003947Z"
    }
   },
   "source": [
    "The results of each profiling function are stored under the `frame` and `series` attributes, depending on whether the function works at DataFrame (looks at multiple series together) or Series (looks at a single time series) level.\n",
    "  * `frame` is a flat dictionary indexed by the profiling function name\n",
    "  * `series` is a 2-level deep dictionary, first indexed by the series (column) name, then by profiling function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:25.107371Z",
     "start_time": "2022-09-21T08:58:25.103151Z"
    }
   },
   "outputs": [],
   "source": [
    "profile.result.frame.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:25.113130Z",
     "start_time": "2022-09-21T08:58:25.109637Z"
    }
   },
   "outputs": [],
   "source": [
    "profile.result.series.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:25.122530Z",
     "start_time": "2022-09-21T08:58:25.118669Z"
    }
   },
   "outputs": [],
   "source": [
    "profile.result.series['HIC'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:25.128498Z",
     "start_time": "2022-09-21T08:58:25.124226Z"
    }
   },
   "outputs": [],
   "source": [
    "profile.result.frame['sz_total']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:25.137024Z",
     "start_time": "2022-09-21T08:58:25.129969Z"
    }
   },
   "outputs": [],
   "source": [
    "profile.result.frame['corr_kendall']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-09-21T08:58:25.149557Z",
     "start_time": "2022-09-21T08:58:25.138520Z"
    }
   },
   "outputs": [],
   "source": [
    "profile.result.series['HIC']['acf']"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Edit Metadata",
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": false,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
