Skip to content

Instantly share code, notes, and snippets.

@ryxcommar
Last active July 23, 2021 21:53
Show Gist options
  • Save ryxcommar/61f205c23cec5946dd006727b616472a to your computer and use it in GitHub Desktop.
Save ryxcommar/61f205c23cec5946dd006727b616472a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "e4770bb6",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from pandas.core.indexing import _ScalarAccessIndexer\n",
"\n",
"\n",
"class AssignmentContext(object):\n",
"\n",
" def __init__(self, pandas_obj, li=None, include_loc=True):\n",
" self._obj = pandas_obj\n",
" self._set = set(li)\n",
" self.include_loc = include_loc\n",
" self._transforms = {}\n",
"\n",
" def _wrapper(self, old_func):\n",
" def new_func(obj, key, val):\n",
" res = old_func(obj, key, val)\n",
" nonlocal self\n",
" if isinstance(key, (pd.Series, np.ndarray, list, pd.Index)):\n",
" self._set.union(key)\n",
" else:\n",
" self._set.add(key)\n",
" return res\n",
" return new_func\n",
"\n",
" def _wrap(self, o):\n",
" old = o.__setitem__\n",
" o.__setitem__ = self._wrapper(o.__setitem__)\n",
" self._transforms[o.__setitem__] = old\n",
"\n",
" def _unwrap(self, o):\n",
" o.__setitem__ = self._transforms.pop(o.__setitem__, None)\n",
"\n",
" def __enter__(self):\n",
" self._wrap(pd.Series)\n",
" self._wrap(pd.DataFrame)\n",
" if self.include_loc:\n",
" self._wrap(_ScalarAccessIndexer)\n",
" return self._set\n",
"\n",
" def __exit__(self, *args):\n",
" self._unwrap(pd.Series)\n",
" self._unwrap(pd.DataFrame)\n",
" if self.include_loc:\n",
" self._unwrap(_ScalarAccessIndexer)\n",
"\n",
"\n",
"@pd.api.extensions.register_dataframe_accessor(\"assignment_context\")\n",
"class AssignmentContextAccessor(object):\n",
"\n",
" def __init__(self, pandas_obj):\n",
" self._obj = pandas_obj\n",
"\n",
" def __call__(self, *args, **kwargs):\n",
" return AssignmentContext(self._obj, *args, **kwargs)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c01896fb",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({\n",
" \"foo\": [4, 5, 7, 6, 7, 7, 5],\n",
" \"bar\": [12, 63, 23, 36, 21, 28, 42]\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "33f22945",
"metadata": {},
"outputs": [],
"source": [
"df[\"a\"] = \"please ignore me\"\n",
"\n",
"# Do feature transformations...\n",
"\n",
"with df.assignment_context() as features:\n",
" df[\"fizz\"] = df[\"foo\"] + df[\"bar\"]\n",
" df.loc[:, \"buzz\"] = 2\n",
"\n",
"df[\"b\"] = \"please ignore me too\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1e1793b9",
"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>foo</th>\n",
" <th>bar</th>\n",
" <th>a</th>\n",
" <th>fizz</th>\n",
" <th>buzz</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>4</td>\n",
" <td>12</td>\n",
" <td>please ignore me</td>\n",
" <td>16</td>\n",
" <td>2</td>\n",
" <td>please ignore me too</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>5</td>\n",
" <td>63</td>\n",
" <td>please ignore me</td>\n",
" <td>68</td>\n",
" <td>2</td>\n",
" <td>please ignore me too</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>7</td>\n",
" <td>23</td>\n",
" <td>please ignore me</td>\n",
" <td>30</td>\n",
" <td>2</td>\n",
" <td>please ignore me too</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6</td>\n",
" <td>36</td>\n",
" <td>please ignore me</td>\n",
" <td>42</td>\n",
" <td>2</td>\n",
" <td>please ignore me too</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>7</td>\n",
" <td>21</td>\n",
" <td>please ignore me</td>\n",
" <td>28</td>\n",
" <td>2</td>\n",
" <td>please ignore me too</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" foo bar a fizz buzz b\n",
"0 4 12 please ignore me 16 2 please ignore me too\n",
"1 5 63 please ignore me 68 2 please ignore me too\n",
"2 7 23 please ignore me 30 2 please ignore me too\n",
"3 6 36 please ignore me 42 2 please ignore me too\n",
"4 7 21 please ignore me 28 2 please ignore me too"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c4e52961",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'fizz', 'buzz'}\n"
]
}
],
"source": [
"# All the new variables you created within the context are here:\n",
"\n",
"print(features)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1567016d",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment