Skip to content

Instantly share code, notes, and snippets.

@Kelvinrr
Last active December 27, 2017 23:51
Show Gist options
  • Select an option

  • Save Kelvinrr/271030c807bfa6603d67a328d938ae95 to your computer and use it in GitHub Desktop.

Select an option

Save Kelvinrr/271030c807bfa6603d67a328d938ae95 to your computer and use it in GitHub Desktop.
Pandas Shenanigans
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd \n",
"\n",
"class SubclassedSeries(pd.Series):\n",
" # normal properties\n",
" _metadata = ['tolerance']\n",
"\n",
" @property\n",
" def _constructor(self):\n",
" return SubclassedSeries\n",
"\n",
" @property\n",
" def _constructor_expanddim(self):\n",
" return SubclassedDataFrame\n",
"\n",
"class SubclassedDataFrame(pd.DataFrame):\n",
" def __init__(self, *args, **kw):\n",
" super(SubclassedDataFrame, self).__init__(*args, **kw)\n",
" \n",
" @property\n",
" def _constructor(self):\n",
" return SubclassedDataFrame\n",
"\n",
" @property\n",
" def _constructor_sliced(self):\n",
" return SubclassedSeries"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style>\n",
" .dataframe thead tr:only-child th {\n",
" text-align: right;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: left;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>test1</th>\n",
" <th>test2</th>\n",
" <th>test3</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>22</td>\n",
" <td>33</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" test1 test2 test3\n",
"0 5 22 33"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = SubclassedDataFrame.from_dict([{'test1': 5, 'test2': 22, 'test3' : 33}])\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(__main__.SubclassedSeries,\n",
" __main__.SubclassedSeries,\n",
" __main__.SubclassedSeries)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(d.loc[0]), type(d['test1']), type(d.iloc[0])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def gettype(row):\n",
" return type(row)\n",
"\n",
"def changetype(row):\n",
" row.__class__ = SubclassedSeries\n",
" return type(row)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 <class 'pandas.core.series.Series'>\n",
"dtype: object"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# this is a problem\n",
"d.apply(gettype, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 <class '__main__.SubclassedSeries'>\n",
"dtype: object"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use geopandas style type change\n",
"d.apply(changetype, axis=1)"
]
}
],
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment