Skip to content

Commit fb42766

Browse files
glygjreback
authored andcommitted
BUG: column_format argument not passing from DataFrame.to_latex to formatter, #9402
removed now irrelevant TODO comment release notes + some refs
1 parent 788ce4e commit fb42766

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

doc/source/whatsnew/v0.17.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ Bug Fixes
727727

728728
- Bug in incorrection computation of ``.mean()`` on ``timedelta64[ns]`` because of overflow (:issue:`9442`)
729729
- Bug in ``DataFrame.to_html(index=False)`` renders unnecessary ``name`` row (:issue:`10344`)
730+
- Bug in ``DataFrame.to_latex()`` the ``column_format`` argument could not be passed (:issue:`9402`)
730731
- Bug in ``DataFrame.apply`` when function returns categorical series. (:issue:`9573`)
731732
- Bug in ``to_datetime`` with invalid dates and formats supplied (:issue:`10154`)
732733
- Bug in ``Index.drop_duplicates`` dropping name(s) (:issue:`10115`)

pandas/core/format.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ def to_latex(self, column_format=None, longtable=False):
584584
"""
585585
self.escape = self.kwds.get('escape', True)
586586

587-
# TODO: column_format is not settable in df.to_latex
588587
def get_col_type(dtype):
589588
if issubclass(dtype.type, np.number):
590589
return 'r'

pandas/core/frame.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,8 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
14771477
def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
14781478
header=True, index=True, na_rep='NaN', formatters=None,
14791479
float_format=None, sparsify=None, index_names=True,
1480-
bold_rows=True, longtable=False, escape=True):
1480+
bold_rows=True, column_format=None,
1481+
longtable=False, escape=True):
14811482
"""
14821483
Render a DataFrame to a tabular environment table. You can splice
14831484
this into a LaTeX document. Requires \\usepackage{booktabs}.
@@ -1486,6 +1487,9 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
14861487
14871488
bold_rows : boolean, default True
14881489
Make the row labels bold in the output
1490+
column_format : str, default None
1491+
The columns format as specified in LaTeX (e.g 'rcl' for a 3 columns
1492+
table), see https://en.wikibooks.org/wiki/LaTeX/Tables
14891493
longtable : boolean, default False
14901494
Use a longtable environment instead of tabular. Requires adding
14911495
a \\usepackage{longtable} to your LaTeX preamble.
@@ -1509,7 +1513,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15091513
sparsify=sparsify,
15101514
index_names=index_names,
15111515
escape=escape)
1512-
formatter.to_latex(longtable=longtable)
1516+
formatter.to_latex(column_format=column_format, longtable=longtable)
15131517

15141518
if buf is None:
15151519
return formatter.buf.getvalue()

pandas/tests/test_format.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,24 @@ def test_to_latex(self):
23802380
"""
23812381
self.assertEqual(withoutindex_result, withoutindex_expected)
23822382

2383+
def test_to_latex_format(self):
2384+
# GH Bug #9402
2385+
self.frame.to_latex(column_format='ccc')
2386+
2387+
df = DataFrame({'a': [1, 2],
2388+
'b': ['b1', 'b2']})
2389+
withindex_result = df.to_latex(column_format='ccc')
2390+
withindex_expected = r"""\begin{tabular}{ccc}
2391+
\toprule
2392+
{} & a & b \\
2393+
\midrule
2394+
0 & 1 & b1 \\
2395+
1 & 2 & b2 \\
2396+
\bottomrule
2397+
\end{tabular}
2398+
"""
2399+
self.assertEqual(withindex_result, withindex_expected)
2400+
23832401
def test_to_latex_multiindex(self):
23842402
df = DataFrame({('x', 'y'): ['a']})
23852403
result = df.to_latex()

0 commit comments

Comments
 (0)