Skip to content

Instantly share code, notes, and snippets.

@mikepqr
Created April 22, 2019 22:25
Show Gist options
  • Save mikepqr/a8328fe4130267bb75bc44d7e236a83f to your computer and use it in GitHub Desktop.
Save mikepqr/a8328fe4130267bb75bc44d7e236a83f 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,
"metadata": {},
"outputs": [],
"source": [
"import string\n",
"import itertools as it\n",
"\n",
"def nalpha(n, sep=\"\"):\n",
" return sep.join(it.islice(it.cycle(string.ascii_lowercase), n))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.23.3\n"
]
}
],
"source": [
"import pandas as pd\n",
"\n",
"print(pd.__version__)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.4.0\r\n"
]
}
],
"source": [
"!jupyter --version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Highlight row on hover\n",
"\n",
"This is a basic DF. Note hover behavior in running notebook. We don't do this. That's not a bug, but it would be nice to have this while we're working on the CSS."
]
},
{
"cell_type": "code",
"execution_count": 4,
"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>abcdefghij</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" abcdefghij\n",
"0 0\n",
"1 1\n",
"2 2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame([0,1,2], columns=[nalpha(10)])\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Allow horizontal scroll for overflow\n",
"\n",
"DF with single wide column, allow horizontal scroll.\n",
"\n",
"This is broken (DSE-3562)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"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>abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr\n",
"0 0 \n",
"1 1 \n",
"2 2 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame([0,1,2], columns=[nalpha(200)])\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Horizontal scroll for lots of unbreakable columns\n",
"\n",
"This is broken (DSE-3562)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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>abcdefghijklmnopqrst</th>\n",
" <th>abcdefghijklmnopqrstu</th>\n",
" <th>abcdefghijklmnopqrstuv</th>\n",
" <th>abcdefghijklmnopqrstuvw</th>\n",
" <th>abcdefghijklmnopqrstuvwx</th>\n",
" <th>abcdefghijklmnopqrstuvwxy</th>\n",
" <th>abcdefghijklmnopqrstuvwxyz</th>\n",
" <th>abcdefghijklmnopqrstuvwxyza</th>\n",
" <th>abcdefghijklmnopqrstuvwxyzab</th>\n",
" <th>abcdefghijklmnopqrstuvwxyzabc</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" abcdefghijklmnopqrst abcdefghijklmnopqrstu abcdefghijklmnopqrstuv \\\n",
"0 0 0 0 \n",
"1 1 1 1 \n",
"2 2 2 2 \n",
"\n",
" abcdefghijklmnopqrstuvw abcdefghijklmnopqrstuvwx \\\n",
"0 0 0 \n",
"1 1 1 \n",
"2 2 2 \n",
"\n",
" abcdefghijklmnopqrstuvwxy abcdefghijklmnopqrstuvwxyz \\\n",
"0 0 0 \n",
"1 1 1 \n",
"2 2 2 \n",
"\n",
" abcdefghijklmnopqrstuvwxyza abcdefghijklmnopqrstuvwxyzab \\\n",
"0 0 0 \n",
"1 1 1 \n",
"2 2 2 \n",
"\n",
" abcdefghijklmnopqrstuvwxyzabc \n",
"0 0 \n",
"1 1 \n",
"2 2 "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame()\n",
"for i in range(20, 30):\n",
" df[nalpha(i)] = [0,1,2]\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# No horizontal scroll for lots of breakable columns\n",
"\n",
"This basically works. That should not regress with fix for DSE-3562."
]
},
{
"cell_type": "code",
"execution_count": 7,
"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>a b c d e f g h i j k l m n o p q r s t</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w x</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w x y</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w x y z</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w x y z a</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w x y z a b</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w x y z a b c</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b c d e f g h i j k l m n o p q r s t \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w x \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w x y \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w x y z \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w x y z a \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w x y z a b \\\n",
"0 0 \n",
"1 1 \n",
"2 2 \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w x y z a b c \n",
"0 0 \n",
"1 1 \n",
"2 2 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame()\n",
"for i in range(20, 30):\n",
" df[nalpha(i, sep=\" \")] = [0,1,2]\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ... for max_columns/max_rows\n",
"\n",
"This basically works. That should not regress with fix for DSE-3562."
]
},
{
"cell_type": "code",
"execution_count": 8,
"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>a b c d e f g h i j k l m n o p q r s t</th>\n",
" <th>...</th>\n",
" <th>a b c d e f g h i j k l m n o p q r s t u v w x y z a b c</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>...</td>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>3 rows × 10 columns</p>\n",
"</div>"
],
"text/plain": [
" a b c d e f g h i j k l m n o p q r s t \\\n",
"0 0 \n",
".. ... \n",
"2 2 \n",
"\n",
" ... \\\n",
"0 ... \n",
".. ... \n",
"2 ... \n",
"\n",
" a b c d e f g h i j k l m n o p q r s t u v w x y z a b c \n",
"0 0 \n",
".. ... \n",
"2 2 \n",
"\n",
"[3 rows x 10 columns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.options.display.max_columns = 2\n",
"pd.options.display.max_rows = 2\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# For extra points, do DSE-5729 while you're in there\n",
"\n",
"Note nice styling and hover behavior."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table style='border-style:solid;border-width:1px;'>\n",
"<tr>\n",
"<td style='font-family:monospace;white-space: pre;' title='(0, 0)'>0 </td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(0, 1)'>1 </td>\n",
"<td><center>⋯</center></td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(0, 5)'>5 </td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(0, 6)'>6 </td>\n",
"</tr>\n",
"<tr>\n",
"<td style='font-family:monospace;white-space: pre;' title='(1, 0)'>7 </td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(1, 1)'>8 </td>\n",
"<td><center>⋯</center></td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(1, 5)'>12</td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(1, 6)'>13</td>\n",
"</tr>\n",
"<tr>\n",
"<td><center>⋮</center></td>\n",
"<td><center>⋮</center></td>\n",
"<td><center>⋱</center></td>\n",
"<td><center>⋮</center></td>\n",
"<td><center>⋮</center></td>\n",
"</tr>\n",
"<tr>\n",
"<td style='font-family:monospace;white-space: pre;' title='(5, 0)'>35</td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(5, 1)'>36</td>\n",
"<td><center>⋯</center></td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(5, 5)'>40</td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(5, 6)'>41</td>\n",
"</tr>\n",
"<tr>\n",
"<td style='font-family:monospace;white-space: pre;' title='(6, 0)'>42</td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(6, 1)'>43</td>\n",
"<td><center>⋯</center></td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(6, 5)'>47</td>\n",
"<td style='font-family:monospace;white-space: pre;' title='(6, 6)'>48</td>\n",
"</tr>\n",
"</table>"
],
"text/plain": [
"array([[ 0, 1, ..., 5, 6],\n",
" [ 7, 8, ..., 12, 13],\n",
" ...,\n",
" [35, 36, ..., 40, 41],\n",
" [42, 43, ..., 47, 48]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy_html\n",
"import numpy as np\n",
"np.set_printoptions(threshold=5, edgeitems=2)\n",
"np.arange(49).reshape(7,7)"
]
}
],
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment