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