Skip to content

Commit 24588f7

Browse files
committed
modified to_latex method of DataFrameFormatter to make nicer typeset tables
1 parent 1df192d commit 24588f7

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
@@ -347,6 +347,12 @@ def to_latex(self, force_unicode=None, column_format=None):
347347
"""
348348
Render a DataFrame to a LaTeX tabular environment output.
349349
"""
350+
def get_col_type(dtype):
351+
if issubclass(dtype.type, np.number):
352+
return 'r'
353+
else:
354+
return 'l'
355+
350356
import warnings
351357
if force_unicode is not None: # pragma: no cover
352358
warnings.warn(
@@ -362,27 +368,28 @@ def to_latex(self, force_unicode=None, column_format=None):
362368
strcols = [[info_line]]
363369
else:
364370
strcols = self._to_str_columns()
365-
371+
366372
if column_format is None:
367-
column_format = '|l|%s|' % '|'.join('c' for _ in strcols)
373+
dtypes = self.frame.dtypes.values
374+
column_format = 'l%s' % ''.join(map(get_col_type, dtypes))
368375
elif not isinstance(column_format, basestring):
369376
raise AssertionError(('column_format must be str or unicode, not %s'
370377
% type(column_format)))
371378

372379
self.buf.write('\\begin{tabular}{%s}\n' % column_format)
373-
self.buf.write('\\hline\n')
380+
self.buf.write('\\toprule\n')
374381

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

385-
self.buf.write('\\hline\n')
392+
self.buf.write('\\bottomrule\n')
386393
self.buf.write('\\end{tabular}\n')
387394

388395
def _format_col(self, i):

0 commit comments

Comments
 (0)