Created
July 31, 2015 19:48
-
-
Save wolever/c7d87be30cca4601049b to your computer and use it in GitHub Desktop.
The unique handling of division by zero across Pandas and NumPy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"pd: 0.13.1\n", | |
"np: 1.8.0\n" | |
] | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"print \"pd:\", pd.__version__\n", | |
"print \"np:\", np.__version__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>0 / 0</th>\n", | |
" <th>0 / 0.0</th>\n", | |
" <th>0.0 / 0</th>\n", | |
" <th>0.0 / 0.0</th>\n", | |
" <th>1 / 0</th>\n", | |
" <th>1 / 0.0</th>\n", | |
" <th>1.0 / 0</th>\n", | |
" <th>1.0 / 0.0</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>pd</th>\n", | |
" <td> inf</td>\n", | |
" <td> nan</td>\n", | |
" <td> inf</td>\n", | |
" <td> nan</td>\n", | |
" <td> inf</td>\n", | |
" <td> inf</td>\n", | |
" <td> inf</td>\n", | |
" <td> inf</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>np</th>\n", | |
" <td> 0</td>\n", | |
" <td> nan</td>\n", | |
" <td> nan</td>\n", | |
" <td> nan</td>\n", | |
" <td> 0</td>\n", | |
" <td> inf</td>\n", | |
" <td> inf</td>\n", | |
" <td> inf</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>2 rows × 8 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" 0 / 0 0 / 0.0 0.0 / 0 0.0 / 0.0 1 / 0 1 / 0.0 1.0 / 0 1.0 / 0.0\n", | |
"pd inf nan inf nan inf inf inf inf\n", | |
"np 0 nan nan nan 0 inf inf inf\n", | |
"\n", | |
"[2 rows x 8 columns]" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"index = []\n", | |
"data = []\n", | |
"for arr_name, arr in [(\"pd\", pd.Series), (\"np\", np.array)]:\n", | |
" cols = []\n", | |
" d = []\n", | |
" for divisor in [0, 0.0, 1, 1.0]:\n", | |
" for dividend in [0, 0.0]:\n", | |
" cols.append(\"%s / %s\" %(divisor, dividend))\n", | |
" d.append(repr((arr([divisor]) / dividend)[0]))\n", | |
" data.append(d)\n", | |
" index.append(arr_name)\n", | |
"df = pd.DataFrame(data)\n", | |
"df.index = index\n", | |
"df.columns = cols\n", | |
"df" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment