Skip to content

Commit 43b949d

Browse files
chris-b1jorisvandenbossche
authored andcommitted
BUG: to_csv line endings with compression (#25625)
1 parent c4fa3c9 commit 43b949d

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.24.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Fixed Regressions
3232
- Fixed regression in creating a period-dtype array from a read-only NumPy array of period objects. (:issue:`25403`)
3333
- Fixed regression in :class:`Categorical`, where constructing it from a categorical ``Series`` and an explicit ``categories=`` that differed from that in the ``Series`` created an invalid object which could trigger segfaults. (:issue:`25318`)
3434
- Fixed pip installing from source into an environment without NumPy (:issue:`25193`)
35+
- Fixed regression in :meth:`DataFrame.to_csv` writing duplicate line endings with gzip compress (:issue:`25311`)
3536

3637
.. _whatsnew_0242.enhancements:
3738

pandas/io/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
434434
if (compat.PY3 and is_text and
435435
(compression or isinstance(f, need_text_wrapping))):
436436
from io import TextIOWrapper
437-
f = TextIOWrapper(f, encoding=encoding)
437+
f = TextIOWrapper(f, encoding=encoding, newline='')
438438
handles.append(f)
439439

440440
if memory_map and hasattr(f, 'fileno'):

pandas/tests/frame/test_to_csv.py

+12
Original file line numberDiff line numberDiff line change
@@ -1221,3 +1221,15 @@ def test_multi_index_header(self):
12211221
'1,5,6,7,8']
12221222
expected = tm.convert_rows_list_to_csv_str(expected_rows)
12231223
assert result == expected
1224+
1225+
def test_gz_lineend(self):
1226+
# GH 25311
1227+
df = pd.DataFrame({'a': [1, 2]})
1228+
expected_rows = ['a', '1', '2']
1229+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
1230+
with ensure_clean('__test_gz_lineend.csv.gz') as path:
1231+
df.to_csv(path, index=False)
1232+
with tm.decompress_file(path, compression='gzip') as f:
1233+
result = f.read().decode('utf-8')
1234+
1235+
assert result == expected

0 commit comments

Comments
 (0)