Skip to content

Commit 1c081e3

Browse files
committed
Merge pull request #3264 from anomrake/latex-tables
ENH: update DataFrame to_latex for nicer typesetting
2 parents 1052f47 + 24588f7 commit 1c081e3

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

pandas/core/format.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ def to_latex(self, force_unicode=None, column_format=None):
349349
"""
350350
Render a DataFrame to a LaTeX tabular environment output.
351351
"""
352+
def get_col_type(dtype):
353+
if issubclass(dtype.type, np.number):
354+
return 'r'
355+
else:
356+
return 'l'
357+
352358
import warnings
353359
if force_unicode is not None: # pragma: no cover
354360
warnings.warn(
@@ -364,27 +370,28 @@ def to_latex(self, force_unicode=None, column_format=None):
364370
strcols = [[info_line]]
365371
else:
366372
strcols = self._to_str_columns()
367-
373+
368374
if column_format is None:
369-
column_format = '|l|%s|' % '|'.join('c' for _ in strcols)
375+
dtypes = self.frame.dtypes.values
376+
column_format = 'l%s' % ''.join(map(get_col_type, dtypes))
370377
elif not isinstance(column_format, basestring):
371378
raise AssertionError(('column_format must be str or unicode, not %s'
372379
% type(column_format)))
373380

374381
self.buf.write('\\begin{tabular}{%s}\n' % column_format)
375-
self.buf.write('\\hline\n')
382+
self.buf.write('\\toprule\n')
376383

377384
nlevels = frame.index.nlevels
378385
for i, row in enumerate(izip(*strcols)):
379386
if i == nlevels:
380-
self.buf.write('\\hline\n') # End of header
387+
self.buf.write('\\midrule\n') # End of header
381388
crow = [(x.replace('_', '\\_')
382389
.replace('%', '\\%')
383390
.replace('&', '\\&') if x else '{}') for x in row]
384391
self.buf.write(' & '.join(crow))
385392
self.buf.write(' \\\\\n')
386393

387-
self.buf.write('\\hline\n')
394+
self.buf.write('\\bottomrule\n')
388395
self.buf.write('\\end{tabular}\n')
389396

390397
def _format_col(self, i):

0 commit comments

Comments
 (0)