Skip to content

Commit 04f6cde

Browse files
nbonnottejorisvandenbossche
authored andcommitted
to_latex encoding follows the documentation (py2 ascii, py3 utf8) (#14329)
* to_latex: encoding default follows the documentation (py2 ascii, py3 utf8) * Adapt tests: codecs.open with explicit encoding parameter
1 parent db79aa7 commit 04f6cde

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,8 @@ def to_latex(self, buf=None, columns=None, col_space=None, header=True,
16351635
When set to False prevents from escaping latex special
16361636
characters in column names.
16371637
encoding : str, default None
1638-
Default encoding is ascii in Python 2 and utf-8 in Python 3
1638+
A string representing the encoding to use in the output file,
1639+
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
16391640
decimal : string, default '.'
16401641
Character recognized as decimal separator, e.g. ',' in Europe
16411642

pandas/formats/format.py

+3
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@ def to_latex(self, column_format=None, longtable=False, encoding=None):
654654
latex_renderer = LatexFormatter(self, column_format=column_format,
655655
longtable=longtable)
656656

657+
if encoding is None:
658+
encoding = 'ascii' if compat.PY2 else 'utf-8'
659+
657660
if hasattr(self.buf, 'write'):
658661
latex_renderer.write_result(self.buf)
659662
elif isinstance(self.buf, compat.string_types):

pandas/tests/formats/test_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2823,7 +2823,7 @@ def test_to_latex_filename(self):
28232823
if compat.PY3: # python3: pandas default encoding is utf-8
28242824
with tm.ensure_clean('test.tex') as path:
28252825
df.to_latex(path)
2826-
with codecs.open(path, 'r') as f:
2826+
with codecs.open(path, 'r', encoding='utf-8') as f:
28272827
self.assertEqual(df.to_latex(), f.read())
28282828
else:
28292829
# python2 default encoding is ascii, so an error should be raised

0 commit comments

Comments
 (0)