Move DF to tabular.
This commit is contained in:
parent
82418bd6c3
commit
154d8e1858
@ -10,5 +10,5 @@ from .makebox import makebox
|
|||||||
from .resizebox import resizebox
|
from .resizebox import resizebox
|
||||||
from .subfloat import subfloat
|
from .subfloat import subfloat
|
||||||
from .table import table, tabledf
|
from .table import table, tabledf
|
||||||
from .tabular import tabular
|
from .tabular import tabular, tabulardf
|
||||||
from .inlines import it, bf, mt
|
from .inlines import it, bf, mt
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
from .element import element
|
from .element import element
|
||||||
from .tabular import tabular
|
from .tabular import tabulardf
|
||||||
|
|
||||||
import itertools
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
|
|
||||||
class table(element):
|
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):
|
class tabledf(table):
|
||||||
|
|
||||||
def __init__(self, df, header=None, multilevels=False, fmt=None, **kargs):
|
def __init__(self, df, header=None, multilevels=False, fmt=None, **kargs):
|
||||||
super().__init__(**kargs)
|
super().__init__(**kargs)
|
||||||
if header is None:
|
self.add(tabulardf(df, header, multilevels, fmt))
|
||||||
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
|
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
import itertools
|
||||||
|
|
||||||
from .element import element
|
from .element import element
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pandas as pd
|
||||||
|
except:
|
||||||
|
pd = None
|
||||||
|
|
||||||
|
|
||||||
class row_element(element):
|
class row_element(element):
|
||||||
|
|
||||||
@ -38,6 +45,7 @@ class hline_element(row_element):
|
|||||||
def content(self):
|
def content(self):
|
||||||
return '\\hline'
|
return '\\hline'
|
||||||
|
|
||||||
|
|
||||||
# instance of hline element
|
# instance of hline element
|
||||||
hline = hline_element()
|
hline = hline_element()
|
||||||
|
|
||||||
@ -97,3 +105,42 @@ class tabular(element):
|
|||||||
'columns': self.columns,
|
'columns': self.columns,
|
||||||
'inner': super().content()
|
'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
|
||||||
|
Loading…
Reference in New Issue
Block a user