{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import json\n",
    "import numpy as np\n",
    "from dcf.dcf_widget import DCFWidget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('./examples/data/2014-01-citibike-tripdata.csv')\n",
    "df2 = pd.read_csv('./examples/data/yellow_tripdata_2021-02.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = DCFWidget(df=df2)\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transformed_df = w.dcf_transform(w.commands, w.df)\n",
    "w.transformed_df = json.loads(transformed_df.to_json(orient='table', indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w.commands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w.generated_py_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w.generated_py_code = w.dcf_to_py_core(w.commands[1:])\n",
    "w.generated_py_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dcf.all_transforms import Transform\n",
    "from dcf.lispy import s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@w.add_command\n",
    "class GroupBy2(Transform):\n",
    "    command_default = [s(\"groupby2\"), s('df'), 'col', {}]\n",
    "    command_pattern = [[3, 'colMap', 'colEnum', ['null', 'sum', 'mean', 'median', 'count']]]\n",
    "    @staticmethod \n",
    "    def transform(df, col, col_spec):\n",
    "        grps = df.groupby(col)\n",
    "        df_contents = {}\n",
    "        for k, v in col_spec.items():\n",
    "            if v == \"sum\":\n",
    "                df_contents[k] = grps[k].apply(lambda x: x.sum())\n",
    "            elif v == \"mean\":\n",
    "                df_contents[k] = grps[k].apply(lambda x: x.mean())\n",
    "            elif v == \"median\":\n",
    "                df_contents[k] = grps[k].apply(lambda x: x.median())\n",
    "            elif v == \"count\":\n",
    "                df_contents[k] = grps[k].apply(lambda x: x.count())\n",
    "        return pd.DataFrame(df_contents)\n",
    "\n",
    "    #test_df = group_df\n",
    "    #test_sequence = [s(\"groupby2\"), s('df'), 'c', dict(a='sum', b='mean')]\n",
    "    test_output = pd.DataFrame(\n",
    "        {'a':[100, 110], 'b':[2.5, 5.5]},\n",
    "        index=['q','w'])\n",
    "\n",
    "    @staticmethod \n",
    "    def transform_to_py(df, col, col_spec):\n",
    "        commands = [\n",
    "            \"    grps = df.groupby('%s')\" % col,\n",
    "            \"    df_contents = {}\"\n",
    "        ]\n",
    "        for k, v in col_spec.items():\n",
    "            if v == \"sum\":\n",
    "                commands.append(\"    paddydf_contents['%s'] = grps['%s'].apply(lambda x: x.sum())\" % (k, k))\n",
    "            elif v == \"mean\":\n",
    "                commands.append(\"    df_contents['%s'] = grps['%s'].apply(lambda x: x.mean())\" % (k, k))\n",
    "            elif v == \"median\":\n",
    "                commands.append(\"    df_contents['%s'] = grps['%s'].apply(lambda x: x.median())\" % (k, k))\n",
    "            elif v == \"count\":\n",
    "                commands.append(\"    df_contents['%s'] = grps['%s'].apply(lambda x: x.count())\" % (k, k))\n",
    "        #print(\"commands\", commands)\n",
    "        commands.append(\"    df = pd.DataFrame(df_contents)\")\n",
    "        return \"\\n\".join(commands)\n"
   ]
  }
 ],
 "metadata": {
  "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.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
