Skip to content

Commit 63190f5

Browse files
author
Kyle Meyer
committed
BUG: adjust to_latex column format when no index
`to_latex` was adding an extra alignment to `column_format` when the `index` argument was False
1 parent 5afb0eb commit 63190f5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pandas/core/format.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@ def get_col_type(dtype):
356356

357357
if column_format is None:
358358
dtypes = self.frame.dtypes.values
359-
column_format = 'l%s' % ''.join(map(get_col_type, dtypes))
359+
if self.index:
360+
column_format = 'l%s' % ''.join(map(get_col_type, dtypes))
361+
else:
362+
column_format = '%s' % ''.join(map(get_col_type, dtypes))
360363
elif not isinstance(column_format, basestring):
361364
raise AssertionError(('column_format must be str or unicode, not %s'
362365
% type(column_format)))

pandas/tests/test_format.py

+25
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,31 @@ def test_to_latex(self):
13551355
# it works!
13561356
self.frame.to_latex()
13571357

1358+
df = DataFrame({'a': [1, 2],
1359+
'b': ['b1', 'b2']})
1360+
withindex_result = df.to_latex()
1361+
withindex_expected = r"""\begin{tabular}{lrl}
1362+
\toprule
1363+
{} & a & b \\
1364+
\midrule
1365+
0 & 1 & b1 \\
1366+
1 & 2 & b2 \\
1367+
\bottomrule
1368+
\end{tabular}
1369+
"""
1370+
self.assertEqual(withindex_result, withindex_expected)
1371+
1372+
withoutindex_result = df.to_latex(index=False)
1373+
withoutindex_expected = r"""\begin{tabular}{rl}
1374+
\toprule
1375+
a & b \\
1376+
\midrule
1377+
1 & b1 \\
1378+
2 & b2 \\
1379+
\bottomrule
1380+
\end{tabular}
1381+
"""
1382+
self.assertEqual(withoutindex_result, withoutindex_expected)
13581383

13591384
class TestSeriesFormatting(unittest.TestCase):
13601385
_multiprocess_can_split_ = True

0 commit comments

Comments
 (0)