diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1cc689528caaa..6fb0090dea114 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 diff --git a/pandas/formats/format.py b/pandas/formats/format.py index e5089983ac8f7..7706666142a64 100644 --- a/pandas/formats/format.py +++ b/pandas/formats/format.py @@ -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): diff --git a/pandas/tests/formats/test_format.py b/pandas/tests/formats/test_format.py index 58e9b30e7f624..3bbfd621d2342 100644 --- a/pandas/tests/formats/test_format.py +++ b/pandas/tests/formats/test_format.py @@ -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