From 154d8e18585b1162b752e6863959634992473f2a Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Thu, 7 Dec 2017 15:42:06 +0100 Subject: [PATCH] Move DF to tabular. --- pyltk/__init__.py | 2 +- pyltk/table.py | 39 ++------------------------------------- pyltk/tabular.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/pyltk/__init__.py b/pyltk/__init__.py index a9ec609..7a69a5c 100644 --- a/pyltk/__init__.py +++ b/pyltk/__init__.py @@ -10,5 +10,5 @@ from .makebox import makebox from .resizebox import resizebox from .subfloat import subfloat from .table import table, tabledf -from .tabular import tabular +from .tabular import tabular, tabulardf from .inlines import it, bf, mt diff --git a/pyltk/table.py b/pyltk/table.py index 207780d..432f934 100644 --- a/pyltk/table.py +++ b/pyltk/table.py @@ -1,10 +1,7 @@ # -*- encoding: utf-8 -*- from .element import element -from .tabular import tabular - -import itertools -import pandas as pd +from .tabular import tabulardf class table(element): @@ -50,40 +47,8 @@ class table(element): }) -def multicolumn(n, s=''): - if n == 1: - return s - return '\\multicolumn{' + str(n) + '}{c}{' + s + '}' - - class tabledf(table): def __init__(self, df, header=None, multilevels=False, fmt=None, **kargs): super().__init__(**kargs) - if header is None: - multilevels, header = self.create_header(df) - if multilevels: - ncols = len(header[-1]) - df = df.reset_index() - else: - ncols = len(header) - header = [header] - tab = tabular(columns='r' * ncols) - tab.addrows(header) - tab.addhline() - tab.addrows(df.astype(object).values, fmt=fmt) - self.add(tab) - - def create_header(self, df): - cols = df.columns - if not isinstance(cols, pd.MultiIndex): - return False, list(df.columns) - idx = df.index.names - header = [] - for levels, labels in zip(cols.levels, cols.labels): - tmp = [multicolumn(len(idx))] - for i, g in itertools.groupby(labels): - tmp.append(multicolumn(len(list(g)), levels[i])) - header.append(tmp) - header[-1] = idx + header[-1][1:] # Update last row - return True, header + self.add(tabulardf(df, header, multilevels, fmt)) diff --git a/pyltk/tabular.py b/pyltk/tabular.py index f751d6a..59437b7 100644 --- a/pyltk/tabular.py +++ b/pyltk/tabular.py @@ -1,7 +1,14 @@ # -*- encoding: utf-8 -*- +import itertools + from .element import element +try: + import pandas as pd +except: + pd = None + class row_element(element): @@ -38,6 +45,7 @@ class hline_element(row_element): def content(self): return '\\hline' + # instance of hline element hline = hline_element() @@ -97,3 +105,42 @@ class tabular(element): 'columns': self.columns, 'inner': super().content() }) + + +def multicolumn(n, s=''): + if n == 1: + return s + return '\\multicolumn{' + str(n) + '}{c}{' + s + '}' + + +class tabulardf(tabular): + + def __init__(self, df, header=None, multilevels=False, fmt=None, **kargs): + if header is None: + multilevels, header = self.create_header(df) + if multilevels: + ncols = len(header[-1]) + df = df.reset_index() + else: + ncols = len(header) + header = [header] + + kargs.setdefault('columns', 'r' * ncols) + super().__init__(**kargs) + self.addrows(header) + self.addhline() + self.addrows(df.astype(object).values, fmt=fmt) + + def create_header(self, df): + cols = df.columns + if not isinstance(cols, pd.MultiIndex): + return False, list(df.columns) + idx = df.index.names + header = [] + for levels, labels in zip(cols.levels, cols.labels): + tmp = [multicolumn(len(idx))] + for i, g in itertools.groupby(labels): + tmp.append(multicolumn(len(list(g)), levels[i])) + header.append(tmp) + header[-1] = idx + header[-1][1:] # Update last row + return True, header