import tia.util.fmt as fmt
import pandas as pd
import numpy as np
from IPython.display import HTML
# formatters are callable and accept a variety of values
number = 123456.5
fmt.ThousandDollarsFormatter(number)
'$123.5k'
array = [123456.5, -123456.5]
fmt.ThousandDollarsFormatter(array)
['$123.5k', '$(123.5k)']
series = pd.Series({'a': 123456.5, 'b': 123456.5})
fmt.ThousandDollarsFormatter(series)
a $123.5k b $123.5k dtype: object
frame = pd.DataFrame({'c1': series, 'c2': series})
fmt.ThousandDollarsFormatter(frame)
c1 | c2 | |
---|---|---|
a | $123.5k | $123.5k |
b | $123.5k | $123.5k |
ndarray = frame.values
fmt.ThousandDollarsFormatter(ndarray)
array([['$123.5k', '$123.5k'], ['$123.5k', '$123.5k']], dtype=object)
# heterogeneous data frame
f = pd.DataFrame({'k': [1001.2, -94551.12], 'f': [100.99, -94.12], 'pct': [.021, -.505]})
f
f | k | pct | |
---|---|---|---|
0 | 100.99 | 1001.20 | 0.021 |
1 | -94.12 | -94551.12 | -0.505 |
# let the formatter guess - it uses all values to make decision unless you set method
fmt.DynamicNumberFormatter(f)
f | k | pct | |
---|---|---|---|
0 | 101 | 1k | 2.1% |
1 | (94.1) | (94.6k) | (50.5%) |
# 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)
f | k | pct | |
---|---|---|---|
0 | 100.99 | 1.00k | 2.10% |
1 | (94.12) | (94.55k) | (50.50%) |
# 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)
f | k | pct | |
---|---|---|---|
0 | 100.99 | 1.00k | 0.02 |
1 | (94.12) | (94.55k) | 1.02 |
# You can do each cell individually if you like
fmt.new_dynamic_formatter(method='cell', pcts=1, precision=2)(fc)
f | k | pct | |
---|---|---|---|
0 | 100.99 | 1.00k | 2.10% |
1 | (94.12) | (94.55k) | 1.02 |
# What if the data is by row
tf = f.T
tf
0 | 1 | |
---|---|---|
f | 100.990 | -94.120 |
k | 1001.200 | -94551.120 |
pct | 0.021 | -0.505 |
# 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)
0 | 1 | |
---|---|---|
f | 100.99 | (94.12) |
k | 1,001.20 | (94,551.12) |
pct | 0.02 | (0.51) |
# instead do it by row
fmt.new_dynamic_formatter(method='row', pcts=1, precision=2)(f.T)
0 | 1 | |
---|---|---|
f | 100.99 | (94.12) |
k | 1.00k | (94.55k) |
pct | 2.10% | (50.50%) |
# 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))
f | k | pct | |
---|---|---|---|
0 | 100.990 | 1.0k | 2.1% |
1 | (94.120) | -94.6k | (50.5%) |