Move DF to tabular.

This commit is contained in:
Mikael Capelle 2017-12-07 15:42:06 +01:00
parent 82418bd6c3
commit 154d8e1858
3 changed files with 50 additions and 38 deletions

View File

@ -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

View File

@ -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))

View File

@ -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