From 9c3ac20946a4eef4268858ef3c97c9cf5701c2be Mon Sep 17 00:00:00 2001 From: Nicolas Bonnotte Date: Sat, 1 Oct 2016 19:26:33 +0200 Subject: [PATCH 1/2] to_latex: encoding default follows the documentation (py2 ascii, py3 utf8) --- pandas/core/frame.py | 3 ++- pandas/formats/format.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) 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): From f480b93570597a261b9388e6cbf2a96456be2762 Mon Sep 17 00:00:00 2001 From: Nicolas Bonnotte Date: Sun, 2 Oct 2016 09:27:53 +0200 Subject: [PATCH 2/2] Adapt tests: codecs.open with explicit encoding parameter --- pandas/tests/formats/test_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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