Formatting utility package tia.util.fmt

  • Useful for quick formatting in IPython notebook
  • Used in the pdf generation package

Predefined formatters

  • IntFormatter
  • FloatFormatter
  • PercentFormatter
  • ThousandsFormatter
  • MillionsFormatter
  • BillionsFormatter
  • TrillionsFormatter
  • DollarCentsFormatter
  • DollarFormatter
  • ThousandDollarsFormatter
  • MillionDollarsFormatter
  • BillionDollarsFormatter
  • TrillionDollarsFormatter
  • YmdFormatter
  • Y_m_dFormatter
  • DynamicNumberFormatter

Format builder methods

  • new_int_formatter
  • new_float_formatter
  • new_thousands_formatter
  • new_millions_formatter
  • new_billions_formatter
  • new_trillions_formatter
  • new_percent_formatter
  • new_datetime_formatter
  • new_dynamic_formatter

In [49]:
import tia.util.fmt as fmt
import pandas as pd
import numpy as np
from IPython.display import HTML

In [50]:
# formatters are callable and accept a variety of values
number = 123456.5
fmt.ThousandDollarsFormatter(number)


Out[50]:
'$123.5k'

In [51]:
array = [123456.5, -123456.5]
fmt.ThousandDollarsFormatter(array)


Out[51]:
['$123.5k', '$(123.5k)']

In [52]:
series = pd.Series({'a': 123456.5, 'b': 123456.5})
fmt.ThousandDollarsFormatter(series)


Out[52]:
a    $123.5k
b    $123.5k
dtype: object

In [53]:
frame = pd.DataFrame({'c1': series, 'c2': series})
fmt.ThousandDollarsFormatter(frame)


Out[53]:
c1 c2
a $123.5k $123.5k
b $123.5k $123.5k

In [54]:
ndarray = frame.values
fmt.ThousandDollarsFormatter(ndarray)


Out[54]:
array([['$123.5k', '$123.5k'],
       ['$123.5k', '$123.5k']], dtype=object)

In [55]:
# heterogeneous data frame
f = pd.DataFrame({'k': [1001.2, -94551.12], 'f': [100.99, -94.12], 'pct': [.021, -.505]})
f


Out[55]:
f k pct
0 100.99 1001.20 0.021
1 -94.12 -94551.12 -0.505

In [56]:
# let the formatter guess - it uses all values to make decision unless you set method
fmt.DynamicNumberFormatter(f)


Out[56]:
f k pct
0 101 1k 2.1%
1 (94.1) (94.6k) (50.5%)

In [57]:
# Want more granual so tell it to do it by col (all values per column considered)
fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(f)


Out[57]:
f k pct
0 100.99 1.00k 2.10%
1 (94.12) (94.55k) (50.50%)

In [58]:
# If one of the percents is greater than one, the dynamic will default pct to float format
fc = f.copy()
fc.ix[1, 'pct'] = 1.02
fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(fc)


Out[58]:
f k pct
0 100.99 1.00k 0.02
1 (94.12) (94.55k) 1.02

In [59]:
# You can do each cell individually if you like
fmt.new_dynamic_formatter(method='cell', pcts=1, precision=2)(fc)


Out[59]:
f k pct
0 100.99 1.00k 2.10%
1 (94.12) (94.55k) 1.02

In [60]:
# What if the data is by row
tf = f.T
tf


Out[60]:
0 1
f 100.990 -94.120
k 1001.200 -94551.120
pct 0.021 -0.505

In [61]:
# show dynamic formtter by column. it defaults to best fit for all column values
fmt.new_dynamic_formatter(method='col', pcts=1, precision=2)(f.T)


Out[61]:
0 1
f 100.99 (94.12)
k 1,001.20 (94,551.12)
pct 0.02 (0.51)

In [62]:
# instead do it by row
fmt.new_dynamic_formatter(method='row', pcts=1, precision=2)(f.T)


Out[62]:
0 1
f 100.99 (94.12)
k 1.00k (94.55k)
pct 2.10% (50.50%)

In [63]:
# use the formatters for easier HTML generation
fmts = {
    'k': fmt.new_thousands_formatter(precision=1, commas=True, parens=False),
    'f': fmt.new_float_formatter(precision=3, parens=True),
    'pct': fmt.new_percent_formatter(precision=1, parens=True)
}
HTML(f.to_html(formatters=fmts))


Out[63]:
f k pct
0 100.990 1.0k 2.1%
1 (94.120) -94.6k (50.5%)

In [ ]: