{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Formatting utility package tia.util.fmt\n",
"\n",
"- Useful for quick formatting in IPython notebook\n",
"- Used in the pdf generation package\n",
"\n",
"####Predefined formatters\n",
"- IntFormatter\n",
"- FloatFormatter\n",
"- PercentFormatter\n",
"- ThousandsFormatter\n",
"- MillionsFormatter \n",
"- BillionsFormatter \n",
"- TrillionsFormatter \n",
"- DollarCentsFormatter\n",
"- DollarFormatter\n",
"- ThousandDollarsFormatter\n",
"- MillionDollarsFormatter\n",
"- BillionDollarsFormatter \n",
"- TrillionDollarsFormatter\n",
"- YmdFormatter\n",
"- Y_m_dFormatter \n",
"- DynamicNumberFormatter\n",
"\n",
"####Format builder methods\n",
"- new_int_formatter\n",
"- new_float_formatter\n",
"- new_thousands_formatter\n",
"- new_millions_formatter\n",
"- new_billions_formatter\n",
"- new_trillions_formatter\n",
"- new_percent_formatter\n",
"- new_datetime_formatter\n",
"- new_dynamic_formatter\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import tia.util.fmt as fmt\n",
"import pandas as pd\n",
"import numpy as np\n",
"from IPython.display import HTML"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'$123.5k'"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# formatters are callable and accept a variety of values\n",
"number = 123456.5\n",
"fmt.ThousandDollarsFormatter(number)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['$123.5k', '$(123.5k)']"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array = [123456.5, -123456.5]\n",
"fmt.ThousandDollarsFormatter(array)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"a $123.5k\n",
"b $123.5k\n",
"dtype: object"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"series = pd.Series({'a': 123456.5, 'b': 123456.5})\n",
"fmt.ThousandDollarsFormatter(series)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
"
\n",
" \n",
" \n",
" \n",
" a | \n",
" $123.5k | \n",
" $123.5k | \n",
"
\n",
" \n",
" b | \n",
" $123.5k | \n",
" $123.5k | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2\n",
"a $123.5k $123.5k\n",
"b $123.5k $123.5k"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"frame = pd.DataFrame({'c1': series, 'c2': series})\n",
"fmt.ThousandDollarsFormatter(frame)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([['$123.5k', '$123.5k'],\n",
" ['$123.5k', '$123.5k']], dtype=object)"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ndarray = frame.values\n",
"fmt.ThousandDollarsFormatter(ndarray)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" f | \n",
" k | \n",
" pct | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 100.99 | \n",
" 1001.20 | \n",
" 0.021 | \n",
"
\n",
" \n",
" 1 | \n",
" -94.12 | \n",
" -94551.12 | \n",
" -0.505 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" f k pct\n",
"0 100.99 1001.20 0.021\n",
"1 -94.12 -94551.12 -0.505"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# heterogeneous data frame\n",
"f = pd.DataFrame({'k': [1001.2, -94551.12], 'f': [100.99, -94.12], 'pct': [.021, -.505]})\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" f | \n",
" k | \n",
" pct | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 101 | \n",
" 1k | \n",
" 2.1% | \n",
"
\n",
" \n",
" 1 | \n",
" (94.1) | \n",
" (94.6k) | \n",
" (50.5%) | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" f k pct\n",
"0 101 1k 2.1%\n",
"1 (94.1) (94.6k) (50.5%)"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# let the formatter guess - it uses all values to make decision unless you set method\n",
"fmt.DynamicNumberFormatter(f)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" f | \n",
" k | \n",
" pct | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 100.99 | \n",
" 1.00k | \n",
" 2.10% | \n",
"
\n",
" \n",
" 1 | \n",
" (94.12) | \n",
" (94.55k) | \n",
" (50.50%) | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" f k pct\n",
"0 100.99 1.00k 2.10%\n",
"1 (94.12) (94.55k) (50.50%)"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Want more granual so tell it to do it by col (all values per column considered)\n",
"fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(f)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" f | \n",
" k | \n",
" pct | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 100.99 | \n",
" 1.00k | \n",
" 0.02 | \n",
"
\n",
" \n",
" 1 | \n",
" (94.12) | \n",
" (94.55k) | \n",
" 1.02 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" f k pct\n",
"0 100.99 1.00k 0.02\n",
"1 (94.12) (94.55k) 1.02"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# If one of the percents is greater than one, the dynamic will default pct to float format\n",
"fc = f.copy()\n",
"fc.ix[1, 'pct'] = 1.02\n",
"fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(fc)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" f | \n",
" k | \n",
" pct | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 100.99 | \n",
" 1.00k | \n",
" 2.10% | \n",
"
\n",
" \n",
" 1 | \n",
" (94.12) | \n",
" (94.55k) | \n",
" 1.02 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" f k pct\n",
"0 100.99 1.00k 2.10%\n",
"1 (94.12) (94.55k) 1.02"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# You can do each cell individually if you like\n",
"fmt.new_dynamic_formatter(method='cell', pcts=1, precision=2)(fc)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" f | \n",
" 100.990 | \n",
" -94.120 | \n",
"
\n",
" \n",
" k | \n",
" 1001.200 | \n",
" -94551.120 | \n",
"
\n",
" \n",
" pct | \n",
" 0.021 | \n",
" -0.505 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1\n",
"f 100.990 -94.120\n",
"k 1001.200 -94551.120\n",
"pct 0.021 -0.505"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# What if the data is by row\n",
"tf = f.T\n",
"tf"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" f | \n",
" 100.99 | \n",
" (94.12) | \n",
"
\n",
" \n",
" k | \n",
" 1,001.20 | \n",
" (94,551.12) | \n",
"
\n",
" \n",
" pct | \n",
" 0.02 | \n",
" (0.51) | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1\n",
"f 100.99 (94.12)\n",
"k 1,001.20 (94,551.12)\n",
"pct 0.02 (0.51)"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# show dynamic formtter by column. it defaults to best fit for all column values\n",
"fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(f.T)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" f | \n",
" 100.99 | \n",
" (94.12) | \n",
"
\n",
" \n",
" k | \n",
" 1.00k | \n",
" (94.55k) | \n",
"
\n",
" \n",
" pct | \n",
" 2.10% | \n",
" (50.50%) | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1\n",
"f 100.99 (94.12)\n",
"k 1.00k (94.55k)\n",
"pct 2.10% (50.50%)"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# instead do it by row\n",
"fmt.new_dynamic_formatter(method='row', pcts=1, precision=2)(f.T)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" | \n",
" f | \n",
" k | \n",
" pct | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 100.990 | \n",
" 1.0k | \n",
" 2.1% | \n",
"
\n",
" \n",
" 1 | \n",
" (94.120) | \n",
" -94.6k | \n",
" (50.5%) | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# use the formatters for easier HTML generation\n",
"fmts = {\n",
" 'k': fmt.new_thousands_formatter(precision=1, commas=True, parens=False),\n",
" 'f': fmt.new_float_formatter(precision=3, parens=True),\n",
" 'pct': fmt.new_percent_formatter(precision=1, parens=True)\n",
"}\n",
"HTML(f.to_html(formatters=fmts))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}