Skip to content

to_latex encoding issue #14329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,8 @@ def to_latex(self, buf=None, columns=None, col_space=None, header=True,
When set to False prevents from escaping latex special
characters in column names.
encoding : str, default None
Default encoding is ascii in Python 2 and utf-8 in Python 3
A string representing the encoding to use in the output file,
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
decimal : string, default '.'
Character recognized as decimal separator, e.g. ',' in Europe

Expand Down
3 changes: 3 additions & 0 deletions pandas/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,9 @@ def to_latex(self, column_format=None, longtable=False, encoding=None):
latex_renderer = LatexFormatter(self, column_format=column_format,
longtable=longtable)

if encoding is None:
encoding = 'ascii' if compat.PY2 else 'utf-8'

if hasattr(self.buf, 'write'):
latex_renderer.write_result(self.buf)
elif isinstance(self.buf, compat.string_types):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2823,7 +2823,7 @@ def test_to_latex_filename(self):
if compat.PY3: # python3: pandas default encoding is utf-8
with tm.ensure_clean('test.tex') as path:
df.to_latex(path)
with codecs.open(path, 'r') as f:
with codecs.open(path, 'r', encoding='utf-8') as f:
self.assertEqual(df.to_latex(), f.read())
else:
# python2 default encoding is ascii, so an error should be raised
Expand Down