#!/usr/bin/env python # coding: utf-8 # ## 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) # In[51]: array = [123456.5, -123456.5] fmt.ThousandDollarsFormatter(array) # In[52]: series = pd.Series({'a': 123456.5, 'b': 123456.5}) fmt.ThousandDollarsFormatter(series) # In[53]: frame = pd.DataFrame({'c1': series, 'c2': series}) fmt.ThousandDollarsFormatter(frame) # In[54]: ndarray = frame.values fmt.ThousandDollarsFormatter(ndarray) # In[55]: # heterogeneous data frame f = pd.DataFrame({'k': [1001.2, -94551.12], 'f': [100.99, -94.12], 'pct': [.021, -.505]}) f # In[56]: # let the formatter guess - it uses all values to make decision unless you set method fmt.DynamicNumberFormatter(f) # 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) # 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) # In[59]: # You can do each cell individually if you like fmt.new_dynamic_formatter(method='cell', pcts=1, precision=2)(fc) # In[60]: # What if the data is by row tf = f.T tf # 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) # In[62]: # instead do it by row fmt.new_dynamic_formatter(method='row', pcts=1, precision=2)(f.T) # 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)) # In[ ]: