Skip to content

ENH to_latex mi index will use & sep for levels #8219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,23 @@ def get_col_type(dtype):
else:
strcols = self._to_str_columns()

if self.index and isinstance(self.frame.index, MultiIndex):
fmt = self._get_formatter('__index__')
clevels = self.frame.columns.nlevels
strcols.pop(0)
name = any(self.frame.columns.names)
for i, lev in enumerate(self.frame.index.levels):
lev2 = lev.format(name=name)
width = len(lev2[0])
lev3 = [' ' * width] * clevels + lev2
strcols.insert(i, lev3)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is "a bit" hacky...


if column_format is None:
dtypes = self.frame.dtypes.values
column_format = ''.join(map(get_col_type, dtypes))
if self.index:
column_format = 'l%s' % ''.join(map(get_col_type, dtypes))
else:
column_format = '%s' % ''.join(map(get_col_type, dtypes))
index_format = 'l' * self.frame.index.nlevels
column_format = index_format + column_format
elif not isinstance(column_format,
compat.string_types): # pragma: no cover
raise AssertionError('column_format must be str or unicode, not %s'
Expand Down Expand Up @@ -645,8 +656,8 @@ def has_index_names(self):
def has_column_names(self):
return _has_names(self.frame.columns)

def _get_formatted_index(self,frame):
# Note: this is only used by to_string(), not by to_html().
def _get_formatted_index(self, frame):
# Note: this is only used by to_string() and to_latex(), not by to_html().
index = frame.index
columns = frame.columns

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2098,11 +2098,11 @@ def test_to_latex_multiindex(self):
self.assertEqual(result, expected)

result = df.T.to_latex()
expected = r"""\begin{tabular}{ll}
expected = r"""\begin{tabular}{lll}
\toprule
{} & 0 \\
& & 0 \\
\midrule
x y & a \\
x & y & a \\
\bottomrule
\end{tabular}
"""
Expand Down