diff --git a/pandas/io/common.py b/pandas/io/common.py index 161c4cba24c22b..f6129eaa2a3f5d 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -4,7 +4,6 @@ import csv import codecs import mmap -import io from contextlib import contextmanager, closing import zipfile @@ -395,7 +394,9 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None, elif is_path: if compat.PY2: # Python 2 - f = io.open(path_or_buf, mode, newline='\n') + if mode == 'w': + mode = 'wb' + f = open(path_or_buf, mode) elif encoding: # Python 3 and encoding f = open(path_or_buf, mode, encoding=encoding, newline='\n') diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index b3adfd91929e4f..fc36a10d5dc071 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -307,7 +307,7 @@ def test_to_csv_string_with_lf(self): assert f.read() == expected_bin # 'line_terminator' should not change inner element - expected_bin =( + expected_bin = ( b'int,str_lf\r\n' b'1,abc\r\n' b'2,"d\nef"\r\n'