Skip to content

Commit 4650563

Browse files
Nicolas Bonnottejreback
Nicolas Bonnotte
authored andcommitted
ENH: decimal parameter added in to_latex and to_html
closes #12031 Author: Nicolas Bonnotte <bonnotte@descartes> Closes #12417 from nbonnotte/12031-to-latex-missing-decimal and squashes the following commits: ea50fd2 [Nicolas Bonnotte] ENH decimal parameter to to_latex and to_html, #12031
1 parent 6b544de commit 4650563

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

doc/source/whatsnew/v0.18.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ Other API Changes
867867

868868
- Statistical functions for ``NDFrame`` objects will now raise if non-numpy-compatible arguments are passed in for ``**kwargs`` (:issue:`12301`)
869869

870+
- ``.to_latex`` and ``.to_html`` gain a ``decimal`` parameter like ``.to_csv``; the default is ``'.'`` (:issue:`12031`)
871+
872+
870873
.. _whatsnew_0180.deprecations:
871874

872875
Deprecations

pandas/core/format.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
338338
header=True, index=True, na_rep='NaN', formatters=None,
339339
justify=None, float_format=None, sparsify=None,
340340
index_names=True, line_width=None, max_rows=None,
341-
max_cols=None, show_dimensions=False, **kwds):
341+
max_cols=None, show_dimensions=False, decimal='.', **kwds):
342342
self.frame = frame
343343
self.buf = _expand_user(buf) if buf is not None else StringIO()
344344
self.show_index_names = index_names
@@ -351,6 +351,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
351351
self.float_format = float_format
352352
self.formatters = formatters if formatters is not None else {}
353353
self.na_rep = na_rep
354+
self.decimal = decimal
354355
self.col_space = col_space
355356
self.header = header
356357
self.index = index
@@ -648,7 +649,7 @@ def _format_col(self, i):
648649
formatter = self._get_formatter(i)
649650
return format_array(frame.iloc[:, i]._values, formatter,
650651
float_format=self.float_format, na_rep=self.na_rep,
651-
space=self.col_space)
652+
space=self.col_space, decimal=self.decimal)
652653

