Fix tabledf.

This commit is contained in:
Mikaël Capelle 2017-07-20 11:02:29 +02:00
parent f3c3a6185a
commit 8aa3d06b19

View File

@ -61,8 +61,7 @@ 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: if header is None:
header = self.create_header(df) multilevels, header = self.create_header(df)
multilevels = True
df = df.reset_index() df = df.reset_index()
if multilevels: if multilevels:
ncols = len(header[-1]) ncols = len(header[-1])
@ -74,13 +73,13 @@ class tabledf(table):
else: else:
tab.addrow(header) tab.addrow(header)
tab.addhline() tab.addhline()
tab.addrows(df.as_matrix(), fmt=fmt) tab.addrows(df.astype(object).values, fmt=fmt)
self.add(tab) self.add(tab)
def create_header(self, df): def create_header(self, df):
cols = df.columns cols = df.columns
if not isinstance(cols, pd.MultiIndex): if not isinstance(cols, pd.MultiIndex):
return df.columns return False, list(df.columns)
idx = df.index.names idx = df.index.names
header = [] header = []
for levels, labels in zip(cols.levels, cols.labels): for levels, labels in zip(cols.levels, cols.labels):
@ -89,4 +88,4 @@ class tabledf(table):
tmp.append(multicolumn(len(list(g)), levels[i])) tmp.append(multicolumn(len(list(g)), levels[i]))
header.append(tmp) header.append(tmp)
header[-1] = idx + header[-1][1:] # Update last row header[-1] = idx + header[-1][1:] # Update last row
return header return True, header