From 63190f5de6cdc262b6dfecb0cf4ce3c3767ceac6 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 26 Apr 2013 16:35:38 -0400 Subject: [PATCH] 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 --- pandas/core/format.py | 5 ++++- pandas/tests/test_format.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pandas/core/format.py b/pandas/core/format.py index 7226bd14e5576..5b68b26a41b77 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -356,7 +356,10 @@ def get_col_type(dtype): if column_format is None: dtypes = self.frame.dtypes.values - column_format = 'l%s' % ''.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)) elif not isinstance(column_format, basestring): raise AssertionError(('column_format must be str or unicode, not %s' % type(column_format))) diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index c5ee51d9a7408..37f08dd177eae 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -1355,6 +1355,31 @@ def test_to_latex(self): # it works! self.frame.to_latex() + df = DataFrame({'a': [1, 2], + 'b': ['b1', 'b2']}) + withindex_result = df.to_latex() + withindex_expected = r"""\begin{tabular}{lrl} +\toprule +{} & a & b \\ +\midrule +0 & 1 & b1 \\ +1 & 2 & b2 \\ +\bottomrule +\end{tabular} +""" + self.assertEqual(withindex_result, withindex_expected) + + withoutindex_result = df.to_latex(index=False) + withoutindex_expected = r"""\begin{tabular}{rl} +\toprule + a & b \\ +\midrule + 1 & b1 \\ + 2 & b2 \\ +\bottomrule +\end{tabular} +""" + self.assertEqual(withoutindex_result, withoutindex_expected) class TestSeriesFormatting(unittest.TestCase): _multiprocess_can_split_ = True