653654
def to_html(self, classes=None, notebook=False):
654655
"""
@@ -1970,7 +1971,7 @@ def get_formatted_cells(self):
19701971

19711972

19721973
def format_array(values, formatter, float_format=None, na_rep='NaN',
1973-
digits=None, space=None, justify='right'):
1974+
digits=None, space=None, justify='right', decimal='.'):
19741975

19751976
if com.is_categorical_dtype(values):
19761977
fmt_klass = CategoricalArrayFormatter
@@ -2000,7 +2001,7 @@ def format_array(values, formatter, float_format=None, na_rep='NaN',
20002001

20012002
fmt_obj = fmt_klass(values, digits=digits, na_rep=na_rep,
20022003
float_format=float_format, formatter=formatter,
2003-
space=space, justify=justify)
2004+
space=space, justify=justify, decimal=decimal)
20042005

20052006
return fmt_obj.get_result()
20062007

pandas/core/frame.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
14971497
float_format=None, sparsify=None, index_names=True,
14981498
justify=None, bold_rows=True, classes=None, escape=True,
14991499
max_rows=None, max_cols=None, show_dimensions=False,
1500-
notebook=False):
1500+
notebook=False, decimal='.'):
15011501
"""
15021502
Render a DataFrame as an HTML table.
15031503
@@ -1515,7 +1515,10 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
15151515
max_cols : int, optional
15161516
Maximum number of columns to show before truncating. If None, show
15171517
all.
1518+
decimal : string, default '.'
1519+
Character recognized as decimal separator, e.g. ',' in Europe
15181520
1521+
.. versionadded:: 0.18.0
15191522
"""
15201523

15211524
if colSpace is not None: # pragma: no cover
@@ -1533,7 +1536,8 @@ def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,
15331536
bold_rows=bold_rows, escape=escape,
15341537
max_rows=max_rows,
15351538
max_cols=max_cols,
1536-
show_dimensions=show_dimensions)
1539+
show_dimensions=show_dimensions,
1540+
decimal=decimal)
15371541
# TODO: a generic formatter wld b in DataFrameFormatter
15381542
formatter.to_html(classes=classes, notebook=notebook)
15391543

@@ -1545,7 +1549,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15451549
header=True, index=True, na_rep='NaN', formatters=None,
15461550
float_format=None, sparsify=None, index_names=True,
15471551
bold_rows=True, column_format=None, longtable=None,
1548-
escape=None, encoding=None):
1552+
escape=None, encoding=None, decimal='.'):
15491553
"""
15501554
Render a DataFrame to a tabular environment table. You can splice
15511555
this into a LaTeX document. Requires \\usepackage{booktabs}.
@@ -1568,6 +1572,11 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15681572
characters in column names.
15691573
encoding : str, default None
15701574
Default encoding is ascii in Python 2 and utf-8 in Python 3
1575+
decimal : string, default '.'
1576+
Character recognized as decimal separator, e.g. ',' in Europe
1577+
1578+
.. versionadded:: 0.18.0
1579+
15711580
"""
15721581

15731582
if colSpace is not None: # pragma: no cover
@@ -1588,7 +1597,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None,
15881597
bold_rows=bold_rows,
15891598
sparsify=sparsify,
15901599
index_names=index_names,
1591-
escape=escape)
1600+
escape=escape, decimal=decimal)
15921601
formatter.to_latex(column_format=column_format, longtable=longtable,
15931602
encoding=encoding)
15941603

pandas/tests/test_format.py

+46
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,34 @@ def test_to_html_unicode(self):
760760
expected = u'<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>A</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>\u03c3</td>\n </tr>\n </tbody>\n</table>'
761761
self.assertEqual(df.to_html(), expected)
762762

763+
def test_to_html_decimal(self):
764+
# GH 12031
765+
df = DataFrame({'A': [6.0, 3.1, 2.2]})
766+
result = df.to_html(decimal=',')
767+
expected = ('<table border="1" class="dataframe">\n'
768+
' <thead>\n'
769+
' <tr style="text-align: right;">\n'
770+
' <th></th>\n'
771+
' <th>A</th>\n'
772+
' </tr>\n'
773+
' </thead>\n'
774+
' <tbody>\n'
775+
' <tr>\n'
776+
' <th>0</th>\n'
777+
' <td>6,0</td>\n'
778+
' </tr>\n'
779+
' <tr>\n'
780+
' <th>1</th>\n'
781+
' <td>3,1</td>\n'
782+
' </tr>\n'
783+
' <tr>\n'
784+
' <th>2</th>\n'
785+
' <td>2,2</td>\n'
786+
' </tr>\n'
787+
' </tbody>\n'
788+
'</table>')
789+
self.assertEqual(result, expected)
790+
763791
def test_to_html_escaped(self):
764792
a = 'str<ing1 &amp;'
765793
b = 'stri>ng2 &amp;'
@@ -2897,6 +2925,24 @@ def test_to_latex_no_header(self):
28972925

28982926
self.assertEqual(withoutindex_result, withoutindex_expected)
28992927

2928+
def test_to_latex_decimal(self):
2929+
# GH 12031
2930+
self.frame.to_latex()
2931+
df = DataFrame({'a': [1.0, 2.1], 'b': ['b1', 'b2']})
2932+
withindex_result = df.to_latex(decimal=',')
2933+
print("WHAT THE")
2934+
withindex_expected = r"""\begin{tabular}{lrl}
2935+
\toprule
2936+
{} & a & b \\
2937+
\midrule
2938+
0 & 1,0 & b1 \\
2939+
1 & 2,1 & b2 \\
2940+
\bottomrule
2941+
\end{tabular}
2942+
"""
2943+
2944+
self.assertEqual(withindex_result, withindex_expected)
2945+
29002946
def test_to_csv_quotechar(self):
29012947
df = DataFrame({'col': [1, 2]})
29022948
expected = """\

0 commit comments

Comments
 (0)